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

Commit 92e16a9

Browse files
committed
store wait all promises :)
1 parent b7b3198 commit 92e16a9

5 files changed

Lines changed: 58 additions & 23 deletions

File tree

conf/browsersync-dist.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ module.exports = function () {
99
},
1010
open: false
1111
};
12-
};
12+
};

src/library/service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ export class Service extends ParentResourceService implements IService {
230230
this.runFc(fc_success, success);
231231
},
232232
error => {
233-
tempororay_collection.$source = 'server';
233+
// do not replace $source, because localstorage don't write if = server
234+
// tempororay_collection.$source = 'server';
234235
tempororay_collection.$is_loading = false;
235236
this.runFc(fc_error, error);
236237
}

src/library/services/cachememory.ts

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export class CacheMemory implements ICache {
5555
resource.id = id;
5656

5757
if (id && use_store) {
58-
console.log('pido al cachestore');
5958
Converter.getService(type).cachememory.getResourceFromStore(resource);
6059
}
6160

@@ -96,16 +95,19 @@ export class CacheMemory implements ICache {
9695

9796
// -------- STORE ---------------------------------
9897

99-
public getResourceFromStore(resource: IResource): void {
100-
let promise = Core.injectedServices.JsonapiCacheStore.getObjet(resource.type + '.' + resource.id);
101-
promise.then (
102-
success => {
103-
if (success) {
104-
console.log('recibí del cachestore, actualizo');
105-
Converter.build({ data: success }, resource);
106-
}
98+
public getResourceFromStore(resource: IResource): Promise<any> {
99+
let promise = this.fetchResourceFromStore(resource);
100+
promise.then (success => {
101+
if (success) {
102+
Converter.build({ data: success }, resource);
103+
console.log('recibí resource del cachestore, actualizo', resource);
107104
}
108-
);
105+
});
106+
return promise;
107+
}
108+
109+
private fetchResourceFromStore(resource: IResource): Promise<any> {
110+
return Core.injectedServices.JsonapiCacheStore.getObjet(resource.type + '.' + resource.id);
109111
}
110112

111113
private saveResourceStore(resource: IResource) {
@@ -117,16 +119,49 @@ export class CacheMemory implements ICache {
117119

118120
private getCollectionFromStore(url:string, collection: ICollection): void {
119121
let promise = Core.injectedServices.JsonapiCacheStore.getObjet('collection.' + url);
120-
promise.then (
121-
success => {
122-
if (success) {
123-
collection.$source = 'cachestore';
124-
angular.forEach(success.data, (dataresource: IDataResource) => {
125-
collection[dataresource.id] = this.getOrCreateResource(dataresource.type, dataresource.id, true);
126-
});
122+
promise.then(success => {
123+
if (success) {
124+
let all_ok = true;
125+
for (let key in success.data) {
126+
let dataresource: IDataResource = success.data[key];
127+
let resource = this.getOrCreateResource(dataresource.type, dataresource.id);
128+
if (resource.is_new) {
129+
all_ok = false;
130+
break;
131+
}
132+
collection[dataresource.id] = resource;
133+
}
134+
135+
// collection full with resources
136+
if (all_ok) {
137+
collection.$source = 'cachestore'; // collection from cachestore, resources from memory
138+
return;
139+
}
140+
141+
let temporalcollection = {};
142+
let promises = [];
143+
for (let key in success.data) {
144+
let dataresource: IDataResource = success.data[key];
145+
temporalcollection[dataresource.id] = this.getOrCreateResource(dataresource.type, dataresource.id);
146+
promises.push(
147+
this.getResourceFromStore(temporalcollection[dataresource.id])
148+
);
127149
}
150+
151+
// we have all resources from store
152+
Core.injectedServices.$q.all(promises).then(success => {
153+
// just for precaution, we not rewrite server data
154+
if (collection.$source !== 'new') {
155+
return ;
156+
}
157+
for (let key in temporalcollection) {
158+
let resource: IResource = temporalcollection[key];
159+
collection.$source = 'cachestore'; // collection and resources from cachestore
160+
collection[resource.id] = resource; // collection from cachestore, resources from memory
161+
}
162+
});
128163
}
129-
);
164+
});
130165
}
131166

132167
private saveCollectionStore(url: string, collection: ICollection) {

src/library/services/cachestore.service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ export class CacheStore {
66

77
/** @ngInject */
88
public constructor(
9-
protected $localForage,
10-
protected $q
9+
protected $localForage
1110
) {
1211
this.globalstore = $localForage.createInstance({ name: 'jsonapiglobal' });
1312
this.allstore = $localForage.createInstance({ name: 'allstore' });

tslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"curly": true,
1313
"eofline": true,
14-
"forin": true,
14+
"forin": false,
1515
"indent": [true, 2],
1616
"interface-name": true,
1717
"label-position": true,

0 commit comments

Comments
 (0)