Skip to content

Commit

Permalink
fix(Decorator): read value from first storage decorator and save to f…
Browse files Browse the repository at this point in the history
…ollowing ones in order to keep consistent data
  • Loading branch information
DanielKucal committed Feb 17, 2018
1 parent 9020c06 commit abd270d
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ngx-store",
"private": true,
"version": "1.4.0-RC2",
"version": "1.4.0-RC4",
"description": "Angular decorators to automagically keep variables in HTML5 LocalStorage, SessionStorage, cookies; injectable services for managing and listening to data changes and a bit more.",
"main": "./ngx-store.js",
"main for angular5": "./bundles/ngx-store.umd.js",
Expand Down
4 changes: 3 additions & 1 deletion src/decorator/cache-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface CacheItemInterface {
targets: Array<Object>;
services: Array<WebStorageServiceInterface>;
utilities: Array<WebStorageUtility>;
config?: DecoratorConfig;
}

export class CacheItem implements CacheItemInterface {
Expand Down Expand Up @@ -148,10 +149,11 @@ export class CacheItem implements CacheItemInterface {
});
}

public addUtilities(utilities: Array<WebStorageUtility>): void {
public addUtilities(utilities: Array<WebStorageUtility>, config?: DecoratorConfig): void {
utilities.forEach(utility => {
if (this.utilities.indexOf(utility) === -1) {
this.utilities.push(utility);
utility.set(this.key, this.readValue(config));
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/decorator/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export class Cache {
Cache.set(cacheItem);
return cacheItem;
}
debug.log(`Loaded prior CacheItem of ${cacheItem.name} for ${cacheItem.utilities[0].getStorageName()}`);
debug.log(`Loaded prior CacheItem of ${cacheItem.name} for ${cacheCandidate.utilities[0].getStorageName()}`);
cacheItem.addTargets(cacheCandidate.targets);
cacheItem.addServices(cacheCandidate.services);
cacheItem.addUtilities(cacheCandidate.utilities);
cacheItem.addUtilities(cacheCandidate.utilities, cacheCandidate.config);
Cache.set(cacheItem);
return cacheItem;
}
Expand Down
5 changes: 3 additions & 2 deletions src/decorator/webstorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function WebStorage(
config: DecoratorConfig = {}
) {
return (target: any, propertyName: string): void => {
let key;
let key: string;
if (typeof keyOrConfig === 'object') {
key = keyOrConfig.key;
config = keyOrConfig;
Expand All @@ -62,7 +62,8 @@ function WebStorage(
name: propertyName,
targets: [ target ],
services: [ service ],
utilities: [ webStorageUtility ]
utilities: [ webStorageUtility ],
config: config,
});

Object.defineProperty(target, propertyName, {
Expand Down
3 changes: 2 additions & 1 deletion src/utility/webstorage.utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export class WebStorageUtility {
this._storage.setItem(storageKey, storable, config.expires);
const cacheItem = Cache.get(key);
if (cacheItem) {
cacheItem.saveValue(value, config, this);
debug.log(`updating following CacheItem from ${this.constructor.name}:`, cacheItem);
cacheItem.resetProxy();
}
} catch (error) {
console.warn(`[ngx-store] ${this.getStorageName()}: following error occurred while trying to save ${key} =`, value);
Expand Down

0 comments on commit abd270d

Please sign in to comment.