Skip to content

Commit fbfb631

Browse files
committed
fix(hf): override
1 parent 06739f5 commit fbfb631

File tree

3 files changed

+59
-45
lines changed

3 files changed

+59
-45
lines changed

scripts/common/override.ts

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import { type GetPropsWithValueExtends } from "@scripts/object";
21
import { createGlobalStore } from "./globalStore";
3-
import { type Adaptor, type AnyFunction, type AnyValue, type ObjectKey } from "./types";
4-
import * as DObject from "@scripts/object";
5-
import * as DArray from "@scripts/array";
6-
import { pipe } from "./pipe";
2+
import { type Adaptor, type AnyFunction, type AnyValue } from "./types";
3+
import type * as DObject from "@scripts/object";
74

85
const SymbolOverrideStore = Symbol.for("@duplojs/utils/override");
96

107
declare module "./globalStore" {
118
interface GlobalStore {
129
[SymbolOverrideStore]: Record<
1310
string,
14-
[ObjectKey, Exclude<AnyValue, AnyFunction> | AnyFunction<[object, ...unknown[]]>][]
11+
Record<string, unknown>
1512
>;
1613
}
1714
}
@@ -22,7 +19,7 @@ export interface OverrideHandler<
2219
GenericInterface extends object,
2320
> {
2421
setMethod<
25-
GenericProperty extends GetPropsWithValueExtends<
22+
GenericProperty extends DObject.GetPropsWithValueExtends<
2623
GenericInterface,
2724
AnyFunction
2825
>,
@@ -37,7 +34,7 @@ export interface OverrideHandler<
3734
setPropertyDefaultValue<
3835
GenericProperty extends Exclude<
3936
keyof GenericInterface,
40-
GetPropsWithValueExtends<
37+
DObject.GetPropsWithValueExtends<
4138
GenericInterface,
4239
AnyFunction
4340
>
@@ -55,38 +52,62 @@ export function createOverride<
5552
>(
5653
overrideName: string,
5754
): OverrideHandler<GenericInterface> {
58-
const store = overrideStore.value[overrideName] ?? [];
59-
60-
overrideStore.set({
61-
...overrideStore.value,
62-
[overrideName]: store,
63-
});
55+
const overridePropertiesStore = overrideStore.value[overrideName] ?? {};
56+
overrideStore.value[overrideName] ||= overridePropertiesStore;
57+
let cachedStoreKey = Object.keys(overridePropertiesStore);
6458

6559
return {
6660
setMethod(prop, theFunction) {
67-
store.push([prop, theFunction]);
61+
overridePropertiesStore[prop] = theFunction;
62+
cachedStoreKey = Object.keys(overridePropertiesStore);
6863
},
6964
setPropertyDefaultValue(prop, value) {
70-
store.push([prop, value]);
65+
overridePropertiesStore[prop as string] = value;
66+
cachedStoreKey = Object.keys(overridePropertiesStore);
7167
},
7268
apply(overrideInterface) {
73-
const self = {
74-
...overrideInterface,
75-
...pipe(
76-
store,
77-
DArray.map(
78-
([key, value]) => DObject.entry(
79-
key,
80-
typeof value === "function"
81-
? (...args: unknown[]) => value(self, ...args)
82-
: value,
83-
),
84-
),
85-
DObject.fromEntries,
86-
),
87-
};
69+
const cachedOverrideProperties: Record<string, unknown> = {};
70+
71+
const cachedKey = Object.keys(overrideInterface);
72+
73+
const self = new Proxy(
74+
{},
75+
{
76+
get(target, prop: string) {
77+
if (overridePropertiesStore[prop]) {
78+
if (!cachedOverrideProperties[prop]) {
79+
cachedOverrideProperties[prop] = typeof overridePropertiesStore[prop] === "function"
80+
? (...args: unknown[]) => (
81+
overridePropertiesStore[prop] as AnyFunction
82+
)(self, ...args)
83+
: overridePropertiesStore[prop];
84+
}
85+
86+
return cachedOverrideProperties[prop];
87+
}
88+
89+
return overrideInterface[prop as keyof typeof overrideInterface];
90+
},
91+
ownKeys() {
92+
return [
93+
...cachedKey,
94+
...cachedStoreKey,
95+
];
96+
},
97+
has(target, prop) {
98+
return cachedKey.includes(prop as never)
99+
|| cachedStoreKey.includes(prop as never);
100+
},
101+
getOwnPropertyDescriptor() {
102+
return {
103+
enumerable: true,
104+
configurable: true,
105+
};
106+
},
107+
},
108+
);
88109

89-
return self;
110+
return self as never;
90111
},
91112
};
92113
}

tests/common/override.test.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,13 @@ describe("createOverride", () => {
1010
it("overrides methods and properties", () => {
1111
const handler = createOverride<Service>("service.override");
1212
handler.setMethod("getValue", (self) => self.value * 2);
13-
handler.setMethod("increment", (self, delta) => self.value + delta + 1);
14-
handler.setPropertyDefaultValue("value", 10);
1513

16-
const base: Service = {
17-
value: 1,
18-
getValue() {
19-
return this.value;
20-
},
21-
increment(delta) {
22-
return this.value + delta;
23-
},
24-
};
14+
const base = {} as Service;
15+
16+
const result = handler.apply(base as any);
2517

26-
const result = handler.apply(base);
18+
handler.setMethod("increment", (self, delta) => self.value + delta + 1);
19+
handler.setPropertyDefaultValue("value", 10);
2720

2821
expect(result.value).toBe(10);
2922
expect(result.getValue()).toBe(20);

tests/common/pipeCall.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ describe("pipeCall", () => {
1212
),
1313
);
1414

15-
expect(result).toBe(20);
15+
expect(result).toBe(11);
1616
});
1717
});

0 commit comments

Comments
 (0)