This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from FullScreenShenanigans/firefox-factory
Added a separate factory function for non-class components
- Loading branch information
Showing
4 changed files
with
126 additions
and
111 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 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 |
---|---|---|
@@ -1,33 +1,42 @@ | ||
export type IClassWithArg<TContainer, TInstance> = new(arg: TContainer) => TInstance; | ||
export type IClassWithArg<TContainer, TInstance> = new (arg: TContainer) => TInstance; | ||
|
||
export type IClassWithoutArgs<TInstance> = new() => TInstance; | ||
export type IClassWithoutArgs<TInstance> = new () => TInstance; | ||
|
||
export type IComponentFunction<TContainer, TInstance> = (container: TContainer) => TInstance; | ||
|
||
export type IComponentClassOrFunction<TContainer, TInstance> = | ||
export type IComponentClass<TContainer, TInstance> = | ||
| IClassWithArg<TContainer, TInstance> | ||
| IClassWithoutArgs<TInstance> | ||
| IComponentFunction<TContainer, TInstance> | ||
; | ||
; | ||
|
||
/** | ||
* Adds a member component to a parent container. | ||
* Decorates a caching getter on a class prototype. | ||
* | ||
* @param componentFunction Class or function that creates the component. | ||
* @param factory Method used once within the getter to create an instance member. | ||
*/ | ||
export const component = <TContainer extends {}, TInstance>(componentFunction: IComponentClassOrFunction<TContainer, TInstance>) => | ||
export const factory = <TContainer extends {}, TInstance>( | ||
factory: IComponentFunction<TContainer, TInstance>, | ||
) => | ||
(parentPrototype: TContainer, memberName: string) => { | ||
Object.defineProperty(parentPrototype, memberName, { | ||
configurable: true, | ||
get(this: TContainer): TInstance { | ||
const value: TInstance = new (componentFunction as IClassWithArg<TContainer, TInstance>)(this); | ||
const value: TInstance = factory(this); | ||
|
||
Object.defineProperty(this, memberName, { | ||
configurable: true, | ||
configurable: false, | ||
get: () => value, | ||
}); | ||
|
||
return value; | ||
}, | ||
}); | ||
}; | ||
|
||
/** | ||
* Decorates a member component class on a class prototype. | ||
* | ||
* @param componentClass Class to be initialized for the member instance. | ||
*/ | ||
export const component = <TContainer extends {}, TInstance>(componentClass: IComponentClass<TContainer, TInstance>) => | ||
factory((container: TContainer) => new componentClass(container)); |
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