-
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.
feat: allow to inject states inside builders
- Loading branch information
1 parent
f9343c8
commit 0b35308
Showing
7 changed files
with
101 additions
and
33 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
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,49 @@ | ||
import { | ||
ExtractStore, | ||
GenericStoreApi, | ||
HookConsumerFunction, | ||
Plugin, | ||
PluginContext, | ||
PluginHooks, | ||
StoreApiDefinition, | ||
} from '~/types'; | ||
import { Container } from '~/container'; | ||
|
||
export class ResolvedPluginContext implements PluginContext { | ||
private readonly initSubscriptions = new Set<HookConsumerFunction>(); | ||
private readonly destroySubscriptions = new Set<HookConsumerFunction>(); | ||
public readonly metadata: Map<string, unknown> = new Map<string, unknown>(); | ||
|
||
hooks: PluginHooks<GenericStoreApi> = { | ||
onInit: (callback) => this.initSubscriptions.add(callback), | ||
onDestroy: (callback) => this.destroySubscriptions.add(callback), | ||
}; | ||
|
||
constructor( | ||
private readonly container: Container | undefined, | ||
public readonly plugins: Plugin<any, any>[], | ||
) {} | ||
|
||
inject<TStoreDefinition extends StoreApiDefinition<any, any>>( | ||
storeDefinition: TStoreDefinition, | ||
): ExtractStore<TStoreDefinition> { | ||
if (!this.container) { | ||
throw new Error('[statebuilder] No container set in current context.'); | ||
} | ||
return this.container.get(storeDefinition); | ||
} | ||
|
||
public runInitSubscriptions(resolvedStore: GenericStoreApi) { | ||
for (const listener of this.initSubscriptions) { | ||
listener(resolvedStore); | ||
this.initSubscriptions.delete(listener); | ||
} | ||
} | ||
|
||
public runDestroySubscriptions(resolvedStore: GenericStoreApi) { | ||
for (const listener of this.destroySubscriptions) { | ||
listener(resolvedStore); | ||
this.destroySubscriptions.delete(listener); | ||
} | ||
} | ||
} |
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