@@ -8,12 +8,15 @@ import { UrlParamsBuilder } from './services/url-params-builder';
88import { Converter } from './services/converter' ;
99import { LocalFilter } from './services/localfilter' ;
1010import { CacheMemory } from './services/cachememory' ;
11+ import { CacheStore } from './services/cachestore' ;
1112
12- import { IService , ISchema , IResource , ICollection , IExecParams , ICache , IParamsCollection , IParamsResource } from './interfaces' ;
13+ import { IService , ISchema , IResource , ICollection , IExecParams , ICacheStore , ICacheMemory ,
14+ IParamsCollection , IParamsResource } from './interfaces' ;
1315
1416export class Service extends ParentResourceService implements IService {
1517 public schema : ISchema ;
16- public cachememory : ICache ;
18+ public cachememory : ICacheMemory ;
19+ public cachestore : ICacheStore ;
1720 public type : string ;
1821
1922 private path : string ; // without slashes
@@ -29,6 +32,7 @@ export class Service extends ParentResourceService implements IService {
2932 }
3033 // only when service is registered, not cloned object
3134 this . cachememory = new CacheMemory ( ) ;
35+ this . cachestore = new CacheStore ( ) ;
3236 this . schema = angular . extend ( { } , Base . Schema , this . schema ) ;
3337 return Core . me . _register ( this ) ;
3438 }
@@ -78,8 +82,8 @@ export class Service extends ParentResourceService implements IService {
7882 path . applyParams ( this , params ) ;
7983 path . appendPath ( id ) ;
8084
81- // cache
82- let resource = this . getService ( ) . cachememory . getOrCreateResource ( this . type , id , true ) ;
85+ // CACHEMEMORY
86+ let resource = this . getService ( ) . cachememory . getOrCreateResource ( this . type , id ) ;
8387 resource . is_loading = true ;
8488 // exit if ttl is not expired
8589 let temporal_ttl = params . ttl || 0 ; // this.schema.ttl
@@ -95,22 +99,39 @@ export class Service extends ParentResourceService implements IService {
9599 return resource ;
96100 }
97101
102+ // CACHESTORE
103+ this . getService ( ) . cachestore . getResource ( resource )
104+ . then (
105+ success => {
106+ if ( Base . isObjectLive ( temporal_ttl , resource . lastupdate ) ) {
107+ this . runFc ( fc_success , { data : success } ) ;
108+ } else {
109+ this . getGetFromServer ( path , fc_success , fc_error , resource ) ;
110+ }
111+ } ,
112+ error => {
113+ this . getGetFromServer ( path , fc_success , fc_error , resource ) ;
114+ }
115+ ) ;
116+
117+ return resource ;
118+ }
98119
120+ private getGetFromServer ( path , fc_success , fc_error , resource : IResource ) {
99121 Core . injectedServices . JsonapiHttp
100122 . get ( path . get ( ) )
101123 . then (
102124 success => {
103125 Converter . build ( success . data , resource ) ;
104126 resource . is_loading = false ;
105127 this . getService ( ) . cachememory . setResource ( resource ) ;
128+ this . getService ( ) . cachestore . setResource ( resource ) ;
106129 this . runFc ( fc_success , success ) ;
107130 } ,
108131 error => {
109132 this . runFc ( fc_error , error ) ;
110133 }
111134 ) ;
112-
113- return resource ;
114135 }
115136
116137 private _all ( params : IParamsCollection , fc_success , fc_error ) : ICollection {
@@ -136,6 +157,15 @@ export class Service extends ParentResourceService implements IService {
136157 // if we remove this, dont work the same .all on same time (ej: <component /><component /><component />)
137158 let tempororay_collection = this . getService ( ) . cachememory . getOrCreateCollection ( path . getForCache ( ) ) ;
138159
160+ // creamos otra colleción si luego será filtrada
161+ let localfilter = new LocalFilter ( params . localfilter ) ;
162+ let cached_collection : ICollection ;
163+ if ( params . localfilter && Object . keys ( params . localfilter ) . length > 0 ) {
164+ cached_collection = Base . newCollection ( ) ;
165+ } else {
166+ cached_collection = tempororay_collection ;
167+ }
168+
139169 // MEMORY_CACHE
140170 let temporal_ttl = params . ttl || this . schema . ttl ;
141171 if ( temporal_ttl >= 0 && this . getService ( ) . cachememory . isCollectionExist ( path . getForCache ( ) ) ) {
@@ -148,8 +178,7 @@ export class Service extends ParentResourceService implements IService {
148178 }
149179
150180 // fill collection and localfilter
151- let localfilter = new LocalFilter ( ) ;
152- tempororay_collection = localfilter . filterCollection ( tempororay_collection , params . localfilter ) ;
181+ localfilter . filterCollection ( tempororay_collection , cached_collection ) ;
153182
154183 // exit if ttl is not expired
155184 if ( this . getService ( ) . cachememory . isCollectionLive ( path . getForCache ( ) , temporal_ttl ) ) {
@@ -160,71 +189,36 @@ export class Service extends ParentResourceService implements IService {
160189 deferred . promise . then ( fc_success => {
161190 this . runFc ( fc_success , 'cachememory' ) ;
162191 } ) ;
163- return tempororay_collection ;
192+ } else {
193+ this . getAllFromServer ( path , params , fc_success , fc_error , tempororay_collection , cached_collection ) ;
164194 }
165195 } else {
166196 // STORE
167- this . getService ( ) . cachememory . getCollectionFromStorePromise ( path . getForCache ( ) , tempororay_collection )
197+ this . getService ( ) . cachestore . getCollectionFromStorePromise ( path . getForCache ( ) , tempororay_collection )
168198 . then (
169199 success => {
170200 tempororay_collection . $source = 'store' ;
171201 tempororay_collection . $is_loading = false ;
172202
173203 // localfilter getted data
174- let localfilter = new LocalFilter ( ) ;
175- tempororay_collection = localfilter . filterCollection ( tempororay_collection , params . localfilter ) ;
204+ localfilter . filterCollection ( tempororay_collection , cached_collection ) ;
176205
177- if ( temporal_ttl >= 0 && Date . now ( ) <= ( tempororay_collection . $cache_last_update + temporal_ttl * 1000 ) ) {
206+ if ( Base . isObjectLive ( temporal_ttl , tempororay_collection . $cache_last_update ) ) {
178207 this . runFc ( fc_success , { data : success } ) ;
179208 } else {
180- this . getAllFromHttpStorage ( path , params , fc_success , fc_error , tempororay_collection ) ;
209+ this . getAllFromServer ( path , params , fc_success , fc_error , tempororay_collection , cached_collection ) ;
181210 }
182211 } ,
183212 error => {
184- this . getAllFromHttpStorage ( path , params , fc_success , fc_error , tempororay_collection ) ;
213+ this . getAllFromServer ( path , params , fc_success , fc_error , tempororay_collection , cached_collection ) ;
185214 }
186215 ) ;
187216 }
188217
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
197- tempororay_collection [ '$is_loading' ] = true ;
198-
199- // STORAGE_CACHE
200- Core . injectedServices . JsonapiHttpStorage
201- . get ( path . getForCache ( ) , params . storage_ttl )
202- . then (
203- success => {
204- tempororay_collection . $source = 'httpstorage' ;
205- tempororay_collection . $is_loading = false ;
206- Converter . build ( success , tempororay_collection ) ;
207-
208- // localfilter getted data
209- let localfilter = new LocalFilter ( ) ;
210- tempororay_collection = localfilter . filterCollection ( tempororay_collection , params . localfilter ) ;
211-
212- this . runFc ( fc_success , { data : success } ) ;
213-
214- var deferred = Core . injectedServices . $q . defer ( ) ;
215- deferred . resolve ( fc_success ) ;
216- deferred . promise . then ( fc_success => {
217- this . runFc ( fc_success , 'httpstorage' ) ;
218- } ) ;
219- return tempororay_collection ;
220- } ,
221- error => {
222- this . getAllFromServer ( path , params , fc_success , fc_error , tempororay_collection ) ;
223- }
224- ) ;
218+ return cached_collection ;
225219 }
226220
227- private getAllFromServer ( path , params , fc_success , fc_error , tempororay_collection : ICollection ) {
221+ private getAllFromServer ( path , params , fc_success , fc_error , tempororay_collection : ICollection , cached_collection : ICollection ) {
228222 // SERVER REQUEST
229223 Core . injectedServices . JsonapiHttp
230224 . get ( path . get ( ) )
@@ -236,14 +230,11 @@ export class Service extends ParentResourceService implements IService {
236230 Converter . build ( success . data , tempororay_collection ) ;
237231
238232 this . getService ( ) . cachememory . setCollection ( path . getForCache ( ) , tempororay_collection ) ;
239-
240- if ( params . storage_ttl > 0 ) {
241- Core . injectedServices . JsonapiHttpStorage . save ( path . getForCache ( ) , success . data ) ;
242- }
233+ this . getService ( ) . cachestore . setCollection ( path . getForCache ( ) , tempororay_collection ) ;
243234
244235 // localfilter getted data
245- let localfilter = new LocalFilter ( ) ;
246- tempororay_collection = localfilter . filterCollection ( tempororay_collection , params . localfilter ) ;
236+ let localfilter = new LocalFilter ( params . localfilter ) ;
237+ localfilter . filterCollection ( tempororay_collection , cached_collection ) ;
247238
248239 // trying to define smartfiltertype
249240 if ( this . smartfiltertype === 'undefined' ) {
0 commit comments