Skip to content
This repository was archived by the owner on Aug 18, 2018. It is now read-only.

Commit 5e0c735

Browse files
committed
clearAllColletions removed. deprectateCollections now, just deprecate collection
1 parent b37f4df commit 5e0c735

File tree

8 files changed

+35
-7
lines changed

8 files changed

+35
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-angular-jsonapi",
3-
"version": "0.6.20",
3+
"version": "0.6.21",
44
"description": "JSONAPI library developed for AngularJS in Typescript",
55
"repository": {
66
"type": "git",

src/library/interfaces/cache.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import { ICollection, IResource } from '../interfaces';
33
export interface ICache {
44
setCollection(url: string, collection: ICollection): void;
55
setResource(resource: IResource): void;
6+
deprecateCollections(path_start_with: string): boolean;
67
}

src/library/interfaces/cachememory.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export interface ICacheMemory extends ICache {
77
getOrCreateCollection(url: string): ICollection;
88
isCollectionExist(url: string): boolean;
99
isCollectionLive(url: string, ttl: number): boolean;
10-
clearAllCollections(): boolean;
1110

1211
isResourceLive(id: string, ttl: number): boolean;
1312
getOrCreateResource(type: string, id: string): IResource;

src/library/resource.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ export class Resource extends ParentResourceService implements IResource {
148148

149149
// foce reload cache (for example, we add a new element)
150150
if (!this.id) {
151-
this.getService().cachememory.clearAllCollections();
151+
this.getService().cachememory.deprecateCollections(path.get());
152+
this.getService().cachestore.deprecateCollections(path.get());
152153
}
153154

154155
// is a resource?

src/library/service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ export class Service extends ParentResourceService implements IService {
304304
}
305305

306306
public clearCacheMemory(): boolean {
307-
return this.getService().cachememory.clearAllCollections();
307+
return this.getService().cachememory.deprecateCollections(this.type) &&
308+
this.getService().cachestore.deprecateCollections(this.type);
308309
}
309310

310311
public parseFromServer(attributes: IAttributes): void {

src/library/services/cachememory.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@ export class CacheMemory implements ICacheMemory {
6363
this.resources[resource.id].lastupdate = (update_lastupdate ? Date.now() : 0);
6464
}
6565

66-
public clearAllCollections(): boolean {
67-
this.collections = {};
68-
this.collections_lastupdate = {};
66+
public deprecateCollections(path_start_with: string): boolean {
67+
angular.forEach(this.collections_lastupdate, (lastupdate: number, key: string) => {
68+
// we don't need check, memorycache has one collection per resoure type
69+
// if (key.startsWith(path_start_with) === true) {
70+
this.collections_lastupdate[key] = 0;
71+
// }
72+
});
6973
return true;
7074
}
7175

src/library/services/cachestore.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,11 @@ export class CacheStore implements ICacheStore {
107107
tmp
108108
);
109109
}
110+
111+
public deprecateCollections(collection_type: string) {
112+
Core.injectedServices.JsonapiStoreService.deprecateObjectsWithKey(
113+
'collection.' + collection_type
114+
);
115+
return true;
116+
}
110117
}

src/library/sources/store.service.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,20 @@ export class StoreService {
6060
this.allstore.clear();
6161
this.globalstore.clear();
6262
}
63+
64+
public deprecateObjectsWithKey(key_start_with: string) {
65+
this.allstore.keys().then(success => {
66+
angular.forEach(success, (key: string) => {
67+
if (key.startsWith(key_start_with)) {
68+
// key of stored object starts with key_start_with
69+
this.allstore.getItem(key).then(success2 => {
70+
success2['_lastupdate_time'] = 0;
71+
this.allstore.setItem(key, success2);
72+
});
73+
}
74+
});
75+
});
76+
}
6377
}
78+
6479
angular.module('Jsonapi.services').service('JsonapiStoreService', StoreService);

0 commit comments

Comments
 (0)