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

Commit 7630882

Browse files
committed
store with promise
1 parent 64429fa commit 7630882

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

src/library/interfaces/cache.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export interface ICache {
55

66
isCollectionExist(url: string): boolean;
77
isCollectionLive(url: string, ttl: number): boolean;
8-
getOrCreateCollection(url: string, use_store?: boolean): ICollection;
8+
getOrCreateCollection(url: string): ICollection;
9+
getCollectionFromStorePromise(url:string, collection: ICollection): ng.IPromise<ICollection>;
910
setCollection(url: string, collection: ICollection): void;
1011
clearAllCollections(): boolean;
1112

src/library/service.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class Service extends ParentResourceService implements IService {
8282
let resource = this.getService().cachememory.getOrCreateResource(this.type, id, true);
8383
resource.is_loading = true;
8484
// exit if ttl is not expired
85-
let temporal_ttl = params.ttl ? params.ttl : 0;
85+
let temporal_ttl = params.ttl || 0; // this.schema.ttl
8686
if (this.getService().cachememory.isResourceLive(id, temporal_ttl)) {
8787
// we create a promise because we need return collection before
8888
// run success client function
@@ -134,10 +134,10 @@ export class Service extends ParentResourceService implements IService {
134134

135135
// make request
136136
// if we remove this, dont work the same .all on same time (ej: <component /><component /><component />)
137-
let tempororay_collection = this.getService().cachememory.getOrCreateCollection(path.getForCache(), true);
137+
let tempororay_collection = this.getService().cachememory.getOrCreateCollection(path.getForCache());
138138

139139
// MEMORY_CACHE
140-
let temporal_ttl = params.ttl ? params.ttl : this.schema.ttl;
140+
let temporal_ttl = params.ttl || this.schema.ttl;
141141
if (temporal_ttl >= 0 && this.getService().cachememory.isCollectionExist(path.getForCache())) {
142142
// get cached data and merge with temporal collection
143143
tempororay_collection.$source = 'memory';
@@ -162,8 +162,38 @@ export class Service extends ParentResourceService implements IService {
162162
});
163163
return tempororay_collection;
164164
}
165+
} else {
166+
// STORE
167+
this.getService().cachememory.getCollectionFromStorePromise(path.getForCache(), tempororay_collection)
168+
.then(
169+
success => {
170+
tempororay_collection.$source = 'store';
171+
tempororay_collection.$is_loading = false;
172+
173+
// localfilter getted data
174+
let localfilter = new LocalFilter();
175+
tempororay_collection = localfilter.filterCollection(tempororay_collection, params.localfilter);
176+
177+
if (temporal_ttl >= 0 && Date.now() <= (tempororay_collection.$cache_last_update + temporal_ttl * 1000)) {
178+
this.runFc(fc_success, { data: success});
179+
} else {
180+
this.getAllFromHttpStorage(path, params, fc_success, fc_error, tempororay_collection);
181+
}
182+
},
183+
error => {
184+
this.getAllFromHttpStorage(path, params, fc_success, fc_error, tempororay_collection);
185+
}
186+
);
165187
}
166188

189+
return tempororay_collection;
190+
}
191+
192+
/**
193+
@deprecated
194+
**/
195+
private getAllFromHttpStorage(path, params, fc_success, fc_error, tempororay_collection: ICollection) {
196+
// SERVER REQUEST
167197
tempororay_collection['$is_loading'] = true;
168198

169199
// STORAGE_CACHE
@@ -192,8 +222,6 @@ export class Service extends ParentResourceService implements IService {
192222
this.getAllFromServer(path, params, fc_success, fc_error, tempororay_collection);
193223
}
194224
);
195-
196-
return tempororay_collection;
197225
}
198226

