This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
generated from CourseDesign/monorepo-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #40 from siyual-park/develop
deploy: minor
- Loading branch information
Showing
12 changed files
with
201 additions
and
11 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/cheeket/lib/binding/combined-binding-dictionary.ts
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,37 @@ | ||
import * as interfaces from "../interfaces"; | ||
|
||
class CombinedBindingDictionary implements interfaces.BindingDictionary { | ||
readonly #bindingDictionaries: interfaces.BindingDictionary[] = []; | ||
|
||
constructor(bindingDictionaries: interfaces.BindingDictionary[]) { | ||
this.#bindingDictionaries = bindingDictionaries; | ||
} | ||
|
||
get<T>(token: interfaces.Token<T>): interfaces.Provider<T> | undefined { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const bindingDictionary of this.#bindingDictionaries) { | ||
const provider = bindingDictionary.get(token); | ||
if (provider !== undefined) return provider; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
getAll<T>(token: interfaces.Token<T>): interfaces.Provider<T>[] { | ||
const providers: interfaces.Provider<T>[] = []; | ||
|
||
this.#bindingDictionaries.forEach((bindingDictionary) => { | ||
providers.push(...bindingDictionary.getAll(token)); | ||
}); | ||
|
||
return providers; | ||
} | ||
|
||
has<T>(token: interfaces.Token<T>): boolean { | ||
return this.#bindingDictionaries.some((bindingDictionary) => | ||
bindingDictionary.has(token) | ||
); | ||
} | ||
} | ||
|
||
export default CombinedBindingDictionary; |
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,91 @@ | ||
import { EventEmitter2 } from "eventemitter2"; | ||
|
||
import * as interfaces from "../interfaces"; | ||
import Context from "../context/context"; | ||
import MutableBindingDictionary from "../binding/mutable-binding-dictionary"; | ||
import CombinedBindingDictionary from "../binding/combined-binding-dictionary"; | ||
import Request from "../context/request"; | ||
import CantResolveError from "../error/cant-resolve-error"; | ||
import { EventType } from "../event"; | ||
|
||
class ChildContainer extends EventEmitter2 implements interfaces.Container { | ||
readonly #bindingDictionary: interfaces.MutableBindingDictionary = new MutableBindingDictionary(); | ||
|
||
readonly #parentBindingDictionary: interfaces.BindingDictionary; | ||
|
||
readonly #combinedBindingDictionary: interfaces.BindingDictionary; | ||
|
||
constructor(parentBindingDictionary: interfaces.BindingDictionary) { | ||
super(); | ||
|
||
this.#parentBindingDictionary = parentBindingDictionary; | ||
this.#combinedBindingDictionary = new CombinedBindingDictionary([ | ||
this.#parentBindingDictionary, | ||
this.#bindingDictionary, | ||
]); | ||
} | ||
|
||
bind<T>(token: interfaces.Token<T>, provider: interfaces.Provider<T>): void { | ||
this.#bindingDictionary.set(token, provider); | ||
} | ||
|
||
isBound<T>(token: interfaces.Token<T>): boolean { | ||
return this.#combinedBindingDictionary.has(token); | ||
} | ||
|
||
rebind<T>( | ||
token: interfaces.Token<T>, | ||
provider: interfaces.Provider<T> | ||
): void { | ||
this.#bindingDictionary.delete(token); | ||
this.#bindingDictionary.set(token, provider); | ||
} | ||
|
||
unbind<T>(token: interfaces.Token<T>): void { | ||
this.#bindingDictionary.delete(token); | ||
} | ||
|
||
async resolve<T>(token: interfaces.Token<T>): Promise<T> { | ||
const provider = this.#combinedBindingDictionary.get(token); | ||
if (provider !== undefined) { | ||
return this.resolveProvider(provider, token); | ||
} | ||
|
||
throw new CantResolveError(token, this); | ||
} | ||
|
||
async resolveAll<T>(token: interfaces.Token<T>): Promise<T[]> { | ||
const providers = this.#combinedBindingDictionary.getAll(token); | ||
if (providers.length > 0) { | ||
return Promise.all( | ||
providers.map((provider) => this.resolveProvider(provider, token)) | ||
); | ||
} | ||
|
||
throw new CantResolveError(token, this); | ||
} | ||
|
||
private async resolveProvider<T>( | ||
provider: interfaces.Provider<T>, | ||
token: interfaces.Token<T> | ||
): Promise<T> { | ||
const context = this.createContext(token); | ||
const value = await provider(context); | ||
context.request.resolved = value; | ||
|
||
await this.emitAsync(EventType.Resolve, context); | ||
|
||
return value; | ||
} | ||
|
||
private createContext<T>(token: interfaces.Token<T>): interfaces.Context { | ||
const request = new Request(token); | ||
return new Context(this.#combinedBindingDictionary, this, request); | ||
} | ||
|
||
createChildContainer(): interfaces.Container { | ||
return new ChildContainer(this.#combinedBindingDictionary); | ||
} | ||
} | ||
|
||
export default ChildContainer; |
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
13 changes: 13 additions & 0 deletions
13
packages/cheeket/lib/interfaces/mutable-binding-dictionary.ts
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,13 @@ | ||
import Token from "./token"; | ||
import Provider from "./provider"; | ||
import BindingDictionary from "./binding-dictionary"; | ||
|
||
interface MutableBindingDictionary extends BindingDictionary { | ||
set<T>(token: Token<T>, provider: Provider<T>): void; | ||
get<T>(token: Token<T>): Provider<T> | undefined; | ||
getAll<T>(token: Token<T>): Provider<T>[]; | ||
delete<T>(token: Token<T>): void; | ||
has<T>(token: Token<T>): boolean; | ||
} | ||
|
||
export default MutableBindingDictionary; |
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,7 @@ | ||
import Provider from "./provider"; | ||
|
||
interface ScopeProvider<T> extends Provider<T> { | ||
clear(): void; | ||
} | ||
|
||
export default ScopeProvider; |
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