-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove async api and add lazy option (#42)
* refactor: add lazy option and remove async api * feat: add no throw option * feat: inject suppot nothrow * chore: lint code * refactor: lazy implemention * fix: no use object as type
- Loading branch information
1 parent
4a982f5
commit ad79d40
Showing
17 changed files
with
138 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Container from './container'; | ||
import { Identifier } from './types'; | ||
|
||
const reflectMethods: ReadonlyArray<keyof ProxyHandler<any>> = ['get', 'set', 'getPrototypeOf']; | ||
|
||
function createHandler<T extends Object>(delayedObject: () => T): ProxyHandler<T> { | ||
const handler: ProxyHandler<T> = {}; | ||
const install = (name: keyof ProxyHandler<T>) => { | ||
handler[name] = (...args: any[]) => { | ||
args[0] = delayedObject(); | ||
const method = Reflect[name]; | ||
return (method as any)(...args); | ||
}; | ||
}; | ||
reflectMethods.forEach(install); | ||
return handler; | ||
} | ||
|
||
function createProxy<T>(id: Identifier<T>, container: Container): T { | ||
const target: Record<string, any> = {}; | ||
let init = false; | ||
let value: T; | ||
const delayedObject: () => T = (): T => { | ||
if (!init) { | ||
value = container.get(id); | ||
init = true; | ||
} | ||
return value; | ||
}; | ||
return new Proxy<any>(target, createHandler(delayedObject)) as T; | ||
} | ||
|
||
export function lazyHandler<T>(id: Identifier<T>, container: Container): T { | ||
return createProxy(id, container); | ||
} |
Oops, something went wrong.