1- import { type GetPropsWithValueExtends } from "@scripts/object" ;
21import { 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
85const SymbolOverrideStore = Symbol . for ( "@duplojs/utils/override" ) ;
96
107declare 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}
0 commit comments