-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
464 lines (464 loc) · 15.8 KB
/
service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
import { map } from 'rxjs/operators';
import { Core } from './core';
import { Base } from './services/base';
import { Resource } from './resource';
import { PathBuilder } from './services/path-builder';
import { Converter } from './services/converter';
import { CacheMemory } from './services/cachememory';
import { DocumentCollection } from './document-collection';
import { isLive, relationshipsAreBuilded } from './common';
import { BehaviorSubject, Subject } from 'rxjs';
import { PathCollectionBuilder } from './services/path-collection-builder';
import { JsonRipper } from './services/json-ripper';
import { ClonedResource } from './cloned-resource';
// unsupported: template constraints.
/**
* @template R
*/
export class Service {
constructor() {
this.resource = Resource;
setTimeout(() => this.register());
}
/**
* @return {?}
*/
register() {
if (Core.me === null) {
throw new Error('Error: you are trying register `' + this.type + '` before inject JsonapiCore somewhere, almost one time.');
}
return Core.me.registerService(this);
}
/**
* @deprecated since 2.2.0. Use new() method.
* @return {?}
*/
newResource() {
return this.new();
}
/**
* @return {?}
*/
newCollection() {
return new DocumentCollection();
}
/**
* @return {?}
*/
new() {
/** @type {?} */
let resource = new this.resource();
resource.type = this.type;
// issue #36: just if service is not registered yet.
this.getService();
resource.reset();
return /** @type {?} */ (resource);
}
/**
* @return {?}
*/
getPrePath() {
return '';
}
/**
* @return {?}
*/
getPath() {
return this.path || this.type;
}
/**
* @param {?} id
* @param {?=} params
* @return {?}
*/
getClone(id, params = {}) {
return this.get(id, params).pipe(map((resource) => {
// return resource.clone();
return new ClonedResource(resource);
}));
}
/**
* @param {?} id
* @param {?=} params
* @return {?}
*/
pathForGet(id, params = {}) {
params = Object.assign({}, Base.ParamsResource, params);
/** @type {?} */
let path = new PathBuilder();
path.applyParams(this, params);
path.appendPath(id);
return path.get();
}
/**
* @param {?} id
* @param {?=} params
* @return {?}
*/
get(id, params = {}) {
params = Object.assign({}, Base.ParamsResource, params);
/** @type {?} */
let path = new PathBuilder();
path.applyParams(this, params);
path.appendPath(id);
/** @type {?} */
let resource = this.getOrCreateResource(id);
resource.setLoaded(false);
/** @type {?} */
let subject = new BehaviorSubject(resource);
if (Object.keys(params.fields || []).length > 0) {
// memory/store cache doesnt support fields
this.getGetFromServer(path, resource, subject);
}
else if (isLive(resource, params.ttl) && relationshipsAreBuilded(resource, params.include || [])) {
// data on memory and its live
resource.setLoaded(true);
setTimeout(() => subject.complete(), 0);
}
else if (resource.cache_last_update === 0) {
// we dont have any data on memory
this.getGetFromLocal(params, path, resource)
.then(() => {
subject.next(resource);
setTimeout(() => subject.complete(), 0);
})
.catch(() => {
resource.setLoaded(false);
this.getGetFromServer(path, resource, subject);
});
}
else {
this.getGetFromServer(path, resource, subject);
}
return subject.asObservable();
}
/**
* @param {?=} params
* @param {?=} path
* @param {?=} resource
* @return {?}
*/
getGetFromLocal(params = {}, path, resource) {
return __awaiter(this, void 0, void 0, function* () {
// STORE
if (!Core.injectedServices.rsJsonapiConfig.cachestore_support) {
throw new Error('We cant handle this request');
}
resource.setLoaded(false);
/** @type {?} */
let json_ripper = new JsonRipper();
/** @type {?} */
let success = yield json_ripper.getResource(JsonRipper.getResourceKey(resource), path.includes);
resource.fill(success);
resource.setSource('store');
// when fields is set, get resource form server
if (isLive(resource, params.ttl)) {
resource.setLoadedAndPropagate(true);
// resource.setBuildedAndPropagate(true);
return;
}
throw new Error('Resource is dead!');
});
}
/**
* @param {?} path
* @param {?} resource
* @param {?} subject
* @return {?}
*/
getGetFromServer(path, resource, subject) {
Core.get(path.get()).subscribe(success => {
resource.fill(/** @type {?} */ (success));
resource.cache_last_update = Date.now();
resource.setLoadedAndPropagate(true);
resource.setSourceAndPropagate('server');
// this.getService().cachememory.setResource(resource, true);
if (Core.injectedServices.rsJsonapiConfig.cachestore_support) {
/** @type {?} */
let json_ripper = new JsonRipper();
json_ripper.saveResource(resource, path.includes);
}
subject.next(resource);
setTimeout(() => subject.complete(), 0);
}, error => {
resource.setLoadedAndPropagate(true);
subject.next(resource);
subject.error(error);
});
}
/**
* @template T
* @return {?}
*/
getService() {
return /** @type {?} */ ((Converter.getService(this.type) || this.register()));
}
/**
* @param {?} path
* @return {?}
*/
getOrCreateCollection(path) {
/** @type {?} */
const service = this.getService();
/** @type {?} */
const collection = /** @type {?} */ (CacheMemory.getInstance().getOrCreateCollection(path.getForCache()));
collection.ttl = service.collections_ttl;
if (collection.source !== 'new') {
collection.source = 'memory';
}
return collection;
}
/**
* @param {?} id
* @return {?}
*/
getOrCreateResource(id) {
/** @type {?} */
let service = this.getService();
/** @type {?} */
let resource;
resource = /** @type {?} */ (CacheMemory.getInstance().getResource(this.type, id));
if (resource === null) {
resource = /** @type {?} */ (service.new());
resource.id = id;
CacheMemory.getInstance().setResource(resource, false);
}
if (resource.source !== 'new') {
resource.source = 'memory';
}
return resource;
}
/**
* @param {?} id
* @return {?}
*/
createResource(id) {
/** @type {?} */
let service = Converter.getServiceOrFail(this.type);
/** @type {?} */
let resource = service.new();
resource.id = id;
CacheMemory.getInstance().setResource(resource, false);
return /** @type {?} */ (resource);
}
/**
* deprecated since 2.2
* @return {?}
*/
clearCacheMemory() {
return __awaiter(this, void 0, void 0, function* () {
return this.clearCache();
});
}
/**
* @return {?}
*/
clearCache() {
return __awaiter(this, void 0, void 0, function* () {
/** @type {?} */
let path = new PathBuilder();
path.applyParams(this);
// @todo this code is repeated on core.clearCache()
CacheMemory.getInstance().deprecateCollections(path.getForCache());
/** @type {?} */
let json_ripper = new JsonRipper();
return json_ripper.deprecateCollection(path.getForCache()).then(() => true);
});
}
/**
* @param {?} attributes
* @return {?}
*/
parseToServer(attributes) {
/* */
}
/**
* @param {?} attributes
* @return {?}
*/
parseFromServer(attributes) {
/* */
}
/**
* @param {?} id
* @param {?=} params
* @return {?}
*/
delete(id, params) {
params = Object.assign({}, Base.ParamsResource, params);
/** @type {?} */
let path = new PathBuilder();
path.applyParams(this, params);
path.appendPath(id);
/** @type {?} */
let subject = new Subject();
Core.delete(path.get()).subscribe(success => {
CacheMemory.getInstance().removeResource(this.type, id);
subject.next();
subject.complete();
}, error => {
subject.error(error);
});
return subject.asObservable();
}
/**
* @param {?=} params
* @return {?}
*/
pathForAll(params = {}) {
/** @type {?} */
let builded_params = Object.assign({}, Base.ParamsCollection, params);
/** @type {?} */
let path = new PathCollectionBuilder();
path.applyParams(this, builded_params);
return path.get();
}
/**
* @param {?=} params
* @return {?}
*/
all(params = {}) {
/** @type {?} */
let builded_params = Object.assign({}, Base.ParamsCollection, params);
if (!builded_params.ttl && builded_params.ttl !== 0) {
builded_params.ttl = this.collections_ttl;
}
/** @type {?} */
let path = new PathCollectionBuilder();
path.applyParams(this, builded_params);
/** @type {?} */
let temporary_collection = this.getOrCreateCollection(path);
temporary_collection.page.number = builded_params.page.number * 1;
/** @type {?} */
let subject = new BehaviorSubject(temporary_collection);
if (Object.keys(builded_params.fields).length > 0) {
// memory/store cache dont suppont fields
this.getAllFromServer(path, builded_params, temporary_collection, subject);
}
else if (isLive(temporary_collection, builded_params.ttl)) {
// data on memory and its live
setTimeout(() => subject.complete(), 0);
}
else if (temporary_collection.cache_last_update === 0) {
// we dont have any data on memory
temporary_collection.source = 'new';
this.getAllFromLocal(builded_params, path, temporary_collection)
.then(() => {
subject.next(temporary_collection);
setTimeout(() => {
subject.complete();
}, 0);
})
.catch(() => {
temporary_collection.setLoaded(false);
this.getAllFromServer(path, builded_params, temporary_collection, subject);
});
}
else {
this.getAllFromServer(path, builded_params, temporary_collection, subject);
}
return subject.asObservable();
}
/**
* @param {?=} params
* @param {?=} path
* @param {?=} temporary_collection
* @return {?}
*/
getAllFromLocal(params = {}, path, temporary_collection) {
return __awaiter(this, void 0, void 0, function* () {
// STORE
if (!Core.injectedServices.rsJsonapiConfig.cachestore_support) {
throw new Error('We cant handle this request');
}
temporary_collection.setLoaded(false);
/** @type {?} */
let success;
if (params.store_cache_method === 'compact') {
// STORE (compact)
success = yield Core.injectedServices.JsonapiStoreService.getDataObject('collection', path.getForCache() + '.compact');
}
else {
/** @type {?} */
let json_ripper = new JsonRipper();
success = yield json_ripper.getCollection(path.getForCache(), path.includes);
}
temporary_collection.fill(success);
temporary_collection.setSourceAndPropagate('store');
// when fields is set, get resource form server
if (isLive(temporary_collection, params.ttl)) {
temporary_collection.setLoadedAndPropagate(true);
temporary_collection.setBuildedAndPropagate(true);
return;
}
throw new Error('Collection is dead!');
});
}
/**
* @param {?} path
* @param {?} params
* @param {?} temporary_collection
* @param {?} subject
* @return {?}
*/
getAllFromServer(path, params, temporary_collection, subject) {
temporary_collection.setLoaded(false);
Core.get(path.get()).subscribe(success => {
// this create a new ID for every resource (for caching proposes)
// for example, two URL return same objects but with different attributes
// tslint:disable-next-line:deprecation
if (params.cachehash) {
for (const key in success.data) {
/** @type {?} */
let resource = success.data[key];
// tslint:disable-next-line:deprecation
resource.id = resource.id + params.cachehash;
}
}
temporary_collection.fill(/** @type {?} */ (success));
temporary_collection.cache_last_update = Date.now();
temporary_collection.setCacheLastUpdateAndPropagate();
temporary_collection.setSourceAndPropagate('server');
temporary_collection.setLoadedAndPropagate(true);
// this.getService().cachememory.setCollection(path.getForCache(), temporary_collection);
if (Core.injectedServices.rsJsonapiConfig.cachestore_support) {
/** @type {?} */
let json_ripper = new JsonRipper();
json_ripper.saveCollection(path.getForCache(), temporary_collection, path.includes);
}
if (Core.injectedServices.rsJsonapiConfig.cachestore_support && params.store_cache_method === 'compact') {
// @todo migrate to dexie
Core.injectedServices.JsonapiStoreService.saveCollection(path.getForCache() + '.compact', /** @type {?} */ ((success)));
}
subject.next(temporary_collection);
setTimeout(() => subject.complete(), 0);
}, error => {
temporary_collection.setLoadedAndPropagate(true);
subject.next(temporary_collection);
subject.error(error);
});
}
}
if (false) {
/** @type {?} */
Service.prototype.type;
/** @type {?} */
Service.prototype.resource;
/** @type {?} */
Service.prototype.collections_ttl;
/** @type {?} */
Service.prototype.path;
}
//# sourceMappingURL=service.js.map