-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dispose hooks (deprecate useContainerRaw) (#323)
* feat: dispose hooks * build: unminify, add source map, deprecate useContainerRaw * fix regression of context and fix tsup build
- Loading branch information
Showing
17 changed files
with
142 additions
and
1,562 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,3 +95,5 @@ dist | |
.yalc | ||
|
||
yalc.lock | ||
|
||
*.svg |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { Awaitable } from '../../types/utility'; | ||
|
||
/** | ||
* Represents a Disposable contract. | ||
* Let dependencies implement this to dispose and cleanup. | ||
*/ | ||
export interface Disposable { | ||
dispose(): Awaitable<unknown>; | ||
} |
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,40 @@ | ||
import type { CoreContainer } from "./container" | ||
|
||
interface HookEvent { | ||
key : PropertyKey | ||
newContainer: any | ||
} | ||
type HookName = 'init'; | ||
|
||
export const createInitListener = (coreContainer : CoreContainer<any>) => { | ||
const initCalled = new Set<PropertyKey>(); | ||
const hasCallableMethod = createPredicate(initCalled); | ||
const unsubscribe = coreContainer.on('containerUpserted', async (event) => { | ||
|
||
if(isNotHookable(event)) { | ||
return; | ||
} | ||
|
||
if(hasCallableMethod('init', event)) { | ||
await event.newContainer?.init(); | ||
initCalled.add(event.key); | ||
} | ||
|
||
}); | ||
return { unsubscribe }; | ||
} | ||
|
||
const isNotHookable = (hk: HookEvent) => { | ||
return typeof hk.newContainer !== 'object' | ||
|| Array.isArray(hk.newContainer) | ||
|| hk.newContainer === null; | ||
} | ||
|
||
const createPredicate = <T extends HookEvent>(called: Set<PropertyKey>) => { | ||
return (hookName: HookName, event: T) => { | ||
const hasMethod = Reflect.has(event.newContainer!, hookName); | ||
const beenCalledOnce = !called.has(event.key) | ||
|
||
return hasMethod && beenCalledOnce | ||
} | ||
} |
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,2 +1,2 @@ | ||
export { useContainerRaw, makeDependencies } from './base'; | ||
export { makeDependencies } from './base'; | ||
export { Service, Services, single, transient } from './dependency-injection'; |
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
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