Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for strictStorage #117

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ app.use('/messages', service({ storage, id, startId, name, store, paginate }));
__Options:__

- `storage` (**required**) - The local storage engine. You can pass in the browsers `window.localStorage`, React Native's `AsyncStorage` or a NodeJS localstorage module.
- `throttle` (*optional*, default `200`) - The minimum time (ms) before in-memory data is written to `storage`. Data is only written if changed since last write.
- `throttle` (*optional*, default `200`) - The minimum time (ms) before in-memory data is written to `storage`.
- `id` (*optional*, default: `'id'`) - The name of the id field property.
- `startId` (*optional*, default: `0`) - An id number to start with that will be incremented for new record.
- `name` (*optional*, default: `'feathers'`) - The key to store data under in local or async storage.
Expand All @@ -43,6 +43,7 @@ __Options:__
- `whitelist` (*optional*) - A list of additional query parameters to allow.
- `multi` (*optional*) - Allow `create` with arrays and `update` and `remove` with `id` `null` to change multiple items. Can be `true` for all methods or an array of allowed methods (e.g. `[ 'remove', 'create' ]`).
- `reuseKeys` (*optional*, default: `false`) Allow duplicate keys i.e. last definition wins. Mostly useful for demonstration and testing purposes.
- `strictStorage` (*optional*, default false) - When false, all storage data is held in memory after initialization. When true, the storage data is get/set on each method.

## Example

Expand Down
10 changes: 8 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class LocalStorage extends Service {
this._storageKey = options.name || 'feathers';
this._storage = options.storage || (typeof window !== 'undefined' && window.localStorage);
this._throttle = options.throttle || 200;
this._strictStorage = options.strictStorage || false;
this._reuseKeys = options.reuseKeys || false;
this.store = null;

Expand Down Expand Up @@ -49,6 +50,9 @@ class LocalStorage extends Service {
this._timeout = setTimeout(() => {
this._storage.setItem(this._storageKey, JSON.stringify(this.store));
delete this._timeout;
if (this._strictStorage) {
this.store = null;
}
}, this._throttle);
}

Expand All @@ -61,11 +65,13 @@ class LocalStorage extends Service {
}

find (...args) {
return this.execute('find', ...args);
return this.execute('find', ...args)
.then(data => this._strictStorage ? this.flush(data) : data);
}

get (...args) {
return this.execute('get', ...args);
return this.execute('get', ...args)
.then(data => this._strictStorage ? this.flush(data) : data);
}

create (...args) {
Expand Down
27 changes: 27 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,33 @@ describe('Feathers Localstorage Service', () => {
done();
});

it('clears memory when using strictStorage', () => {
const name = 'test-storage-8';

storage.setItem(name, '{ "0": { "id": 0, "text": "test 0" } }');

const app = feathers()
.use('/messages', service({ name, storage, strictStorage: true }));

return app.service('messages').find()
.then(() => {
return new Promise((resolve) => {
setTimeout(() => {
assert.notStrictEqual(app.service('messages').store, null);
resolve();
}, 100);
});
})
.then(() => {
return new Promise((resolve) => {
setTimeout(() => {
assert.strictEqual(app.service('messages').store, null);
resolve();
}, 150);
});
});
});

testSuite(app, errors, 'people');
testSuite(app, errors, 'people-customid', 'customid');
});
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Service as MemoryService, MemoryServiceOptions, MemoryServiceStore } fr
export interface LocalStorageServiceOptions extends MemoryServiceOptions {
name: string;
throttle: number;
strictStorage: boolean;
}

export class Service<T = any> extends MemoryService<T> {
Expand Down