@prostojs/mate 0.5.0 → 0.5.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.
- package/dist/index.cjs +24 -7
- package/dist/index.d.ts +12 -0
- package/dist/index.mjs +24 -7
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -23,7 +23,7 @@ function formatIdentity(identity) {
|
|
|
23
23
|
function warnOnDuplicateMate() {
|
|
24
24
|
const g = globalThis;
|
|
25
25
|
const identity = {};
|
|
26
|
-
identity.version = "0.
|
|
26
|
+
identity.version = "0.5.0";
|
|
27
27
|
const path = getModulePath();
|
|
28
28
|
if (path) identity.path = path;
|
|
29
29
|
const first = g[MATE_MODULE_IDENTITY_KEY];
|
|
@@ -33,11 +33,12 @@ function warnOnDuplicateMate() {
|
|
|
33
33
|
|
|
34
34
|
//#endregion
|
|
35
35
|
//#region src/utils/helpers.ts
|
|
36
|
+
const hasOwn = Object.prototype.hasOwnProperty;
|
|
36
37
|
function getConstructor(instance) {
|
|
37
38
|
return isConstructor(instance) ? instance : instance.constructor ? instance.constructor : Object.getPrototypeOf(instance).constructor;
|
|
38
39
|
}
|
|
39
40
|
function isConstructor(v) {
|
|
40
|
-
return typeof v === "function" &&
|
|
41
|
+
return typeof v === "function" && hasOwn.call(v, "prototype") && !hasOwn.call(v, "caller") && !!v.name;
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
//#endregion
|
|
@@ -108,9 +109,11 @@ var Mate = class {
|
|
|
108
109
|
_cleanup() {
|
|
109
110
|
Reflect._cleanup?.call(Reflect);
|
|
110
111
|
this._readCache = void 0;
|
|
112
|
+
this._readCacheNonCtor = void 0;
|
|
111
113
|
}
|
|
112
114
|
set(args, key, value, isArray) {
|
|
113
115
|
this._readCache = void 0;
|
|
116
|
+
this._readCacheNonCtor = void 0;
|
|
114
117
|
let level = "CLASS";
|
|
115
118
|
const newArgs = args.level === "CLASS" ? { target: getConstructor(args.target) } : args.level === "PROP" ? {
|
|
116
119
|
target: args.target,
|
|
@@ -156,11 +159,19 @@ var Mate = class {
|
|
|
156
159
|
Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
|
|
157
160
|
}
|
|
158
161
|
read(target, propKey) {
|
|
159
|
-
const constructor = isConstructor(target) ? target : getConstructor(target);
|
|
160
|
-
const proto = constructor.prototype;
|
|
161
162
|
const propKeyStr = propKey;
|
|
162
|
-
const
|
|
163
|
-
if (
|
|
163
|
+
const ctorCacheRoot = this._readCache;
|
|
164
|
+
if (ctorCacheRoot !== void 0 && typeof target === "function") {
|
|
165
|
+
const hit = readCached(ctorCacheRoot.get(target), propKeyStr);
|
|
166
|
+
if (hit !== MISS) return hit;
|
|
167
|
+
}
|
|
168
|
+
const isConstr = isConstructor(target);
|
|
169
|
+
const constructor = isConstr ? target : getConstructor(target);
|
|
170
|
+
if (!isConstr) {
|
|
171
|
+
const hit = readCached(ctorCacheRoot?.get(constructor) ?? this._readCacheNonCtor?.get(constructor), propKeyStr);
|
|
172
|
+
if (hit !== MISS) return hit;
|
|
173
|
+
}
|
|
174
|
+
const proto = constructor.prototype;
|
|
164
175
|
let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === "string" ? proto : constructor, propKey);
|
|
165
176
|
if (ownMeta && propKey === void 0 && ownMeta.params === void 0) {
|
|
166
177
|
const parent = Object.getPrototypeOf(constructor);
|
|
@@ -192,7 +203,7 @@ var Mate = class {
|
|
|
192
203
|
}
|
|
193
204
|
}
|
|
194
205
|
}
|
|
195
|
-
const cache = this._readCache ??= /* @__PURE__ */ new WeakMap();
|
|
206
|
+
const cache = isConstr || typeof constructor !== "function" || isConstructor(constructor) ? this._readCache ??= /* @__PURE__ */ new WeakMap() : this._readCacheNonCtor ??= /* @__PURE__ */ new WeakMap();
|
|
196
207
|
let tc = cache.get(constructor);
|
|
197
208
|
if (!tc) {
|
|
198
209
|
tc = /* @__PURE__ */ new Map();
|
|
@@ -229,6 +240,12 @@ var Mate = class {
|
|
|
229
240
|
}
|
|
230
241
|
};
|
|
231
242
|
const fnProto = Object.getPrototypeOf(Function);
|
|
243
|
+
const MISS = Symbol("mate.read.miss");
|
|
244
|
+
function readCached(tc, propKey) {
|
|
245
|
+
if (tc === void 0) return MISS;
|
|
246
|
+
const v = tc.get(propKey);
|
|
247
|
+
return v !== void 0 || tc.has(propKey) ? v : MISS;
|
|
248
|
+
}
|
|
232
249
|
|
|
233
250
|
//#endregion
|
|
234
251
|
//#region src/index.ts
|
package/dist/index.d.ts
CHANGED
|
@@ -46,7 +46,19 @@ declare class Mate<TClass extends TObject = TMateClassMeta<TMateParamMeta>, TPro
|
|
|
46
46
|
protected workspace: string;
|
|
47
47
|
protected options: TMateOptions<TClass, TProp>;
|
|
48
48
|
protected logger: TConsoleBase;
|
|
49
|
+
/**
|
|
50
|
+
* Read cache keyed by the resolved constructor. Invariant: every function
|
|
51
|
+
* key here passed `isConstructor` when stored, so a hit on a function
|
|
52
|
+
* `target` itself proves `target` is that constructor — `read` probes
|
|
53
|
+
* it before running `isConstructor`.
|
|
54
|
+
*/
|
|
49
55
|
protected _readCache?: WeakMap<object, Map<string | undefined, unknown>>;
|
|
56
|
+
/**
|
|
57
|
+
* Read cache for resolved "constructors" that are functions failing
|
|
58
|
+
* `isConstructor` (e.g. an anonymous class reached through an instance's
|
|
59
|
+
* `.constructor`). Kept apart so they cannot satisfy the fast probe.
|
|
60
|
+
*/
|
|
61
|
+
protected _readCacheNonCtor?: WeakMap<object, Map<string | undefined, unknown>>;
|
|
50
62
|
constructor(workspace: string, options?: TMateOptions<TClass, TProp>);
|
|
51
63
|
/**
|
|
52
64
|
* Cleanup function to reset metadata
|
package/dist/index.mjs
CHANGED
|
@@ -21,7 +21,7 @@ function formatIdentity(identity) {
|
|
|
21
21
|
function warnOnDuplicateMate() {
|
|
22
22
|
const g = globalThis;
|
|
23
23
|
const identity = {};
|
|
24
|
-
identity.version = "0.
|
|
24
|
+
identity.version = "0.5.0";
|
|
25
25
|
const path = getModulePath();
|
|
26
26
|
if (path) identity.path = path;
|
|
27
27
|
const first = g[MATE_MODULE_IDENTITY_KEY];
|
|
@@ -31,11 +31,12 @@ function warnOnDuplicateMate() {
|
|
|
31
31
|
|
|
32
32
|
//#endregion
|
|
33
33
|
//#region src/utils/helpers.ts
|
|
34
|
+
const hasOwn = Object.prototype.hasOwnProperty;
|
|
34
35
|
function getConstructor(instance) {
|
|
35
36
|
return isConstructor(instance) ? instance : instance.constructor ? instance.constructor : Object.getPrototypeOf(instance).constructor;
|
|
36
37
|
}
|
|
37
38
|
function isConstructor(v) {
|
|
38
|
-
return typeof v === "function" &&
|
|
39
|
+
return typeof v === "function" && hasOwn.call(v, "prototype") && !hasOwn.call(v, "caller") && !!v.name;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
//#endregion
|
|
@@ -106,9 +107,11 @@ var Mate = class {
|
|
|
106
107
|
_cleanup() {
|
|
107
108
|
Reflect._cleanup?.call(Reflect);
|
|
108
109
|
this._readCache = void 0;
|
|
110
|
+
this._readCacheNonCtor = void 0;
|
|
109
111
|
}
|
|
110
112
|
set(args, key, value, isArray) {
|
|
111
113
|
this._readCache = void 0;
|
|
114
|
+
this._readCacheNonCtor = void 0;
|
|
112
115
|
let level = "CLASS";
|
|
113
116
|
const newArgs = args.level === "CLASS" ? { target: getConstructor(args.target) } : args.level === "PROP" ? {
|
|
114
117
|
target: args.target,
|
|
@@ -154,11 +157,19 @@ var Mate = class {
|
|
|
154
157
|
Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
|
|
155
158
|
}
|
|
156
159
|
read(target, propKey) {
|
|
157
|
-
const constructor = isConstructor(target) ? target : getConstructor(target);
|
|
158
|
-
const proto = constructor.prototype;
|
|
159
160
|
const propKeyStr = propKey;
|
|
160
|
-
const
|
|
161
|
-
if (
|
|
161
|
+
const ctorCacheRoot = this._readCache;
|
|
162
|
+
if (ctorCacheRoot !== void 0 && typeof target === "function") {
|
|
163
|
+
const hit = readCached(ctorCacheRoot.get(target), propKeyStr);
|
|
164
|
+
if (hit !== MISS) return hit;
|
|
165
|
+
}
|
|
166
|
+
const isConstr = isConstructor(target);
|
|
167
|
+
const constructor = isConstr ? target : getConstructor(target);
|
|
168
|
+
if (!isConstr) {
|
|
169
|
+
const hit = readCached(ctorCacheRoot?.get(constructor) ?? this._readCacheNonCtor?.get(constructor), propKeyStr);
|
|
170
|
+
if (hit !== MISS) return hit;
|
|
171
|
+
}
|
|
172
|
+
const proto = constructor.prototype;
|
|
162
173
|
let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === "string" ? proto : constructor, propKey);
|
|
163
174
|
if (ownMeta && propKey === void 0 && ownMeta.params === void 0) {
|
|
164
175
|
const parent = Object.getPrototypeOf(constructor);
|
|
@@ -190,7 +201,7 @@ var Mate = class {
|
|
|
190
201
|
}
|
|
191
202
|
}
|
|
192
203
|
}
|
|
193
|
-
const cache = this._readCache ??= /* @__PURE__ */ new WeakMap();
|
|
204
|
+
const cache = isConstr || typeof constructor !== "function" || isConstructor(constructor) ? this._readCache ??= /* @__PURE__ */ new WeakMap() : this._readCacheNonCtor ??= /* @__PURE__ */ new WeakMap();
|
|
194
205
|
let tc = cache.get(constructor);
|
|
195
206
|
if (!tc) {
|
|
196
207
|
tc = /* @__PURE__ */ new Map();
|
|
@@ -227,6 +238,12 @@ var Mate = class {
|
|
|
227
238
|
}
|
|
228
239
|
};
|
|
229
240
|
const fnProto = Object.getPrototypeOf(Function);
|
|
241
|
+
const MISS = Symbol("mate.read.miss");
|
|
242
|
+
function readCached(tc, propKey) {
|
|
243
|
+
if (tc === void 0) return MISS;
|
|
244
|
+
const v = tc.get(propKey);
|
|
245
|
+
return v !== void 0 || tc.has(propKey) ? v : MISS;
|
|
246
|
+
}
|
|
230
247
|
|
|
231
248
|
//#endregion
|
|
232
249
|
//#region src/index.ts
|