Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
58 lines (48 sloc) 2.22 KB
layout title description group toc github_issue_id
api
localStorage
Local Visuals Storage API
references
true
467

The Local Storage API is an API a custom visual can use to request the host to save or load data from the device's storage. It is isolated in the sense that there's separation of storage access between different visual types.

Methods

Method                               Return type Brief description
get(key: string) IPromise<string> Returns a promise that resolves to the data associated with 'key' if it was found or rejects otherwise.
set(key: string, data: string) IPromise<number> Saves the data to the local storage. This data can be later be retrieved using the 'key'. Returns a promise that resolves to the amount of free space available to caller after the save if there is any or rejects otherwise.
remove(key: string) void Deletes data associated with the 'key' from local storage.

Sample

If the custom visual should increase some counter every time the update method is called, but the counter value should also be preserved and not reset on every visual start:

    export class Visual implements IVisual {
        ...
        private updateCountName: string = 'updateCount';
        private updateCount: number;
        private storage: ILocalVisualStorageService;
        ...

        constructor(options: VisualConstructorOptions) {

            ...
            this.storage = options.host.storageService;
            ...

            ...
            this.storage.get(this.updateCountName).then(count =>
            {
                this.updateCount = +count;
            })
            .catch(() =>
            {
                this.updateCount = 0;
                this.storage.set(this.updateCountName, this.updateCount.toString());
            });
            ...
        }

        public update(options: VisualUpdateOptions) {
            ...
            this.updateCount++;
            this.storage.set(this.updateCountName, this.updateCount.toString());
            ...
        }
You can’t perform that action at this time.