@bringg/dashboard-sdk 9.91.0 → 9.91.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,27 @@
1
+ interface Identifiable {
2
+ id: number;
3
+ }
4
+ interface LoadManyOptions {
5
+ useCache?: boolean;
6
+ }
7
+ type LoadMany<TItem extends Identifiable> = (ids: number[], options?: LoadManyOptions) => Promise<TItem[]>;
8
+ interface NegativeCacheOptions<TInstance> {
9
+ ttlMs: number;
10
+ hasItem: (instance: TInstance, id: number) => boolean;
11
+ maxSize?: number;
12
+ }
13
+ export declare const DEFAULT_NEGATIVE_CACHE_MAX_SIZE = 5000;
14
+ /**
15
+ * Suppresses ids the server repeatedly fails to return.
16
+ *
17
+ * Decorates a `loadMany(ids, options?)` method: ids the previous response omitted are dropped from the
18
+ * next request until `ttlMs` elapses, so an id the backend cannot return (soft-deleted, scoped to another
19
+ * team, external, other company) is requested once per TTL instead of on every call.
20
+ *
21
+ * TODO(TS 5.x): written as a legacy decorator (`experimentalDecorators`, TS 4.9 here - standard Stage 3
22
+ * decorators need TS 5.0+). Upgrade when the repo moves to TS 5.x: a Stage 3 method decorator takes
23
+ * `(originalMethod, context)` and returns the replacement function, so the `PropertyDescriptor`
24
+ * plumbing below goes away. `./cached-promise.ts` needs the same treatment at that point.
25
+ */
26
+ declare function withNegativeCache<TInstance extends object, TItem extends Identifiable>({ ttlMs, hasItem, maxSize }: NegativeCacheOptions<TInstance>): (target: TInstance, propertyName: string, propertyDescriptor: TypedPropertyDescriptor<LoadMany<TItem>>) => TypedPropertyDescriptor<LoadMany<TItem>>;
27
+ export default withNegativeCache;
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.DEFAULT_NEGATIVE_CACHE_MAX_SIZE = void 0;
40
+ exports.DEFAULT_NEGATIVE_CACHE_MAX_SIZE = 5000;
41
+ var instanceCaches = new WeakMap();
42
+ var getCache = function (instance) {
43
+ var cache = instanceCaches.get(instance);
44
+ if (!cache) {
45
+ cache = { misses: new Map(), lastSweptAt: Date.now() };
46
+ instanceCaches.set(instance, cache);
47
+ }
48
+ return cache;
49
+ };
50
+ // Entries are only ever added by the decorated method, so sweeping on call covers every growth path
51
+ // without a timer that would pin the instance and defeat the WeakMap. An entry recorded just after a
52
+ // sweep survives until the sweep after next, so worst-case lifetime is ~2x ttlMs.
53
+ var sweep = function (cache, ttlMs, now) {
54
+ if (now - cache.lastSweptAt < ttlMs) {
55
+ return;
56
+ }
57
+ var expired = [];
58
+ cache.misses.forEach(function (missedAt, id) {
59
+ if (now - missedAt >= ttlMs) {
60
+ expired.push(id);
61
+ }
62
+ });
63
+ expired.forEach(function (id) { return cache.misses.delete(id); });
64
+ cache.lastSweptAt = now;
65
+ };
66
+ // A burst of distinct misses inside one ttl window has nothing to sweep - drop oldest first.
67
+ // Map keeps insertion order and entries are inserted at miss time, so the front is the oldest.
68
+ // Runs AFTER recording, so a single call that overshoots the cap is trimmed by that same call.
69
+ var enforceCap = function (cache, maxSize) {
70
+ if (cache.misses.size <= maxSize) {
71
+ return;
72
+ }
73
+ var overflow = cache.misses.size - maxSize;
74
+ var oldestFirst = cache.misses.keys();
75
+ for (var i = 0; i < overflow; i++) {
76
+ cache.misses.delete(oldestFirst.next().value);
77
+ }
78
+ };
79
+ /**
80
+ * Suppresses ids the server repeatedly fails to return.
81
+ *
82
+ * Decorates a `loadMany(ids, options?)` method: ids the previous response omitted are dropped from the
83
+ * next request until `ttlMs` elapses, so an id the backend cannot return (soft-deleted, scoped to another
84
+ * team, external, other company) is requested once per TTL instead of on every call.
85
+ *
86
+ * TODO(TS 5.x): written as a legacy decorator (`experimentalDecorators`, TS 4.9 here - standard Stage 3
87
+ * decorators need TS 5.0+). Upgrade when the repo moves to TS 5.x: a Stage 3 method decorator takes
88
+ * `(originalMethod, context)` and returns the replacement function, so the `PropertyDescriptor`
89
+ * plumbing below goes away. `./cached-promise.ts` needs the same treatment at that point.
90
+ */
91
+ function withNegativeCache(_a) {
92
+ var ttlMs = _a.ttlMs, hasItem = _a.hasItem, _b = _a.maxSize, maxSize = _b === void 0 ? exports.DEFAULT_NEGATIVE_CACHE_MAX_SIZE : _b;
93
+ return function (target, propertyName, propertyDescriptor) {
94
+ var method = propertyDescriptor.value;
95
+ propertyDescriptor.value = function (ids, options) {
96
+ return __awaiter(this, void 0, void 0, function () {
97
+ var instanceCache, cache, idsToLoad, items, returnedIds, now;
98
+ var _this = this;
99
+ return __generator(this, function (_a) {
100
+ switch (_a.label) {
101
+ case 0:
102
+ // Nothing to load, and no instance to key the per-instance cache on when the method was
103
+ // detached from it (`const { loadMany } = api`).
104
+ if (!this || !(ids === null || ids === void 0 ? void 0 : ids.length)) {
105
+ return [2 /*return*/, method.call(this, ids, options)];
106
+ }
107
+ instanceCache = getCache(this);
108
+ cache = instanceCache.misses;
109
+ sweep(instanceCache, ttlMs, Date.now());
110
+ if ((options === null || options === void 0 ? void 0 : options.useCache) === false) {
111
+ ids.forEach(function (id) { return cache.delete(id); });
112
+ }
113
+ idsToLoad = ids.filter(function (id) {
114
+ var missedAt = cache.get(id);
115
+ if (missedAt === undefined) {
116
+ return true;
117
+ }
118
+ // Expired, or the item showed up by another route - either way it is requestable again.
119
+ if (Date.now() - missedAt >= ttlMs || hasItem(_this, id)) {
120
+ cache.delete(id);
121
+ return true;
122
+ }
123
+ return false;
124
+ });
125
+ if (!idsToLoad.length) {
126
+ return [2 /*return*/, []];
127
+ }
128
+ return [4 /*yield*/, method.call(this, idsToLoad, options)];
129
+ case 1:
130
+ items = _a.sent();
131
+ returnedIds = new Set(items.map(function (item) { return item.id; }));
132
+ now = Date.now();
133
+ idsToLoad.forEach(function (id) { return (returnedIds.has(id) ? cache.delete(id) : cache.set(id, now)); });
134
+ enforceCap(instanceCache, maxSize);
135
+ return [2 /*return*/, items];
136
+ }
137
+ });
138
+ });
139
+ };
140
+ return propertyDescriptor;
141
+ };
142
+ }
143
+ exports.default = withNegativeCache;
144
+ //# sourceMappingURL=negative-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"negative-cache.js","sourceRoot":"","sources":["../../../src/utils/decorators/negative-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBa,QAAA,+BAA+B,GAAG,IAAI,CAAC;AAOpD,IAAM,cAAc,GAAG,IAAI,OAAO,EAAyB,CAAC;AAE5D,IAAM,QAAQ,GAAG,UAAC,QAAgB;IACjC,IAAI,KAAK,GAAG,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAEzC,IAAI,CAAC,KAAK,EAAE;QACX,KAAK,GAAG,EAAE,MAAM,EAAE,IAAI,GAAG,EAAE,EAAE,WAAW,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;QACvD,cAAc,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;KACpC;IAED,OAAO,KAAK,CAAC;AACd,CAAC,CAAC;AAEF,oGAAoG;AACpG,qGAAqG;AACrG,kFAAkF;AAClF,IAAM,KAAK,GAAG,UAAC,KAAoB,EAAE,KAAa,EAAE,GAAW;IAC9D,IAAI,GAAG,GAAG,KAAK,CAAC,WAAW,GAAG,KAAK,EAAE;QACpC,OAAO;KACP;IAED,IAAM,OAAO,GAAa,EAAE,CAAC;IAE7B,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,UAAC,QAAQ,EAAE,EAAE;QACjC,IAAI,GAAG,GAAG,QAAQ,IAAI,KAAK,EAAE;YAC5B,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;SACjB;IACF,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,OAAO,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,OAAA,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,EAA7D,CAA6D,CAAC,CAAC;4BAEvF,UAAU,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;4BAEnC,sBAAO,KAAK,EAAC;;;;SACb,CAAC;QAEF,OAAO,kBAAkB,CAAC;IAC3B,CAAC,CAAC;AACH,CAAC;AAED,kBAAe,iBAAiB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bringg/dashboard-sdk",
3
- "version": "9.91.0",
3
+ "version": "9.91.1",
4
4
  "description": "Bringg dashboard SDK",
5
5
  "main": "dist/bringg-dashboard-sdk-cjs2.js",
6
6
  "typings": "dist/index.d.ts",