jsii-reflect 1.91.0 → 1.93.0
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/lib/_memoized.d.ts +8 -0
- package/lib/_memoized.js +42 -19
- package/lib/class.d.ts +6 -0
- package/lib/class.js +22 -1
- package/lib/method.js +10 -0
- package/lib/type-system.d.ts +9 -0
- package/lib/type-system.js +16 -0
- package/lib/type.js +1 -1
- package/package.json +7 -7
- package/test/_memoized.test.d.ts +2 -0
- package/test/_memoized.test.js +77 -0
package/lib/_memoized.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TypeSystem } from './type-system';
|
|
1
2
|
/**
|
|
2
3
|
* Decorates property readers for readonly properties so that their results are
|
|
3
4
|
* memoized in a `WeakMap`-based cache. Those properties will consequently be
|
|
@@ -5,6 +6,13 @@
|
|
|
5
6
|
*
|
|
6
7
|
* This can only be applied to property accessors (`public get foo(): any`), and not to
|
|
7
8
|
* property declarations (`public readonly foo: any`).
|
|
9
|
+
*
|
|
10
|
+
* This should not be applied to any computations relying on a typesystem.
|
|
11
|
+
* The typesystem can be changed and thus change the result of the call.
|
|
12
|
+
* Use `memoizedWhenLocked` instead.
|
|
8
13
|
*/
|
|
9
14
|
export declare function memoized(_prototype: unknown, propertyKey: string, descriptor: PropertyDescriptor): void;
|
|
15
|
+
export declare function memoizedWhenLocked<T extends {
|
|
16
|
+
system: TypeSystem;
|
|
17
|
+
}>(_prototype: T, propertyKey: string, descriptor: PropertyDescriptor): void;
|
|
10
18
|
//# sourceMappingURL=_memoized.d.ts.map
|
package/lib/_memoized.js
CHANGED
|
@@ -1,25 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/ban-types -- WeakMap<T, _> demands T extends object */
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.memoized = void 0;
|
|
3
|
+
exports.memoizedWhenLocked = exports.memoized = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/ban-types -- WeakMap<T, _> demands T extends object */
|
|
5
5
|
const CACHE = new WeakMap();
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* memoized in a `WeakMap`-based cache. Those properties will consequently be
|
|
9
|
-
* computed exactly once.
|
|
10
|
-
*
|
|
11
|
-
* This can only be applied to property accessors (`public get foo(): any`), and not to
|
|
12
|
-
* property declarations (`public readonly foo: any`).
|
|
13
|
-
*/
|
|
14
|
-
function memoized(_prototype, propertyKey, descriptor) {
|
|
15
|
-
if (!descriptor.get) {
|
|
16
|
-
throw new Error(`@memoized can only be applied to property getters!`);
|
|
17
|
-
}
|
|
18
|
-
if (descriptor.set) {
|
|
19
|
-
throw new Error(`@memoized can only be applied to readonly properties!`);
|
|
20
|
-
}
|
|
21
|
-
const original = descriptor.get;
|
|
22
|
-
descriptor.get = function memoizedGet() {
|
|
6
|
+
function memoizedGet(original, propertyKey) {
|
|
7
|
+
return function () {
|
|
23
8
|
let cache = CACHE.get(this);
|
|
24
9
|
if (cache == null) {
|
|
25
10
|
cache = new Map();
|
|
@@ -39,5 +24,43 @@ function memoized(_prototype, propertyKey, descriptor) {
|
|
|
39
24
|
return result;
|
|
40
25
|
};
|
|
41
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Decorates property readers for readonly properties so that their results are
|
|
29
|
+
* memoized in a `WeakMap`-based cache. Those properties will consequently be
|
|
30
|
+
* computed exactly once.
|
|
31
|
+
*
|
|
32
|
+
* This can only be applied to property accessors (`public get foo(): any`), and not to
|
|
33
|
+
* property declarations (`public readonly foo: any`).
|
|
34
|
+
*
|
|
35
|
+
* This should not be applied to any computations relying on a typesystem.
|
|
36
|
+
* The typesystem can be changed and thus change the result of the call.
|
|
37
|
+
* Use `memoizedWhenLocked` instead.
|
|
38
|
+
*/
|
|
39
|
+
function memoized(_prototype, propertyKey, descriptor) {
|
|
40
|
+
if (!descriptor.get) {
|
|
41
|
+
throw new Error(`@memoized can only be applied to property getters!`);
|
|
42
|
+
}
|
|
43
|
+
if (descriptor.set) {
|
|
44
|
+
throw new Error(`@memoized can only be applied to readonly properties!`);
|
|
45
|
+
}
|
|
46
|
+
const original = descriptor.get;
|
|
47
|
+
descriptor.get = memoizedGet(original, propertyKey);
|
|
48
|
+
}
|
|
42
49
|
exports.memoized = memoized;
|
|
50
|
+
function memoizedWhenLocked(_prototype, propertyKey, descriptor) {
|
|
51
|
+
if (!descriptor.get) {
|
|
52
|
+
throw new Error(`@memoized can only be applied to property getters!`);
|
|
53
|
+
}
|
|
54
|
+
if (descriptor.set) {
|
|
55
|
+
throw new Error(`@memoized can only be applied to readonly properties!`);
|
|
56
|
+
}
|
|
57
|
+
const original = descriptor.get;
|
|
58
|
+
descriptor.get = function () {
|
|
59
|
+
if (this.system.isLocked) {
|
|
60
|
+
return memoizedGet(original, propertyKey).call(this);
|
|
61
|
+
}
|
|
62
|
+
return original.call(this);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
exports.memoizedWhenLocked = memoizedWhenLocked;
|
|
43
66
|
//# sourceMappingURL=_memoized.js.map
|
package/lib/class.d.ts
CHANGED
|
@@ -25,8 +25,14 @@ export declare class ClassType extends ReferenceType {
|
|
|
25
25
|
get abstract(): boolean;
|
|
26
26
|
/**
|
|
27
27
|
* Returns list of all base classes (first is the direct base and last is the top-most).
|
|
28
|
+
*
|
|
29
|
+
* @deprecated use ClassType.ancestors instead
|
|
28
30
|
*/
|
|
29
31
|
getAncestors(): ClassType[];
|
|
32
|
+
/**
|
|
33
|
+
* Returns list of all base classes (first is the direct base and last is the top-most).
|
|
34
|
+
*/
|
|
35
|
+
get ancestors(): ClassType[];
|
|
30
36
|
/**
|
|
31
37
|
* Lists all properties in this class.
|
|
32
38
|
* @param inherited include all properties inherited from base classes (default: false)
|
package/lib/class.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
2
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
9
|
exports.ClassType = void 0;
|
|
10
|
+
const _memoized_1 = require("./_memoized");
|
|
4
11
|
const initializer_1 = require("./initializer");
|
|
5
12
|
const method_1 = require("./method");
|
|
6
13
|
const property_1 = require("./property");
|
|
@@ -42,12 +49,20 @@ class ClassType extends reference_type_1.ReferenceType {
|
|
|
42
49
|
}
|
|
43
50
|
/**
|
|
44
51
|
* Returns list of all base classes (first is the direct base and last is the top-most).
|
|
52
|
+
*
|
|
53
|
+
* @deprecated use ClassType.ancestors instead
|
|
45
54
|
*/
|
|
46
55
|
getAncestors() {
|
|
56
|
+
return this.ancestors;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Returns list of all base classes (first is the direct base and last is the top-most).
|
|
60
|
+
*/
|
|
61
|
+
get ancestors() {
|
|
47
62
|
const out = new Array();
|
|
48
63
|
if (this.base) {
|
|
49
64
|
out.push(this.base);
|
|
50
|
-
out.push(...this.base.
|
|
65
|
+
out.push(...this.base.ancestors);
|
|
51
66
|
}
|
|
52
67
|
return out;
|
|
53
68
|
}
|
|
@@ -106,6 +121,12 @@ class ClassType extends reference_type_1.ReferenceType {
|
|
|
106
121
|
return result;
|
|
107
122
|
}
|
|
108
123
|
}
|
|
124
|
+
__decorate([
|
|
125
|
+
_memoized_1.memoizedWhenLocked
|
|
126
|
+
], ClassType.prototype, "base", null);
|
|
127
|
+
__decorate([
|
|
128
|
+
_memoized_1.memoizedWhenLocked
|
|
129
|
+
], ClassType.prototype, "ancestors", null);
|
|
109
130
|
exports.ClassType = ClassType;
|
|
110
131
|
function flatten(xs) {
|
|
111
132
|
return Array.prototype.concat([], ...xs);
|
package/lib/method.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
2
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
9
|
exports.Method = exports.INITIALIZER_NAME = void 0;
|
|
10
|
+
const _memoized_1 = require("./_memoized");
|
|
4
11
|
const callable_1 = require("./callable");
|
|
5
12
|
const optional_value_1 = require("./optional-value");
|
|
6
13
|
const type_member_1 = require("./type-member");
|
|
@@ -55,5 +62,8 @@ class Method extends callable_1.Callable {
|
|
|
55
62
|
return !!this.spec.static;
|
|
56
63
|
}
|
|
57
64
|
}
|
|
65
|
+
__decorate([
|
|
66
|
+
_memoized_1.memoizedWhenLocked
|
|
67
|
+
], Method.prototype, "overrides", null);
|
|
58
68
|
exports.Method = Method;
|
|
59
69
|
//# sourceMappingURL=method.js.map
|
package/lib/type-system.d.ts
CHANGED
|
@@ -12,10 +12,19 @@ export declare class TypeSystem {
|
|
|
12
12
|
readonly roots: Assembly[];
|
|
13
13
|
private readonly _assemblyLookup;
|
|
14
14
|
private readonly _cachedClasses;
|
|
15
|
+
private _locked;
|
|
16
|
+
get isLocked(): boolean;
|
|
15
17
|
/**
|
|
16
18
|
* All assemblies in this type system.
|
|
17
19
|
*/
|
|
18
20
|
get assemblies(): readonly Assembly[];
|
|
21
|
+
/**
|
|
22
|
+
* Locks the TypeSystem from further changes
|
|
23
|
+
*
|
|
24
|
+
* Call this once all assemblies have been loaded.
|
|
25
|
+
* This allows the reflection to optimize and cache certain expensive calls.
|
|
26
|
+
*/
|
|
27
|
+
lock(): void;
|
|
19
28
|
/**
|
|
20
29
|
* Load all JSII dependencies of the given NPM package directory.
|
|
21
30
|
*
|
package/lib/type-system.js
CHANGED
|
@@ -17,6 +17,10 @@ class TypeSystem {
|
|
|
17
17
|
this.roots = new Array();
|
|
18
18
|
this._assemblyLookup = new Map();
|
|
19
19
|
this._cachedClasses = new Map();
|
|
20
|
+
this._locked = false;
|
|
21
|
+
}
|
|
22
|
+
get isLocked() {
|
|
23
|
+
return this._locked;
|
|
20
24
|
}
|
|
21
25
|
/**
|
|
22
26
|
* All assemblies in this type system.
|
|
@@ -24,6 +28,15 @@ class TypeSystem {
|
|
|
24
28
|
get assemblies() {
|
|
25
29
|
return Array.from(this._assemblyLookup.values());
|
|
26
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Locks the TypeSystem from further changes
|
|
33
|
+
*
|
|
34
|
+
* Call this once all assemblies have been loaded.
|
|
35
|
+
* This allows the reflection to optimize and cache certain expensive calls.
|
|
36
|
+
*/
|
|
37
|
+
lock() {
|
|
38
|
+
this._locked = true;
|
|
39
|
+
}
|
|
27
40
|
/**
|
|
28
41
|
* Load all JSII dependencies of the given NPM package directory.
|
|
29
42
|
*
|
|
@@ -120,6 +133,9 @@ class TypeSystem {
|
|
|
120
133
|
return this.addAssembly(assembly, options);
|
|
121
134
|
}
|
|
122
135
|
addAssembly(asm, options = {}) {
|
|
136
|
+
if (this.isLocked) {
|
|
137
|
+
throw new Error('The typesystem has been locked from further changes');
|
|
138
|
+
}
|
|
123
139
|
if (asm.system !== this) {
|
|
124
140
|
throw new Error('Assembly has been created for different typesystem');
|
|
125
141
|
}
|
package/lib/type.js
CHANGED
|
@@ -105,7 +105,7 @@ class Type {
|
|
|
105
105
|
return this.getInterfaces(true).some((iface) => iface === base);
|
|
106
106
|
}
|
|
107
107
|
if (this.isClassType() && base.isClassType()) {
|
|
108
|
-
return this.
|
|
108
|
+
return this.ancestors.some((clazz) => clazz === base);
|
|
109
109
|
}
|
|
110
110
|
return false;
|
|
111
111
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsii-reflect",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.93.0",
|
|
4
4
|
"description": "strongly-typed reflection library and tools for jsii",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -34,18 +34,18 @@
|
|
|
34
34
|
"package": "package-js"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@jsii/check-node": "1.
|
|
38
|
-
"@jsii/spec": "^1.
|
|
37
|
+
"@jsii/check-node": "1.93.0",
|
|
38
|
+
"@jsii/spec": "^1.93.0",
|
|
39
39
|
"chalk": "^4",
|
|
40
40
|
"fs-extra": "^10.1.0",
|
|
41
|
-
"oo-ascii-tree": "^1.
|
|
41
|
+
"oo-ascii-tree": "^1.93.0",
|
|
42
42
|
"yargs": "^16.2.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@scope/jsii-calc-lib": "^1.
|
|
45
|
+
"@scope/jsii-calc-lib": "^1.93.0",
|
|
46
46
|
"@types/fs-extra": "^9.0.13",
|
|
47
|
-
"jsii": "^1.
|
|
48
|
-
"jsii-build-tools": "^1.
|
|
47
|
+
"jsii": "^1.93.0",
|
|
48
|
+
"jsii-build-tools": "^1.93.0",
|
|
49
49
|
"jsii-calc": "^3.20.120"
|
|
50
50
|
}
|
|
51
51
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
const lib_1 = require("../lib");
|
|
10
|
+
const _memoized_1 = require("../lib/_memoized");
|
|
11
|
+
const accessorSpy = jest.fn(() => 'foobar');
|
|
12
|
+
class TestClass {
|
|
13
|
+
constructor(system) {
|
|
14
|
+
this.system = system;
|
|
15
|
+
}
|
|
16
|
+
get uncached() {
|
|
17
|
+
return accessorSpy();
|
|
18
|
+
}
|
|
19
|
+
get cached() {
|
|
20
|
+
return accessorSpy();
|
|
21
|
+
}
|
|
22
|
+
get cachedWhenLocked() {
|
|
23
|
+
return accessorSpy();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
__decorate([
|
|
27
|
+
_memoized_1.memoized
|
|
28
|
+
], TestClass.prototype, "cached", null);
|
|
29
|
+
__decorate([
|
|
30
|
+
_memoized_1.memoizedWhenLocked
|
|
31
|
+
], TestClass.prototype, "cachedWhenLocked", null);
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
33
|
+
function noop(_val) { }
|
|
34
|
+
describe('memoized', () => {
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
accessorSpy.mockClear();
|
|
37
|
+
});
|
|
38
|
+
const subject = new TestClass(new lib_1.TypeSystem());
|
|
39
|
+
test('cached property is memoized', () => {
|
|
40
|
+
// Access the property twice
|
|
41
|
+
noop(subject.cached);
|
|
42
|
+
noop(subject.cached);
|
|
43
|
+
expect(accessorSpy).toHaveBeenCalledTimes(1);
|
|
44
|
+
expect(subject.cached).toBe('foobar');
|
|
45
|
+
});
|
|
46
|
+
test('uncached property is not memoized', () => {
|
|
47
|
+
// Access the property twice
|
|
48
|
+
noop(subject.uncached);
|
|
49
|
+
noop(subject.uncached);
|
|
50
|
+
expect(accessorSpy).toHaveBeenCalledTimes(2);
|
|
51
|
+
expect(subject.uncached).toBe('foobar');
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
describe('memoizedWhenLocked', () => {
|
|
55
|
+
let subject;
|
|
56
|
+
beforeEach(() => {
|
|
57
|
+
accessorSpy.mockClear();
|
|
58
|
+
subject = new TestClass(new lib_1.TypeSystem());
|
|
59
|
+
});
|
|
60
|
+
test('property is memoized when the typesystem is locked', () => {
|
|
61
|
+
// Lock the typesystem to enable memoizing
|
|
62
|
+
subject.system.lock();
|
|
63
|
+
// Access the property twice
|
|
64
|
+
noop(subject.cachedWhenLocked);
|
|
65
|
+
noop(subject.cachedWhenLocked);
|
|
66
|
+
expect(accessorSpy).toHaveBeenCalledTimes(1);
|
|
67
|
+
expect(subject.cachedWhenLocked).toBe('foobar');
|
|
68
|
+
});
|
|
69
|
+
test('property is not memoized when the typesystem is not locked', () => {
|
|
70
|
+
// Access the property twice
|
|
71
|
+
noop(subject.cachedWhenLocked);
|
|
72
|
+
noop(subject.cachedWhenLocked);
|
|
73
|
+
expect(accessorSpy).toHaveBeenCalledTimes(2);
|
|
74
|
+
expect(subject.cachedWhenLocked).toBe('foobar');
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
//# sourceMappingURL=_memoized.test.js.map
|