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

Commit b141880

Browse files
committed
resource-converter -> converter. newResource and newCollection autocreate resource/collection
1 parent b42d23e commit b141880

6 files changed

Lines changed: 44 additions & 42 deletions

File tree

src/library/interfaces/cache.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ export interface ICache {
55

66
isCollectionExist(url: string): boolean;
77
isCollectionLive(url: string, ttl: number): boolean;
8-
getCollection(url: string, use_store?: boolean): ICollection;
8+
getOrCreateCollection(url: string, use_store?: boolean): ICollection;
99
setCollection(url: string, collection: ICollection): void;
1010
clearAllCollections(): boolean;
11+
1112
isResourceLive(id: string, ttl: number): boolean;
13+
getOrCreateResource(type: string, id: string, use_store?: boolean): IResource;
1214
getResource(id: string): IResource;
1315
getResourceFromStore(resource: IResource): void;
1416
setResource(resource: IResource): void;

src/library/resource.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Base } from './services/base';
55
import { ParentResourceService } from './parent-resource-service';
66
import { PathBuilder } from './services/path-builder';
77
// import { UrlParamsBuilder } from './services/url-params-builder';
8-
import { Converter } from './services/resource-converter';
8+
import { Converter } from './services/converter';
99
import { IDataObject } from './interfaces/data-object';
1010

