@@ -213,9 +213,24 @@ var TransportWorker = /** @class */ (function () {
213
213
TransportWorker . prototype . flushAsync = function ( key , entry ) {
214
214
return __awaiter ( this , void 0 , void 0 , function ( ) {
215
215
return __generator ( this , function ( _a ) {
216
- this . isReady = true ;
217
- this . lastModified = new Date ( ) ;
218
- this . coll [ key ] = entry ;
216
+ switch ( _a . label ) {
217
+ case 0 :
218
+ if ( ! ( this . isReady === false ) ) return [ 3 /*break*/ , 2 ] ;
219
+ return [ 4 /*yield*/ , this . restoreAsync ( ) ] ;
220
+ case 1 :
221
+ _a . sent ( ) ;
222
+ _a . label = 2 ;
223
+ case 2 :
224
+ this . lastModified = new Date ( ) ;
225
+ this . coll [ key ] = entry ;
226
+ return [ 2 /*return*/ , this . flushRunner . run ( ) ] ;
227
+ }
228
+ } ) ;
229
+ } ) ;
230
+ } ;
231
+ TransportWorker . prototype . flushAsyncAll = function ( ) {
232
+ return __awaiter ( this , void 0 , void 0 , function ( ) {
233
+ return __generator ( this , function ( _a ) {
219
234
return [ 2 /*return*/ , this . flushRunner . run ( ) ] ;
220
235
} ) ;
221
236
} ) ;
@@ -255,20 +270,30 @@ var AsyncRunner = /** @class */ (function () {
255
270
this . shouldRunNext = false ;
256
271
}
257
272
AsyncRunner . prototype . run = function ( ) {
273
+ return __awaiter ( this , void 0 , Promise , function ( ) {
274
+ return __generator ( this , function ( _a ) {
275
+ if ( this . isWaiting && ! this . isBusy ) {
276
+ this . defer ( ) ;
277
+ return [ 2 /*return*/ , this . dfr . promise ] ;
278
+ }
279
+ if ( this . isBusy ) {
280
+ this . shouldRunNext = true ;
281
+ return [ 2 /*return*/ , this . dfr . promise ] ;
282
+ }
283
+ this . isWaiting = true ;
284
+ this . isBusy = false ;
285
+ this . dfr = new Deferred ;
286
+ this . defer ( ) ;
287
+ return [ 2 /*return*/ , this . dfr . promise ] ;
288
+ } ) ;
289
+ } ) ;
290
+ } ;
291
+ AsyncRunner . prototype . defer = function ( ) {
258
292
var _this = this ;
259
- if ( this . isWaiting && ! this . isBusy ) {
260
- this . reset ( ) ;
261
- return this . run ( ) ;
293
+ if ( this . isWaiting ) {
294
+ clearTimeout ( this . timeout ) ;
262
295
}
263
- if ( this . isBusy ) {
264
- this . shouldRunNext = true ;
265
- return this . dfr . promise ;
266
- }
267
- this . isWaiting = true ;
268
- this . isBusy = false ;
269
- this . dfr = new Deferred ;
270
296
this . timeout = setTimeout ( function ( ) { return _this . runInner ( ) ; } , this . debounce ) ;
271
- return this . dfr . promise ;
272
297
} ;
273
298
AsyncRunner . prototype . reset = function ( ) {
274
299
clearTimeout ( this . timeout ) ;
@@ -286,24 +311,23 @@ var AsyncRunner = /** @class */ (function () {
286
311
this . isBusy = true ;
287
312
_a . label = 1 ;
288
313
case 1 :
289
- _a . trys . push ( [ 1 , 3 , 4 , 5 ] ) ;
314
+ _a . trys . push ( [ 1 , 3 , , 4 ] ) ;
290
315
return [ 4 /*yield*/ , this . fn ( ) ] ;
291
316
case 2 :
292
317
_a . sent ( ) ;
293
- return [ 3 /*break*/ , 5 ] ;
318
+ return [ 3 /*break*/ , 4 ] ;
294
319
case 3 :
295
320
error_1 = _a . sent ( ) ;
296
321
console . error ( 'Transport error' , error_1 ) ;
297
- return [ 3 /*break*/ , 5 ] ;
322
+ return [ 3 /*break*/ , 4 ] ;
298
323
case 4 :
299
324
runNext = this . shouldRunNext ;
300
- this . dfr . resolve ( ) ;
325
+ this . dfr . resolve ( null ) ;
301
326
this . reset ( ) ;
302
327
if ( runNext ) {
303
328
this . run ( ) ;
304
329
}
305
- return [ 7 /*endfinally*/ ] ;
306
- case 5 : return [ 2 /*return*/ ] ;
330
+ return [ 2 /*return*/ ] ;
307
331
}
308
332
} ) ;
309
333
} ) ;
@@ -320,6 +344,11 @@ var Deferred = /** @class */ (function () {
320
344
}
321
345
return Deferred ;
322
346
} ( ) ) ;
347
+ function wait ( ms ) {
348
+ return new Promise ( function ( resolve ) {
349
+ setTimeout ( resolve , ms ) ;
350
+ } ) ;
351
+ }
323
352
;
324
353
325
354
function isObject ( x ) {
@@ -666,6 +695,19 @@ var Cache = /** @class */ (function () {
666
695
Cache . prototype . onChanged = function ( key ) {
667
696
this . clear ( key ) ;
668
697
} ;
698
+ Cache . prototype . flushAsync = function ( ) {
699
+ var _a ;
700
+ return __awaiter ( this , void 0 , void 0 , function ( ) {
701
+ return __generator ( this , function ( _b ) {
702
+ switch ( _b . label ) {
703
+ case 0 : return [ 4 /*yield*/ , ( ( _a = this . _transport ) === null || _a === void 0 ? void 0 : _a . flushAsyncAll ( ) ) ] ;
704
+ case 1 :
705
+ _b . sent ( ) ;
706
+ return [ 2 /*return*/ ] ;
707
+ }
708
+ } ) ;
709
+ } ) ;
710
+ } ;
669
711
return Cache ;
670
712
} ( ) ) ;
671
713
exports . Cache = Cache ;
@@ -1407,31 +1449,47 @@ Object.defineProperty(exports, "__esModule", { value: true });
1407
1449
var FsTransport = /** @class */ ( function ( ) {
1408
1450
function FsTransport ( opts ) {
1409
1451
this . opts = opts ;
1410
- this . File = null ;
1452
+ this . _file = null ;
1411
1453
this . isAsync = true ;
1412
1454
if ( typeof process === 'undefined' || typeof process . exit !== 'function' ) {
1413
1455
throw new Error ( 'NodeJS expected' ) ;
1414
1456
}
1415
1457
var r = require ;
1416
1458
var module = 'atma-io' ;
1417
- this . File = r ( module ) . File ;
1459
+ var FileSafe = r ( module ) . FileSafe ;
1460
+ this . _file = new FileSafe ( this . opts . path , { threadSafe : true } ) ;
1418
1461
}
1419
1462
FsTransport . prototype . restoreAsync = function ( ) {
1420
1463
return __awaiter ( this , void 0 , void 0 , function ( ) {
1464
+ var str , error_1 ;
1421
1465
return __generator ( this , function ( _a ) {
1422
1466
switch ( _a . label ) {
1423
- case 0 : return [ 4 /*yield*/ , this . File . existsAsync ( this . opts . path ) ] ;
1467
+ case 0 :
1468
+ _a . trys . push ( [ 0 , 2 , , 3 ] ) ;
1469
+ return [ 4 /*yield*/ , this . _file . readAsync ( ) ] ;
1424
1470
case 1 :
1425
- if ( ( _a . sent ( ) ) === false ) {
1426
- return [ 2 /*return*/ , { } ] ;
1427
- }
1428
- return [ 2 /*return*/ , this . File . readAsync ( this . opts . path ) ] ;
1471
+ str = _a . sent ( ) ;
1472
+ return [ 2 /*return*/ , JSON . parse ( str ) ] ;
1473
+ case 2 :
1474
+ error_1 = _a . sent ( ) ;
1475
+ return [ 2 /*return*/ , { } ] ;
1476
+ case 3 : return [ 2 /*return*/ ] ;
1429
1477
}
1430
1478
} ) ;
1431
1479
} ) ;
1432
1480
} ;
1433
1481
FsTransport . prototype . flushAsync = function ( coll ) {
1434
- return this . File . writeAsync ( this . opts . path , coll ) ;
1482
+ return __awaiter ( this , void 0 , void 0 , function ( ) {
1483
+ var json ;
1484
+ return __generator ( this , function ( _a ) {
1485
+ switch ( _a . label ) {
1486
+ case 0 :
1487
+ json = JSON . stringify ( coll ) ;
1488
+ return [ 4 /*yield*/ , this . _file . writeAsync ( json ) ] ;
1489
+ case 1 : return [ 2 /*return*/ , _a . sent ( ) ] ;
1490
+ }
1491
+ } ) ;
1492
+ } ) ;
1435
1493
} ;
1436
1494
return FsTransport ;
1437
1495
} ( ) ) ;
0 commit comments