Skip to content

Commit ce595c2

Browse files
committed
publish: fix (FileCache)
1 parent 86c0448 commit ce595c2

10 files changed

+2891
-99
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
ts-temp/
2+
ts-temp/
3+
test/tmp/

authorized_keys

-1
This file was deleted.

lib/memd.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ declare module 'memd/Cache' {
112112
clear(key?: string): void;
113113
clearAsync(key?: string): Promise<void>;
114114
destroy(): void;
115+
flushAsync(): Promise<void>;
115116
}
116117
}
117118

@@ -126,7 +127,7 @@ declare module 'memd/persistance/FsTransport' {
126127
isAsync: boolean;
127128
constructor(opts: IFsTransportOpts);
128129
restoreAsync(): Promise<any>;
129-
flushAsync(coll: ICacheEntryCollection): any;
130+
flushAsync(coll: ICacheEntryCollection): Promise<any>;
130131
}
131132
}
132133

lib/memd.js

+86-28
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,24 @@ var TransportWorker = /** @class */ (function () {
213213
TransportWorker.prototype.flushAsync = function (key, entry) {
214214
return __awaiter(this, void 0, void 0, function () {
215215
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) {
219234
return [2 /*return*/, this.flushRunner.run()];
220235
});
221236
});
@@ -255,20 +270,30 @@ var AsyncRunner = /** @class */ (function () {
255270
this.shouldRunNext = false;
256271
}
257272
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 () {
258292
var _this = this;
259-
if (this.isWaiting && !this.isBusy) {
260-
this.reset();
261-
return this.run();
293+
if (this.isWaiting) {
294+
clearTimeout(this.timeout);
262295
}
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;
270296
this.timeout = setTimeout(function () { return _this.runInner(); }, this.debounce);
271-
return this.dfr.promise;
272297
};
273298
AsyncRunner.prototype.reset = function () {
274299
clearTimeout(this.timeout);
@@ -286,24 +311,23 @@ var AsyncRunner = /** @class */ (function () {
286311
this.isBusy = true;
287312
_a.label = 1;
288313
case 1:
289-
_a.trys.push([1, 3, 4, 5]);
314+
_a.trys.push([1, 3, , 4]);
290315
return [4 /*yield*/, this.fn()];
291316
case 2:
292317
_a.sent();
293-
return [3 /*break*/, 5];
318+
return [3 /*break*/, 4];
294319
case 3:
295320
error_1 = _a.sent();
296321
console.error('Transport error', error_1);
297-
return [3 /*break*/, 5];
322+
return [3 /*break*/, 4];
298323
case 4:
299324
runNext = this.shouldRunNext;
300-
this.dfr.resolve();
325+
this.dfr.resolve(null);
301326
this.reset();
302327
if (runNext) {
303328
this.run();
304329
}
305-
return [7 /*endfinally*/];
306-
case 5: return [2 /*return*/];
330+
return [2 /*return*/];
307331
}
308332
});
309333
});
@@ -320,6 +344,11 @@ var Deferred = /** @class */ (function () {
320344
}
321345
return Deferred;
322346
}());
347+
function wait(ms) {
348+
return new Promise(function (resolve) {
349+
setTimeout(resolve, ms);
350+
});
351+
}
323352
;
324353

325354
function isObject(x) {
@@ -666,6 +695,19 @@ var Cache = /** @class */ (function () {
666695
Cache.prototype.onChanged = function (key) {
667696
this.clear(key);
668697
};
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+
};
669711
return Cache;
670712
}());
671713
exports.Cache = Cache;
@@ -1407,31 +1449,47 @@ Object.defineProperty(exports, "__esModule", { value: true });
14071449
var FsTransport = /** @class */ (function () {
14081450
function FsTransport(opts) {
14091451
this.opts = opts;
1410-
this.File = null;
1452+
this._file = null;
14111453
this.isAsync = true;
14121454
if (typeof process === 'undefined' || typeof process.exit !== 'function') {
14131455
throw new Error('NodeJS expected');
14141456
}
14151457
var r = require;
14161458
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 });
14181461
}
14191462
FsTransport.prototype.restoreAsync = function () {
14201463
return __awaiter(this, void 0, void 0, function () {
1464+
var str, error_1;
14211465
return __generator(this, function (_a) {
14221466
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()];
14241470
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*/];
14291477
}
14301478
});
14311479
});
14321480
};
14331481
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+
});
14351493
};
14361494
return FsTransport;
14371495
}());

0 commit comments

Comments
 (0)