1 | const dntGlobals = {
|
2 | };
|
3 | export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
4 |
|
5 |
|
6 | function createMergeProxy<T extends object, U extends object>(
|
7 | baseObj: T,
|
8 | extObj: U,
|
9 | ): Omit<T, keyof U> & U {
|
10 | return new Proxy(baseObj, {
|
11 | get(_target, prop, _receiver) {
|
12 | if (prop in extObj) {
|
13 | return (extObj as any)[prop];
|
14 | } else {
|
15 | return (baseObj as any)[prop];
|
16 | }
|
17 | },
|
18 | set(_target, prop, value) {
|
19 | if (prop in extObj) {
|
20 | delete (extObj as any)[prop];
|
21 | }
|
22 | (baseObj as any)[prop] = value;
|
23 | return true;
|
24 | },
|
25 | deleteProperty(_target, prop) {
|
26 | let success = false;
|
27 | if (prop in extObj) {
|
28 | delete (extObj as any)[prop];
|
29 | success = true;
|
30 | }
|
31 | if (prop in baseObj) {
|
32 | delete (baseObj as any)[prop];
|
33 | success = true;
|
34 | }
|
35 | return success;
|
36 | },
|
37 | ownKeys(_target) {
|
38 | const baseKeys = Reflect.ownKeys(baseObj);
|
39 | const extKeys = Reflect.ownKeys(extObj);
|
40 | const extKeysSet = new Set(extKeys);
|
41 | return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
|
42 | },
|
43 | defineProperty(_target, prop, desc) {
|
44 | if (prop in extObj) {
|
45 | delete (extObj as any)[prop];
|
46 | }
|
47 | Reflect.defineProperty(baseObj, prop, desc);
|
48 | return true;
|
49 | },
|
50 | getOwnPropertyDescriptor(_target, prop) {
|
51 | if (prop in extObj) {
|
52 | return Reflect.getOwnPropertyDescriptor(extObj, prop);
|
53 | } else {
|
54 | return Reflect.getOwnPropertyDescriptor(baseObj, prop);
|
55 | }
|
56 | },
|
57 | has(_target, prop) {
|
58 | return prop in extObj || prop in baseObj;
|
59 | },
|
60 | }) as any;
|
61 | }
|