@@ -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 ) {
0 commit comments