1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.IocProxyClass = exports.IocProxyObject = void 0;
|
12 | function getBindingValue(handler) {
|
13 | return handler.options.has(handler.namespace)
|
14 | ? handler.options.resolve(handler.namespace, handler.value)
|
15 | : handler.value;
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 | const objectHandler = (options) => {
|
21 | return {
|
22 | get(_, key, receiver) {
|
23 | const descriptor = Object.getOwnPropertyDescriptor(options.value, key);
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | if (descriptor && !descriptor.configurable) {
|
33 | if (descriptor.set && !descriptor.get) {
|
34 | return undefined;
|
35 | }
|
36 | if (descriptor.writable === false) {
|
37 | return Reflect.get(options.value, key, receiver);
|
38 | }
|
39 | }
|
40 | return Reflect.get(getBindingValue(options), key, receiver);
|
41 | },
|
42 | apply(_, thisArgument, args) {
|
43 | return Reflect.apply(getBindingValue(options), thisArgument, args);
|
44 | },
|
45 | defineProperty(_, propertyKey, attributes) {
|
46 | return Reflect.defineProperty(getBindingValue(options), propertyKey, attributes);
|
47 | },
|
48 | deleteProperty(_, propertyKey) {
|
49 | return Reflect.deleteProperty(getBindingValue(options), propertyKey);
|
50 | },
|
51 | getOwnPropertyDescriptor(_, propertyKey) {
|
52 | return Reflect.getOwnPropertyDescriptor(getBindingValue(options), propertyKey);
|
53 | },
|
54 | getPrototypeOf(_) {
|
55 | return Reflect.getPrototypeOf(getBindingValue(options));
|
56 | },
|
57 | has(_, propertyKey) {
|
58 | return Reflect.has(getBindingValue(options), propertyKey);
|
59 | },
|
60 | isExtensible(_) {
|
61 | return Reflect.isExtensible(getBindingValue(options));
|
62 | },
|
63 | ownKeys(_) {
|
64 | return Reflect.ownKeys(getBindingValue(options));
|
65 | },
|
66 | preventExtensions() {
|
67 | throw new Error('Cannot prevent extensions during a fake');
|
68 | },
|
69 | set(_, propertyKey, value, receiver) {
|
70 | return Reflect.set(getBindingValue(options), propertyKey, value, receiver);
|
71 | },
|
72 | setPrototypeOf(_, proto) {
|
73 | return Reflect.setPrototypeOf(getBindingValue(options), proto);
|
74 | },
|
75 | };
|
76 | };
|
77 |
|
78 |
|
79 |
|
80 | const classHandler = (options) => {
|
81 | return Object.assign({}, objectHandler(options), {
|
82 | construct(_, args, newTarget) {
|
83 | return Reflect.construct(getBindingValue(options), args, newTarget);
|
84 | },
|
85 | });
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 | class IocProxyObject {
|
91 | constructor(namespace, value, options) {
|
92 | this.namespace = namespace;
|
93 | this.value = value;
|
94 | this.options = options;
|
95 | return new Proxy(value, objectHandler({ namespace, value, options }));
|
96 | }
|
97 | }
|
98 | exports.IocProxyObject = IocProxyObject;
|
99 |
|
100 |
|
101 |
|
102 | function IocProxyClass(namespace, value, options) {
|
103 | return new Proxy(value, classHandler({ namespace, value, options }));
|
104 | }
|
105 | exports.IocProxyClass = IocProxyClass;
|