localforage typings

This commit is contained in:
Gabriel Garcia
2015-07-24 17:46:45 -04:00
parent f6c8ca4719
commit 76ea613b90
+70 -65
View File
@@ -3,71 +3,76 @@
// Definitions by: yuichi david pichsenmeister <https://github.com/3x14159265>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module lf {
interface ILocalForage<T> {
/**
* Removes every key from the database, returning it to a blank slate.
*/
clear(callback: IErrorCallback): void
/**
* Iterate over all value/key pairs in datastore.
*/
iterate(iterateCallback: IIterateCallback<T>): void
/**
* Get the name of a key based on its ID.
*/
key(keyIndex: number, callback: IKeyCallback): void
/**
* Get the list of all keys in the datastore.
*/
keys(callback: IKeysCallback): void;
/**
* Gets the number of keys in the offline store (i.e. its “length”).
*/
length(callback: INumberCallback): void
/**
* Gets an item from the storage library and supplies the result to a callback.
* If the key does not exist, getItem() will return null.
*/
getItem(key: string, callback: ICallback<T>): void
getItem(key: string): IPromise<T>
/**
* Saves data to an offline store.
*/
setItem(key: string, value: T, callback: ICallback<T>): void
setItem(key: string, value: T): IPromise<T>
/**
* Removes the value of a key from the offline store.
*/
removeItem(key: string, callback: IErrorCallback): void
removeItem(key: string): IPromise<T>
}
/// <reference path="../es6-promise/es6-promise.d.ts" />
interface ICallback<T> {
(err: any, value: T): void
}
interface LocalForageOptions {
driver?: LocalForageDriver | LocalForageDriver[];
name?: string;
size?: number;
storeName?: string;
version?: string;
description?: string;
}
interface IIterateCallback<T> {
(value: T, key: string, iterationNumber: number): void
}
interface LocalForageDriver {
_driver: string;
_initStorage(options: LocalForageOptions): void;
_support: boolean | Promise<boolean>;
clear(callback: (err: any) => void): void;
getItem(key: string, callback: (err: any, value: any) => void): void;
key(keyIndex: number, callback: (err: any, key: string) => void): void;
keys(callback: (err: any, keys: string[]) => void): void;
length(callback: (err: any, numberOfKeys: number) => void): void;
removeItem(key: string, callback: (err: any) => void): void;
setItem(key: string, value: any, callback: (err: any, value: any) => void): void;
}
interface IErrorCallback {
(err: any): void
}
interface IKeyCallback {
(err: any, keyName: string): void
}
interface IKeysCallback {
(err: any, keys: Array<string>): void
}
interface INumberCallback {
(err: any, numberOfKeys: number): void
}
interface IPromise<T> {
then(callback: ICallback<T>): void
}
}
interface LocalForage {
LOCALSTORAGE: LocalForageDriver;
WEBSQL: LocalForageDriver;
INDEXEDDB: LocalForageDriver;
config(options: LocalForageOptions): void;
setDriver(driver: LocalForageDriver): void;
setDriver(driver: LocalForageDriver[]): void;
getItem<T>(key: string): Promise<T>;
getItem<T>(key: string, callback: (err: any, value: T) => void): void;
setItem<T>(key: string, value: T): Promise<T>;
setItem<T>(key: string, value: T, callback: (err: any, value: T) => void): void;
removeItem(key: string): Promise<void>;
removeItem(key: string, callback: (err: any) => void): void;
clear(): Promise<void>;
clear(callback: (err: any) => void): void;
length(): Promise<number>;
length(callback: (err: any, numberOfKeys: number) => void): void;
key(keyIndex: number): Promise<string>;
key(keyIndex: number, callback: (err: any, key: string) => void): void;
keys(): Promise<string[]>;
keys(callback: (err: any, keys: string[]) => void): void;
iterate(iteratee: (value: any, key: string, iterationNumber: number) => any): Promise<any>;
iterate(iteratee: (value: any, key: string, iterationNumber: number) => any,
callback: (err: any, result: any) => void): void;
}