199227
private getAllFromServer(path, params, fc_success, fc_error, tempororay_collection: ICollection) {

src/library/services/cachememory.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ export class CacheMemory implements ICache {
2424
return this.resources[id] && (Date.now() <= (this.resources[id].lastupdate + ttl * 1000));
2525
}
2626

27-
public getOrCreateCollection(url: string, use_store = false): ICollection {
27+
public getOrCreateCollection(url: string): ICollection {
2828
if (!(url in this.collections)) {
2929
this.collections[url] = Base.newCollection();
3030
this.collections[url].$source = 'new';
31-
if (use_store) {
32-
this.getCollectionFromStore(url, this.collections[url]);
33-
}
3431
}
3532
return this.collections[url];
3633
}
@@ -112,10 +109,20 @@ export class CacheMemory implements ICache {
112109
);
113110
}
114111

115-
private getCollectionFromStore(url:string, collection: ICollection): void {
112+
public getCollectionFromStorePromise(url:string, collection: ICollection): ng.IPromise<object> {
113+
var deferred = Core.injectedServices.$q.defer();
114+
this.getCollectionFromStore(url, collection, deferred);
115+
return deferred.promise;
116+
}
117+
118+
private getCollectionFromStore(url:string, collection: ICollection, job_deferred: ng.IDeferred<ICollection> = null): void {
116119
let promise = Core.injectedServices.JsonapiStoreService.getObjet('collection.' + url);
117120
promise.then(success => {
118-
if (success) {
121+
try {
122+
if (!success) {
123+
throw '';
124+
}
125+
119126
// build collection from store and resources from memory
120127
let all_ok = true;
121128
for (let key in success.data) {
@@ -129,7 +136,8 @@ export class CacheMemory implements ICache {
129136
}
130137
if (all_ok) {
131138
collection.$source = 'store'; // collection from storeservice, resources from memory
132-
return;
139+
job_deferred.resolve(collection);
140+
return ;
133141
}
134142

135143
// request resources from store
@@ -144,19 +152,26 @@ export class CacheMemory implements ICache {
144152
}
145153

146154
// build collection and resources from store
147-
Core.injectedServices.$q.all(promises).then(success => {
155+
Core.injectedServices.$q.all(promises).then(success2 => {
148156
// just for precaution, we not rewrite server data
149157
if (collection.$source !== 'new') {
150-
return ;
158+
throw '';
151159
}
152160
success.page ? collection.page = success.page : null;
153161
for (let key in temporalcollection) {
154162
let resource: IResource = temporalcollection[key];
155163
collection.$source = 'store'; // collection and resources from storeservice
164+
collection.$cache_last_update = success._lastupdate_time;
156165
collection[resource.id] = resource; // collection from storeservice, resources from memory
157166
}
167+
job_deferred.resolve(collection);
158168
});
169+
} catch (e) {
170+
job_deferred.reject();
159171
}
172+
},
173+
error => {
174+
job_deferred.reject();
160175
});
161176
}
162177

src/library/sources/store.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class StoreService {
3434
// recorremos cada item y vemos si es tiempo de removerlo
3535
this.allstore.getItem(key).then(success2 => {
3636
// es tiempo de removerlo?
37-
if (Date.now() >= (success2._lastupdate_time + 12 * 3600 * 1000)) {
37+
if (Date.now() >= (success2._lastupdate_time + 24 * 3600 * 1000)) {
3838
// removemos!!
3939
this.allstore.removeItem(key);
4040
}
@@ -43,11 +43,11 @@ export class StoreService {
4343
});
4444
}
4545

46-
public getObjet(key: string): any /* Promise<void> */ {
46+
public getObjet(key: string): Promise<object> {
4747
return this.allstore.getItem('jsonapi.' + key);
4848
}
4949

50-
public getObjets(keys: Array<string>): any /* Promise<void> */ {
50+
public getObjets(keys: Array<string>): Promise<object> {
5151
return this.allstore.getItem('jsonapi.' + keys[0]);
5252
}
5353

0 commit comments

Comments
 (0)