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 #18 from AVykhrystyuk/feature/impl-legacy-factory-…
…registry Feature/implement legacy factory registry (es5)
- Loading branch information
Showing
5 changed files
with
30 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
import { FactoryRegistry } from '../interfaces'; | ||
import { FactoryRegistry, ResolveFactoryFunction, Token } from '../interfaces'; | ||
|
||
export function createLegacyFactoryRegistry(): FactoryRegistry { | ||
const factoryByToken = Object.create(null); | ||
// cannot use `string | symbol` type here, see https://github.com/Microsoft/TypeScript/issues/24587 | ||
const factoryByToken: Record<string, ResolveFactoryFunction<unknown>> = Object.create(null); | ||
// cannot iterate through symbols as keys in ES5 | ||
const keys: Token<unknown>[] = []; | ||
|
||
const toKey = (token: Token<unknown>) => token as unknown as string; | ||
|
||
return { | ||
getTokens: () => { | ||
throw Error('not impl'); | ||
}, | ||
hasFactory: (token) => { | ||
throw Error('not impl'); | ||
}, | ||
getFactory: (token) => { | ||
throw Error('not impl'); | ||
}, | ||
getTokens: () => keys, | ||
hasFactory: (token) => Object.prototype.hasOwnProperty.call(factoryByToken, toKey(token)), | ||
getFactory: (token) => factoryByToken[toKey(token)], | ||
setFactory(token, factory) { | ||
throw Error('not impl'); | ||
if (!this.hasFactory(token)) { | ||
keys.push(token); | ||
} | ||
factoryByToken[toKey(token)] = factory; | ||
}, | ||
forEach(callback) { | ||
throw Error('not impl'); | ||
keys.forEach((token) => { | ||
const factory = this.getFactory(token)!; | ||
callback(factory, token, this); | ||
}); | ||
}, | ||
}; | ||
} |
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