Merge pull request #8315 from borislavjivkov/large-local-storage

Added LargeLocalStorage definitions
This commit is contained in:
Masahiro Wakame
2016-03-10 22:32:31 +09:00
2 changed files with 235 additions and 0 deletions

109
lls/lls-tests.ts Normal file
View File

@@ -0,0 +1,109 @@
/// <reference path="lls.d.ts" />
import LargeLocalStorage = require('lls');
var storage = new LargeLocalStorage({size: 75*1024*1024});
storage.initialized.then(function(grantedCapacity) {
// storage ready to be used.
});
var desiredCapacity = 50 * 1024 * 1024; // 50MB
var storage = new LargeLocalStorage({
// desired capacity, in bytes.
size: desiredCapacity,
// optional name for your LLS database. Defaults to lls.
// This is the name given to the underlying
// IndexedDB or WebSQL DB or FSAPI Folder.
// LLS's with different names are independent.
name: 'myStorage'
// the following is an optional param
// that is useful for debugging.
// force LLS to use a specific storage implementation
// forceProvider: 'IndexedDB' or 'WebSQL' or 'FilesystemAPI'
});
storage.initialized.then(function(capacity) {
if (capacity != -1 && capacity != desiredCapacity) {
// the user didn't authorize your storage request
// so instead you have some limitation on your storage
}
});
storage.getAllAttachments('exampleDoc').then(function(attachEntries) {
attachEntries.map(function(entry) {
var a = entry.data;
// do something with it...
if (a.type.indexOf('image') == 0) {
// show image...
} else if (a.type.indexOf('audio') == 0) {
// play audio...
} else {
}
})
});
storage.getAllAttachmentURLs('exampleDoc').then(function(urlEntries) {
urlEntries.map(function(entry) {
var url = entry.url;
})
});
storage.getAttachment('exampleDoc', 'examplePic').then(function(attachment) {
var url = URL.createObjectURL(attachment);
});
storage.getAttachmentURL('myDoc', 'myPic').then(function(url) {
var image = new Image();
image.src = url;
document.body.appendChild(image);
storage.revokeAttachmentURL(url);
});
// the initialized property will call you back with the capacity
storage.initialized.then(function(capacity) {
console.log('Authorized to store: ' + capacity + ' bytes');
});
// or if you know your storage is already available
// you can call getCapacity directly
storage.getCapacity();
storage.getContents('exampleDoc').then(function(contents) {
alert(contents);
});
storage.ls().then(function(docKeys) {
console.log(docKeys);
});
// may or may not be true
storage.ready();
storage.initialized.then(function() {
// always true
storage.ready();
});
storage.getAttachmentURL('doc', 'attach').then(function(url) {
// do something with the URL
storage.revokeAttachmentURL(url);
});
storage.rm('exampleDoc').then(function() {
alert('doc and all attachments were removed');
});
storage.rmAttachment('exampleDoc', 'someAttachment').then(function() {
alert('exampleDoc/someAttachment removed');
}).catch(function(e) {
alert('Attachment removal failed: ' + e);
});
storage.setAttachment('myDoc', 'myPic', [1, 2, 3]).then(function() {
alert('Attachment written');
});
storage.setContents('exampleDoc', 'some data...').then(function() {
alert('doc written');
});

126
lls/lls.d.ts vendored Normal file
View File

@@ -0,0 +1,126 @@
// Type definitions for LargeLocalStorage v0.1.3
// Project: https://github.com/tantaman/LargeLocalStorage
// Definitions by: Borislav Zhivkov <https://github.com/borislavjivkov>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module LargeLocalStorageInterfaces {
interface LargeLocalStorageService {
new(options: Options): LargeLocalStorageService;
initialized: Promise<number>;
/**
* Gets all of the attachments for a document.
*/
getAllAttachments(docKey?: string): Promise<Entry[]>;
/**
* Gets all attachments URLs for a document.
*/
getAllAttachmentURLs(docKey?: string): Promise<Entry[]>;
/**
* Get the attachment identified by attachKey
*/
getAttachment(attachKey: string): Promise<any>;
/**
* Get the attachment identified by docKey and attachKey
*/
getAttachment(docKey: string, attachKey: string): Promise<any>;
/**
* Get the URL for a given attachment.
*/
getAttachmentURL(attachKey: string): Promise<string>;
/**
* Get the URL for a given attachment.
*/
getAttachmentURL(docKey: string, attachKey: string): Promise<string>;
/**
* Returns the actual capacity of the storage or -1 if it is unknown.
*/
getCapacity(): number;
/**
* Get the contents of a document identified by docKey
*/
getContents(docKey: string): Promise<any>;
/**
* List all attachments under a given key. List all documents if no key is provided.
*/
ls(docKey?: string): Promise<string[]>;
/**
* Whether or not LLS is ready to store data. The initialized property can be used to await initialization.
*/
ready(): boolean;
/**
* Revoke the attachment URL as required by the underlying storage system.
*/
revokeAttachmentURL(url: string): void;
/**
* Remove the specified document and all of its attachments.
*/
rm(docKey?: string): Promise<any>;
/**
* Remove an attachment from a document.
*/
rmAttachment(docKey: string, attachKey: string): Promise<void>;
/**
* Set an attachment for a given document. Identified by attachKey.
*/
setAttachment(attachKey: string, attachment: any): Promise<void>;
/**
* Set an attachment for a given document. Identified by docKey and attachKey.
*/
setAttachment(docKey: string, attachKey: string, attachment: any): Promise<void>;
/**
* Set the contents identified by docKey to data. The document will be created if it does not exist.
*/
setContents(docKey: string, data: any): Promise<void>;
}
interface Options {
/**
* Desired capacity in bytes.
*/
size: number;
/**
* Optional name for your LLS database. Defaults to lls. This is the name given to the underlying IndexedDB or WebSQL DB or FSAPI Folder. LLS's with different names are independent.
*/
name?: string;
/**
* Force LLS to use a specific storage implementation: 'IndexedDB' or 'WebSQL' or 'FilesystemAPI'.
*/
forceProvider?: string;
}
interface Entry {
data: any;
docKey: string;
attachKey: string;
url: string;
}
interface Promise<T> {
then<U>(onFulfilled?: (value: T) => U | Promise<U>, onRejected?: (error: any) => U | Promise<U>): Promise<U>;
then<U>(onFulfilled?: (value: T) => U | Promise<U>, onRejected?: (error: any) => void): Promise<U>;
catch<U>(onRejected?: (error: any) => U | Promise<U>): Promise<U>;
}
}
declare module "lls" {
var LargeLocalStorage: LargeLocalStorageInterfaces.LargeLocalStorageService;
export = LargeLocalStorage;
}