1111
import { IService, IAttributes, IResource, ICollection, IExecParams, IParamsResource } from './interfaces';
@@ -125,12 +125,11 @@ export class Resource extends ParentResourceService implements IResource {
125125

126126
// http request
127127
let path = new PathBuilder();
128-
path.appendPath(this.getService().getPrePath());
129-
path.appendPath(this.getService().getPath());
128+
path.applyParams(this.getService(), params);
130129
this.id && path.appendPath(this.id);
131-
params.include ? path.setInclude(params.include) : null;
132130

133-
let resource = Converter.newResource(this.type, this.id);
131+
let resource = this.getService().memorycache.getOrCreateResource(this.type, this.id);
132+
// Converter.getOrCreateResource(this.type, this.id);
134133

135134
let promise = Core.injectedServices.JsonapiHttp.exec(
136135
path.get(), this.id ? 'PUT' : 'POST',
@@ -162,7 +161,7 @@ export class Resource extends ParentResourceService implements IResource {
162161
we request the service again, because server maybe are giving
163162
us another type of resource (getService(resource.type))
164163
*/
165-
let tempororay_collection = this.getService().memorycache.getCollection('justAnUpdate');
164+
let tempororay_collection = this.getService().memorycache.getOrCreateCollection('justAnUpdate');
166165
Converter.build(success.data, tempororay_collection);
167166
angular.forEach(tempororay_collection, (resource_value: IResource, key: string) => {
168167
let res = Converter.getService(resource_value.type).memorycache.resources[resource_value.id];

src/library/service.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Resource } from './resource';
66
import { ParentResourceService } from './parent-resource-service';
77
import { PathBuilder } from './services/path-builder';
88
import { UrlParamsBuilder } from './services/url-params-builder';
9-
import { Converter } from './services/resource-converter';
9+
import { Converter } from './services/converter';
1010
import { LocalFilter } from './services/localfilter';
1111
import { MemoryCache } from './services/memorycache';
1212

@@ -76,13 +76,11 @@ export class Service extends ParentResourceService implements IService {
7676
public _get(id: string, params: IParamsResource, fc_success, fc_error): IResource {
7777
// http request
7878
let path = new PathBuilder();
79-
path.appendPath(this.getPrePath());
80-
path.appendPath(this.getPath());
79+
path.applyParams(this, params);
8180
path.appendPath(id);
82-
params.include ? path.setInclude(params.include) : null;
8381

8482
// cache
85-
let resource = Converter.newResource(this.type, id, true);
83+
let resource = this.getService().memorycache.getOrCreateResource(this.type, id, true);
8684
resource.is_loading = true;
8785
// exit if ttl is not expired
8886
let temporal_ttl = params.ttl ? params.ttl : 0;
@@ -126,10 +124,7 @@ export class Service extends ParentResourceService implements IService {
126124
// http request
127125
let path = new PathBuilder();
128126
let paramsurl = new UrlParamsBuilder();
129-
path.appendPath(this.getPrePath());
130-
params.beforepath ? path.appendPath(params.beforepath) : null;
131-
path.appendPath(this.getPath());
132-
params.include ? path.setInclude(params.include) : null;
127+
path.applyParams(this, params);
133128
params.remotefilter ? path.addParam(paramsurl.toparams( { filter: params.remotefilter } )) : null;
134129
if (params.page) {
135130
params.page.number > 1 ? path.addParam(
@@ -140,7 +135,7 @@ export class Service extends ParentResourceService implements IService {
140135

141136
// make request
142137
// if we remove this, dont work the same .all on same time (ej: <component /><component /><component />)
143-
let tempororay_collection = this.getService().memorycache.getCollection(path.getForCache(), true);
138+
let tempororay_collection = this.getService().memorycache.getOrCreateCollection(path.getForCache(), true);
144139

145140
// MEMORY_CACHE
146141
let temporal_ttl = params.ttl ? params.ttl : this.schema.ttl;
@@ -246,8 +241,7 @@ export class Service extends ParentResourceService implements IService {
246241
private _delete(id: string, params, fc_success, fc_error): void {
247242
// http request
248243
let path = new PathBuilder();
249-
path.appendPath(this.getPrePath());
250-
path.appendPath(this.getPath());
244+
path.applyParams(this, params);
251245
path.appendPath(id);
252246

253247
Core.injectedServices.JsonapiHttp

src/library/services/resource-converter.ts renamed to src/library/services/converter.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,6 @@ export class Converter {
6363
return resource_service;
6464
}
6565

66-
static newResource(type: string, id: string, use_store = false): IResource {
67-
if (Converter.getService(type).memorycache && id in Converter.getService(type).memorycache.resources) {
68-
return Converter.getService(type).memorycache.getResource(id);
69-
} else {
70-
let resource = Converter.getService(type).new();
71-
resource.id = id;
72-
73-
if (id && use_store) {
74-
Converter.getService(type).memorycache.getResourceFromStore(resource);
75-
}
76-
77-
return resource;
78-
}
79-
}
80-
8166
/* return a resource type(resoruce_service) with data(data) */
8267
private static procreate(data: IDataResource): IResource {
8368
if (!('type' in data && 'id' in data)) {
@@ -88,7 +73,7 @@ export class Converter {
8873
if (data.id in Converter.getService(data.type).memorycache.resources) {
8974
resource = Converter.getService(data.type).memorycache.resources[data.id];
9075
} else {
91-
resource = Converter.newResource(data.type, data.id);
76+
resource = Converter.getService(data.type).memorycache.getOrCreateResource(data.type, data.id);
9277
}
9378

9479
resource.attributes = data.attributes ? data.attributes : {};
@@ -129,7 +114,8 @@ export class Converter {
129114
let new_ids = {};
130115
for (let dataresource of collection_data_from.data) {
131116
if (!(dataresource.id in collection_dest)) {
132-
collection_dest[dataresource.id] = Converter.newResource(dataresource.type, dataresource.id);
117+
collection_dest[dataresource.id] =
118+
Converter.getService(dataresource.type).memorycache.getOrCreateResource(dataresource.type, dataresource.id);
133119
}
134120
Converter._buildResource(dataresource, collection_dest[dataresource.id], included_resources);
135121
new_ids[dataresource.id] = dataresource.id;

src/library/services/memorycache.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IDataResource } from '../interfaces/data-resource';
44
import { ICache } from '../interfaces/cache.d';
55
import { Core } from '../core';
66
import { Base } from './base';
7-
import { Converter } from './resource-converter';
7+
import { Converter } from './converter';
88
import { ResourceFunctions } from './resource-functions';
99

1010
export class MemoryCache implements ICache {
@@ -24,7 +24,7 @@ export class MemoryCache implements ICache {
2424
return this.resources[id] && (Date.now() <= (this.resources[id].lastupdate + ttl * 1000));
2525
}
2626

27-
public getCollection(url: string, use_store = false): ICollection {
27+
public getOrCreateCollection(url: string, use_store = false): ICollection {
2828
if (!(url in this.collections)) {
2929
this.collections[url] = Base.newCollection();
3030
this.collections[url].$source = 'new';
@@ -47,6 +47,22 @@ export class MemoryCache implements ICache {
4747
this.collections_lastupdate[url] = Date.now();
4848
}
4949

50+
public getOrCreateResource(type: string, id: string, use_store = false): IResource {
51+
if (Converter.getService(type).memorycache && id in Converter.getService(type).memorycache.resources) {
52+
return Converter.getService(type).memorycache.getResource(id);
53+
} else {
54+
let resource = Converter.getService(type).new();
55+
resource.id = id;
56+
57+
if (id && use_store) {
58+
Converter.getService(type).memorycache.getResourceFromStore(resource);
59+
}
60+
61+
return resource;
62+
}
63+
}
64+
65+
/* @deprecated */
5066
public getResource(id: string): IResource {
5167
return this.resources[id];
5268
}
@@ -104,7 +120,7 @@ export class MemoryCache implements ICache {
104120
if (success) {
105121
collection.$source = 'store';
106122
angular.forEach(success.data, (dataresource: IDataResource) => {
107-
collection[dataresource.id] = Converter.newResource(dataresource.type, dataresource.id, true);
123+
collection[dataresource.id] = this.getOrCreateResource(dataresource.type, dataresource.id, true);
108124
});
109125
}
110126
}

src/library/services/path-builder.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import * as angular from 'angular';
2+
import { IService, IParamsCollection, IParamsResource } from '../interfaces';
3+
24
export class PathBuilder {
35
public paths: Array<string> = [];
46
public includes: Array<string> = [];
57
private get_params: Array<string> = [];
68

7-
public prependPath(value: string) {
8-
this.paths.unshift(value);
9+
public applyParams(service: IService, params: IParamsResource | IParamsCollection) {
10+
this.appendPath(service.getPrePath());
11+
params.beforepath ? this.appendPath(params.beforepath) : null;
12+
this.appendPath(service.getPath());
13+
params.include ? this.setInclude(params.include) : null;
914
}
1015

1116
public appendPath(value: string) {
@@ -18,7 +23,7 @@ export class PathBuilder {
1823
this.get_params.push(param);
1924
}
2025

21-
public setInclude(strings_array: Array<string>) {
26+
private setInclude(strings_array: Array<string>) {
2227
this.includes = strings_array;
2328
}
2429

0 commit comments

Comments
 (0)