-
Notifications
You must be signed in to change notification settings - Fork 3
provide singletons #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wh4everest
wants to merge
6
commits into
master
Choose a base branch
from
explicit-deps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b0d6c5f
provide singletons
wh4everest b87ccf9
parent, not child
wh4everest 9441f10
all tests pass
wh4everest 471c9ea
App#load now auto-provides the plugin fn; added and adapted tests
wh4everest a4ae359
requireSingleton now checks recursively for provided presence
wh4everest 71d67b2
test for requireSingleton
wh4everest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -46,11 +46,18 @@ export type PublicInterface<T> = { [K in keyof T]: T[K] }; | |
| */ | ||
| export class App { | ||
| private singletonLocator: Locator<this>; | ||
| private providedSingletons: WeakMap<ConstructorOrFactory<App, any>, boolean> = new WeakMap(); | ||
| parentApp: App | null; | ||
|
|
||
| constructor(opts: { parentApp?: App } = {}) { | ||
| this.parentApp = opts.parentApp ? opts.parentApp : null; | ||
| this.singletonLocator = new Locator(this, s => '__appSingleton' in s); | ||
|
|
||
| if (!opts.parentApp) { | ||
| // we must provide this, otherwise withServiceContext will fail every time. | ||
| // we do it once, on the parent app, because child app construction will fail otherwise. | ||
| this.provideSingleton(ServiceContextEvents); // otherwise, withServiceContext fails. | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -102,6 +109,37 @@ export class App { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Registers a singleton as "provided". It's informing the application that a plugin | ||
| * agreed to expose that singleton. | ||
| * | ||
| * Calls to app.getSingleton(UnprovidedService) will fail with an error. | ||
| * | ||
| * Also see: `App#requireSingleton` | ||
| */ | ||
| provideSingleton<T>(Klass: ConstructorOrFactory<App, T>): void { | ||
| if (this.isSingletonProvided(Klass)) { | ||
| throw new Error(`The singleton ${Klass.name} is already provided.`); | ||
| } | ||
| this.providedSingletons.set(Klass, true); | ||
| } | ||
|
|
||
| private isSingletonProvided<T>(Klass: ConstructorOrFactory<App, T>): boolean { | ||
| if (this.providedSingletons.has(Klass)) { | ||
| return true; | ||
| } else if (this.parentApp) { | ||
| return this.parentApp.isSingletonProvided(Klass); | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| requireSingleton<T>(Klass: ConstructorOrFactory<App, T>): void { | ||
| if (!this.providedSingletons.has(Klass)) { | ||
| throw new Error(`The singleton ${Klass} is required, but wasnt provided.`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns an instance of the singleton, if it exists somewhere here or | ||
| * in some of the parent apps. If it doesn't it's created in this app. | ||
|
|
@@ -115,6 +153,12 @@ export class App { | |
| if (this.hasSingleton(Klass)) { | ||
| return this.getExistingSingleton(Klass); | ||
| } | ||
| if (!this.isSingletonProvided(Klass)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe at locator level. |
||
| throw new Error(`Singleton ${Klass.name} wasnt provided`); | ||
| // console.warn(`The singleton ${Klass} was constructed, but wasn't provided beforehand.`); | ||
| // console.warn(`Please provide it explicitly using "App#provideSingleton(${Klass})".`); | ||
| // console.warn('In the future, this will be an error.'); | ||
| } | ||
| return this.singletonLocator.get(Klass); | ||
| } | ||
|
|
||
|
|
@@ -298,6 +342,10 @@ export class ServiceContext { | |
| getSingleton<T>(SingletonClass: ConstructorOrFactory<App, T>): T { | ||
| return this.app.getSingleton(SingletonClass); | ||
| } | ||
|
|
||
| requireSingleton<T>(SingletonClass: ConstructorOrFactory<App, T>) { | ||
| return this.app.requireSingleton(SingletonClass); | ||
| } | ||
| } | ||
|
|
||
| type ContextListener = (serviceCtx: ServiceContext, error: Error | null) => PromiseLike<void>; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we refactor so the same map is used for provides and overrides?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on second thought maybe not, you might want to override before providing.