UNPKG

2.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var optimism_1 = require("optimism");
4var hasOwn = Object.prototype.hasOwnProperty;
5var DepTrackingCache = (function () {
6 function DepTrackingCache(data) {
7 var _this = this;
8 if (data === void 0) { data = Object.create(null); }
9 this.data = data;
10 this.depend = optimism_1.wrap(function (dataId) { return _this.data[dataId]; }, {
11 disposable: true,
12 makeCacheKey: function (dataId) {
13 return dataId;
14 },
15 });
16 }
17 DepTrackingCache.prototype.toObject = function () {
18 return this.data;
19 };
20 DepTrackingCache.prototype.get = function (dataId) {
21 this.depend(dataId);
22 return this.data[dataId];
23 };
24 DepTrackingCache.prototype.set = function (dataId, value) {
25 var oldValue = this.data[dataId];
26 if (value !== oldValue) {
27 this.data[dataId] = value;
28 this.depend.dirty(dataId);
29 }
30 };
31 DepTrackingCache.prototype.delete = function (dataId) {
32 if (hasOwn.call(this.data, dataId)) {
33 delete this.data[dataId];
34 this.depend.dirty(dataId);
35 }
36 };
37 DepTrackingCache.prototype.clear = function () {
38 this.replace(null);
39 };
40 DepTrackingCache.prototype.replace = function (newData) {
41 var _this = this;
42 if (newData) {
43 Object.keys(newData).forEach(function (dataId) {
44 _this.set(dataId, newData[dataId]);
45 });
46 Object.keys(this.data).forEach(function (dataId) {
47 if (!hasOwn.call(newData, dataId)) {
48 _this.delete(dataId);
49 }
50 });
51 }
52 else {
53 Object.keys(this.data).forEach(function (dataId) {
54 _this.delete(dataId);
55 });
56 }
57 };
58 return DepTrackingCache;
59}());
60exports.DepTrackingCache = DepTrackingCache;
61function defaultNormalizedCacheFactory(seed) {
62 return new DepTrackingCache(seed);
63}
64exports.defaultNormalizedCacheFactory = defaultNormalizedCacheFactory;
65//# sourceMappingURL=depTrackingCache.js.map
\No newline at end of file