@@ -10,7 +10,6 @@ import {
10
10
import { DEFAULT_SETTINGS , Settings } from '../2-entities/Settings' ;
11
11
import { DEFAULT_SHORTCUTS , Shortcuts } from '../2-entities/Shortcuts' ;
12
12
import { datestr , deserialize , serialize } from '../util/serialization' ;
13
- import { fetchValue } from './helpers/fetchValue' ;
14
13
import { RemoteJson } from './helpers/RemoteJson' ;
15
14
import { RemoteValue } from './helpers/RemoteValue' ;
16
15
import { MixedStore } from './middleware/MixedStore' ;
@@ -48,7 +47,7 @@ export class NotesStorage {
48
47
async all ( ) {
49
48
const pattern = getNotesPath ( ) ;
50
49
51
- return fetchValue (
50
+ return fetchAndUpdate (
52
51
this . store . readAllLocal ( pattern ) ,
53
52
this . store . readAllRemote ( pattern ) ,
54
53
x => Boolean ( Object . keys ( x ) . length ) ,
@@ -123,6 +122,7 @@ export class NotesStorage {
123
122
) ;
124
123
125
124
this . notes . set ( id , remote ) ;
125
+ remote . push ( note ) ;
126
126
return remote ;
127
127
}
128
128
}
@@ -139,3 +139,28 @@ function createNote(content: NoteContent): Note {
139
139
modified : datestr ( ) ,
140
140
} ;
141
141
}
142
+
143
+ function fetchAndUpdate < T > (
144
+ local : Promise < T > ,
145
+ remote : Promise < T > ,
146
+ isValid : ( x : T ) => boolean ,
147
+ patch : ( x : T ) => void ,
148
+ ) {
149
+ remote . then ( x =>
150
+ console . log ( 'Notes fetched from remote' , Object . keys ( x ) . length ) ,
151
+ ) ;
152
+
153
+ return local . then (
154
+ cached => {
155
+ console . log ( 'Notes cached in locally' , Object . keys ( cached ) . length ) ;
156
+
157
+ if ( ! isValid ( cached ) ) {
158
+ return remote ;
159
+ }
160
+
161
+ remote . then ( patch ) ;
162
+ return cached ;
163
+ } ,
164
+ ( ) => remote ,
165
+ ) ;
166
+ }
0 commit comments