@kyrielle/injector 2.0.0-alpha.0 → 2.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/dist/browser.d.ts +25 -0
- package/dist/browser.js +29 -0
- package/dist/browser.js.map +1 -0
- package/dist/{index.d.ts → common.d.ts} +1 -3
- package/dist/common.js +6 -0
- package/dist/common.js.map +1 -0
- package/dist/current-scope.d.ts +1 -0
- package/dist/current-scope.js +7 -0
- package/dist/current-scope.js.map +1 -0
- package/dist/defs/index.js.map +1 -1
- package/dist/defs/injectable.js +2 -2
- package/dist/defs/injectable.js.map +1 -1
- package/dist/defs/scope.d.ts +1 -1
- package/dist/defs/scope.js.map +1 -1
- package/dist/defs/token.d.ts +2 -2
- package/dist/defs/token.js.map +1 -1
- package/dist/global-scope.d.ts +1 -1
- package/dist/global-scope.js +5 -4
- package/dist/global-scope.js.map +1 -1
- package/dist/inject.d.ts +1 -7
- package/dist/inject.js +12 -15
- package/dist/inject.js.map +1 -1
- package/dist/internals/BrowserContext.d.ts +7 -0
- package/dist/internals/BrowserContext.js +23 -0
- package/dist/internals/BrowserContext.js.map +1 -0
- package/dist/internals/InjectionContext.d.ts +12 -0
- package/dist/internals/InjectionContext.js +3 -0
- package/dist/internals/InjectionContext.js.map +1 -0
- package/dist/internals/NodeContext.d.ts +7 -0
- package/dist/internals/NodeContext.js +13 -0
- package/dist/internals/NodeContext.js.map +1 -0
- package/dist/internals/UnsupportedAsyncToken.d.ts +2 -0
- package/dist/internals/UnsupportedAsyncToken.js +4 -0
- package/dist/internals/UnsupportedAsyncToken.js.map +1 -0
- package/dist/node.d.ts +25 -0
- package/dist/node.js +29 -0
- package/dist/node.js.map +1 -0
- package/dist/root-scope.d.ts +7 -0
- package/dist/root-scope.js +15 -0
- package/dist/root-scope.js.map +1 -0
- package/dist/run-in.d.ts +1 -0
- package/dist/run-in.js +7 -0
- package/dist/run-in.js.map +1 -0
- package/dist/scope.d.ts +1 -7
- package/dist/scope.js +16 -19
- package/dist/scope.js.map +1 -1
- package/dist/token.d.ts +1 -1
- package/dist/token.js +4 -4
- package/dist/token.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +69 -60
- package/dist/async-scope.d.ts +0 -30
- package/dist/async-scope.js +0 -14
- package/dist/async-scope.js.map +0 -1
- package/dist/index.js +0 -8
- package/dist/index.js.map +0 -1
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Julien Capellari
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Julien Capellari
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export * from './common.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns current scope.
|
|
4
|
+
*
|
|
5
|
+
* @version 2.0.0
|
|
6
|
+
*/
|
|
7
|
+
export declare const currentScope$: () => import("./common.js").Scope;
|
|
8
|
+
/**
|
|
9
|
+
* Creates an instance of token, using and storing in scope.
|
|
10
|
+
*
|
|
11
|
+
* @version 1.0.0
|
|
12
|
+
*/
|
|
13
|
+
export declare const inject$: <T>(token: import("./common.js").Token<T>, scope?: import("./common.js").Scope) => T;
|
|
14
|
+
/**
|
|
15
|
+
* Runs a function in a given scope. Any {@link inject$} calls within the function will use the provided scope by default.
|
|
16
|
+
*
|
|
17
|
+
* @version 2.0.0
|
|
18
|
+
*/
|
|
19
|
+
export declare const runIn$: <T>(scope: import("./common.js").Scope, fn: () => T) => T;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new {@link ChildScope}, with a given parent defaulting to current scope
|
|
22
|
+
*
|
|
23
|
+
* @version 1.0.0
|
|
24
|
+
*/
|
|
25
|
+
export declare const scope$: (name: string, parent?: import("./common.js").Scope) => import("./common.js").ChildScope;
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createCurrentScope$ } from './current-scope.js';
|
|
2
|
+
import { createInject$ } from './inject.js';
|
|
3
|
+
import { BrowserContext } from './internals/BrowserContext.js';
|
|
4
|
+
import { createRunIn$ } from './run-in.js';
|
|
5
|
+
import { createScope$ } from './scope.js';
|
|
6
|
+
const context = new BrowserContext();
|
|
7
|
+
export * from './common.js';
|
|
8
|
+
/**
|
|
9
|
+
* Returns current scope.
|
|
10
|
+
*
|
|
11
|
+
* @version 2.0.0
|
|
12
|
+
*/ export const currentScope$ = createCurrentScope$(context);
|
|
13
|
+
/**
|
|
14
|
+
* Creates an instance of token, using and storing in scope.
|
|
15
|
+
*
|
|
16
|
+
* @version 1.0.0
|
|
17
|
+
*/ export const inject$ = createInject$(context);
|
|
18
|
+
/**
|
|
19
|
+
* Runs a function in a given scope. Any {@link inject$} calls within the function will use the provided scope by default.
|
|
20
|
+
*
|
|
21
|
+
* @version 2.0.0
|
|
22
|
+
*/ export const runIn$ = createRunIn$(context);
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new {@link ChildScope}, with a given parent defaulting to current scope
|
|
25
|
+
*
|
|
26
|
+
* @version 1.0.0
|
|
27
|
+
*/ export const scope$ = createScope$(context);
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/browser.ts"],"sourcesContent":["import { createCurrentScope$ } from './current-scope.js';\nimport { createInject$ } from './inject.js';\nimport { BrowserContext } from './internals/BrowserContext.js';\nimport { createRunIn$ } from './run-in.js';\nimport { createScope$ } from './scope.js';\n\nconst context = new BrowserContext();\n\nexport * from './common.js';\n\n/**\n * Returns current scope.\n *\n * @version 2.0.0\n */\nexport const currentScope$ = createCurrentScope$(context);\n\n/**\n * Creates an instance of token, using and storing in scope.\n *\n * @version 1.0.0\n */\nexport const inject$ = createInject$(context);\n\n/**\n * Runs a function in a given scope. Any {@link inject$} calls within the function will use the provided scope by default.\n *\n * @version 2.0.0\n */\nexport const runIn$ = createRunIn$(context);\n\n/**\n * Creates a new {@link ChildScope}, with a given parent defaulting to current scope\n *\n * @version 1.0.0\n */\nexport const scope$ = createScope$(context);"],"names":["createCurrentScope$","createInject$","BrowserContext","createRunIn$","createScope$","context","currentScope$","inject$","runIn$","scope$"],"mappings":"AAAA,SAASA,mBAAmB,QAAQ,qBAAqB;AACzD,SAASC,aAAa,QAAQ,cAAc;AAC5C,SAASC,cAAc,QAAQ,gCAAgC;AAC/D,SAASC,YAAY,QAAQ,cAAc;AAC3C,SAASC,YAAY,QAAQ,aAAa;AAE1C,MAAMC,UAAU,IAAIH;AAEpB,cAAc,cAAc;AAE5B;;;;CAIC,GACD,OAAO,MAAMI,gBAAgBN,oBAAoBK,SAAS;AAE1D;;;;CAIC,GACD,OAAO,MAAME,UAAUN,cAAcI,SAAS;AAE9C;;;;CAIC,GACD,OAAO,MAAMG,SAASL,aAAaE,SAAS;AAE5C;;;;CAIC,GACD,OAAO,MAAMI,SAASL,aAAaC,SAAS"}
|
package/dist/common.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/common.ts"],"sourcesContent":["export * from './defs/index.js';\n\nexport * from './global-scope.js';\nexport * from './token.js';\nexport * from './root-scope.js';\n"],"names":[],"mappings":"AAAA,cAAc,kBAAkB;AAEhC,cAAc,oBAAoB;AAClC,cAAc,aAAa;AAC3B,cAAc,kBAAkB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/current-scope.ts"],"sourcesContent":["import type { Scope } from './defs/index.js';\nimport type { InjectionContext } from './internals/InjectionContext.js';\n\n/** @internal */\nexport function createCurrentScope$(context: InjectionContext) {\n return function currentScope$(): Scope {\n return context.getScope();\n };\n}\n"],"names":["createCurrentScope$","context","currentScope$","getScope"],"mappings":"AAGA,cAAc,GACd,OAAO,SAASA,oBAAoBC,OAAyB;IAC3D,OAAO,SAASC;QACd,OAAOD,QAAQE,QAAQ;IACzB;AACF"}
|
package/dist/defs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/defs/index.ts"],"sourcesContent":["export * from './injectable.js';\
|
|
1
|
+
{"version":3,"sources":["../../src/defs/index.ts"],"sourcesContent":["export * from './injectable.js';\nexport * from './scope.js';\nexport * from './token.js';"],"names":[],"mappings":"AAAA,cAAc,kBAAkB;AAChC,cAAc,aAAa;AAC3B,cAAc,aAAa"}
|
package/dist/defs/injectable.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/defs/injectable.ts"],"sourcesContent":["import type { Scope } from './scope.js';\
|
|
1
|
+
{"version":3,"sources":["../../src/defs/injectable.ts"],"sourcesContent":["import type { Scope } from './scope.js';\n\n/**\n * Object that can be injected.\n */\nexport interface Injectable<out T = unknown> {\n /**\n * Create a new instance of objet, and store it in a scope.\n * @param scope Scope used to store instance, defaults to global scope.\n */\n inject(scope: Scope): T;\n}\n\n/**\n * Object that can be asynchronously injected.\n */\nexport interface AsyncInjectable<out T = unknown> extends Injectable<PromiseLike<T>> {}\n\n/**\n * Extract value type from an Injectable type\n */\nexport type InjectedValue<R extends Injectable> = R extends Injectable<infer D> ? D : never;"],"names":[],"mappings":"AAkBA;;CAEC,GACD,WAA4F"}
|
package/dist/defs/scope.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { Token } from './token.js';
|
|
|
3
3
|
* Root scope, map matching tokens to instances
|
|
4
4
|
*/
|
|
5
5
|
export interface RootScope {
|
|
6
|
+
readonly name: string;
|
|
6
7
|
/**
|
|
7
8
|
* Return value stored at given token
|
|
8
9
|
*/
|
|
@@ -20,7 +21,6 @@ export interface RootScope {
|
|
|
20
21
|
* Child scope, map matching tokens to instances, with inheritance.
|
|
21
22
|
*/
|
|
22
23
|
export interface ChildScope extends RootScope {
|
|
23
|
-
readonly name: string;
|
|
24
24
|
readonly parentScope: Scope;
|
|
25
25
|
}
|
|
26
26
|
export type Scope = RootScope | ChildScope;
|
package/dist/defs/scope.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/defs/scope.ts"],"sourcesContent":["import type { Token } from './token.js';\
|
|
1
|
+
{"version":3,"sources":["../../src/defs/scope.ts"],"sourcesContent":["import type { Token } from './token.js';\n\n/**\n * Root scope, map matching tokens to instances\n */\nexport interface RootScope {\n readonly name: string;\n\n /**\n * Return value stored at given token\n */\n get<T>(token: Token<T>): T | undefined;\n\n /**\n * Update value stored at given token\n */\n set<T>(token: Token<T>, obj: T): void;\n\n /**\n * Removes all token stored in the current scope\n */\n clear(): void;\n}\n\n/**\n * Child scope, map matching tokens to instances, with inheritance.\n */\nexport interface ChildScope extends RootScope {\n readonly parentScope: Scope;\n}\n\nexport type Scope = RootScope | ChildScope;"],"names":[],"mappings":"AA+BA,WAA2C"}
|
package/dist/defs/token.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { Injectable } from './injectable.js';
|
|
2
2
|
/**
|
|
3
|
-
* Class
|
|
3
|
+
* Class-based token
|
|
4
4
|
*/
|
|
5
5
|
export type ClassToken<out T = unknown> = new () => T;
|
|
6
6
|
/**
|
|
7
|
-
* Symbol
|
|
7
|
+
* Symbol-based token
|
|
8
8
|
*/
|
|
9
9
|
export type SymbolToken<T = unknown> = symbol & Injectable<T>;
|
|
10
10
|
export type Token<T = unknown> = ClassToken<T> | SymbolToken<T>;
|
package/dist/defs/token.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/defs/token.ts"],"sourcesContent":["import type { Injectable } from './injectable.js';\
|
|
1
|
+
{"version":3,"sources":["../../src/defs/token.ts"],"sourcesContent":["import type { Injectable } from './injectable.js';\n\n/**\n * Class-based token\n */\nexport type ClassToken<out T = unknown> = new () => T;\n\n/**\n * Symbol-based token\n */\nexport type SymbolToken<T = unknown> = symbol & Injectable<T>;\n\nexport type Token<T = unknown> = ClassToken<T> | SymbolToken<T>;"],"names":[],"mappings":"AAYA,WAAgE"}
|
package/dist/global-scope.d.ts
CHANGED
package/dist/global-scope.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
// Constants
|
|
2
2
|
const GLOBAL_SCOPE_MAP = Symbol.for('kyrielle/injector:GLOBAL_SCOPE_MAP');
|
|
3
|
-
/**
|
|
4
|
-
* Returns
|
|
5
|
-
*
|
|
6
|
-
* @version 1.0.0
|
|
3
|
+
/**
|
|
4
|
+
* Returns the global {@link RootScope}
|
|
5
|
+
*
|
|
6
|
+
* @version 1.0.0
|
|
7
7
|
*/ export function globalScope$() {
|
|
8
8
|
const map = globalThis[GLOBAL_SCOPE_MAP] ??= new Map();
|
|
9
9
|
return {
|
|
10
|
+
name: '[[global]]',
|
|
10
11
|
get: (token)=>map.get(token),
|
|
11
12
|
set: (token, value)=>map.set(token, value),
|
|
12
13
|
clear: ()=>map.clear()
|
package/dist/global-scope.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/global-scope.ts"],"sourcesContent":["import type { RootScope, Token } from './defs/index.js';\
|
|
1
|
+
{"version":3,"sources":["../src/global-scope.ts"],"sourcesContent":["import type { RootScope, Token } from './defs/index.js';\n\n// Constants\nconst GLOBAL_SCOPE_MAP = Symbol.for('kyrielle/injector:GLOBAL_SCOPE_MAP');\n\n/**\n * Returns the global {@link RootScope}\n *\n * @version 1.0.0\n */\nexport function globalScope$(): RootScope {\n const map = (globalThis as { [GLOBAL_SCOPE_MAP]?: Map<Token, unknown> })[GLOBAL_SCOPE_MAP] ??= new Map();\n\n return {\n name: '[[global]]',\n get: <T>(token: Token<T>) => map.get(token) as T,\n set: (token, value) => map.set(token, value),\n clear: () => map.clear(),\n };\n}\n"],"names":["GLOBAL_SCOPE_MAP","Symbol","for","globalScope$","map","globalThis","Map","name","get","token","set","value","clear"],"mappings":"AAEA,YAAY;AACZ,MAAMA,mBAAmBC,OAAOC,GAAG,CAAC;AAEpC;;;;CAIC,GACD,OAAO,SAASC;IACd,MAAMC,MAAM,AAACC,UAA2D,CAACL,iBAAiB,KAAK,IAAIM;IAEnG,OAAO;QACLC,MAAM;QACNC,KAAK,CAAIC,QAAoBL,IAAII,GAAG,CAACC;QACrCC,KAAK,CAACD,OAAOE,QAAUP,IAAIM,GAAG,CAACD,OAAOE;QACtCC,OAAO,IAAMR,IAAIQ,KAAK;IACxB;AACF"}
|
package/dist/inject.d.ts
CHANGED
package/dist/inject.js
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
value
|
|
11
|
-
} else {
|
|
12
|
-
value = token.inject(scope);
|
|
1
|
+
/** @internal */ export function createInject$(context) {
|
|
2
|
+
return function inject$(token, scope = context.getScope()) {
|
|
3
|
+
let value = scope.get(token);
|
|
4
|
+
if (value === undefined) {
|
|
5
|
+
if (typeof token === 'function') {
|
|
6
|
+
value = context.runInScope(scope, ()=>new token());
|
|
7
|
+
} else {
|
|
8
|
+
value = context.runInScope(scope, ()=>token.inject(scope));
|
|
9
|
+
}
|
|
10
|
+
scope.set(token, value);
|
|
13
11
|
}
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
return value;
|
|
12
|
+
return value;
|
|
13
|
+
};
|
|
17
14
|
}
|
|
18
15
|
|
|
19
16
|
//# sourceMappingURL=inject.js.map
|
package/dist/inject.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/inject.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../src/inject.ts"],"sourcesContent":["import type { InjectionContext } from './internals/InjectionContext.js';\nimport type { Scope, Token } from './defs/index.js';\n\n/** @internal */\nexport function createInject$(context: InjectionContext) {\n return function inject$<T>(token: Token<T>, scope: Scope = context.getScope()): T {\n let value = scope.get(token);\n\n if (value === undefined) {\n if (typeof token === 'function') {\n value = context.runInScope(scope, () => new token());\n } else {\n value = context.runInScope(scope, () => token.inject(scope));\n }\n\n scope.set(token, value);\n }\n\n return value;\n };\n}"],"names":["createInject$","context","inject$","token","scope","getScope","value","get","undefined","runInScope","inject","set"],"mappings":"AAGA,cAAc,GACd,OAAO,SAASA,cAAcC,OAAyB;IACrD,OAAO,SAASC,QAAWC,KAAe,EAAEC,QAAeH,QAAQI,QAAQ,EAAE;QAC3E,IAAIC,QAAQF,MAAMG,GAAG,CAACJ;QAEtB,IAAIG,UAAUE,WAAW;YACvB,IAAI,OAAOL,UAAU,YAAY;gBAC/BG,QAAQL,QAAQQ,UAAU,CAACL,OAAO,IAAM,IAAID;YAC9C,OAAO;gBACLG,QAAQL,QAAQQ,UAAU,CAACL,OAAO,IAAMD,MAAMO,MAAM,CAACN;YACvD;YAEAA,MAAMO,GAAG,CAACR,OAAOG;QACnB;QAEA,OAAOA;IACT;AACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Scope } from '../defs/index.js';
|
|
2
|
+
import type { InjectionContext } from './InjectionContext.js';
|
|
3
|
+
export declare class BrowserContext implements InjectionContext {
|
|
4
|
+
private _scope;
|
|
5
|
+
getScope(): Scope;
|
|
6
|
+
runInScope<T>(scope: Scope, fn: () => T): T;
|
|
7
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { globalScope$ } from '../global-scope.js';
|
|
2
|
+
import { UnsupportedAsyncToken } from './UnsupportedAsyncToken.js';
|
|
3
|
+
export class BrowserContext {
|
|
4
|
+
_scope = globalScope$();
|
|
5
|
+
getScope() {
|
|
6
|
+
return this._scope;
|
|
7
|
+
}
|
|
8
|
+
runInScope(scope, fn) {
|
|
9
|
+
const oldScope = this._scope;
|
|
10
|
+
this._scope = scope;
|
|
11
|
+
try {
|
|
12
|
+
const result = fn();
|
|
13
|
+
if (result instanceof Promise) {
|
|
14
|
+
throw new UnsupportedAsyncToken('Async tokens are not supported in browsers');
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
} finally{
|
|
18
|
+
this._scope = oldScope;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=BrowserContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/internals/BrowserContext.ts"],"sourcesContent":["import type { Scope } from '../defs/index.js';\nimport type { InjectionContext } from './InjectionContext.js';\nimport { globalScope$ } from '../global-scope.js';\nimport { UnsupportedAsyncToken } from './UnsupportedAsyncToken.js';\n\nexport class BrowserContext implements InjectionContext {\n private _scope: Scope = globalScope$();\n\n getScope(): Scope {\n return this._scope;\n }\n\n runInScope<T>(scope: Scope, fn: () => T): T {\n const oldScope = this._scope;\n this._scope = scope;\n\n try {\n const result = fn();\n\n if (result instanceof Promise) {\n throw new UnsupportedAsyncToken('Async tokens are not supported in browsers');\n }\n\n return result;\n } finally {\n this._scope = oldScope;\n }\n }\n}"],"names":["globalScope$","UnsupportedAsyncToken","BrowserContext","_scope","getScope","runInScope","scope","fn","oldScope","result","Promise"],"mappings":"AAEA,SAASA,YAAY,QAAQ,qBAAqB;AAClD,SAASC,qBAAqB,QAAQ,6BAA6B;AAEnE,OAAO,MAAMC;IACHC,SAAgBH,eAAe;IAEvCI,WAAkB;QAChB,OAAO,IAAI,CAACD,MAAM;IACpB;IAEAE,WAAcC,KAAY,EAAEC,EAAW,EAAK;QAC1C,MAAMC,WAAW,IAAI,CAACL,MAAM;QAC5B,IAAI,CAACA,MAAM,GAAGG;QAEd,IAAI;YACF,MAAMG,SAASF;YAEf,IAAIE,kBAAkBC,SAAS;gBAC7B,MAAM,IAAIT,sBAAsB;YAClC;YAEA,OAAOQ;QACT,SAAU;YACR,IAAI,CAACN,MAAM,GAAGK;QAChB;IACF;AACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Scope } from '../defs/scope.js';
|
|
2
|
+
export interface InjectionContext {
|
|
3
|
+
/**
|
|
4
|
+
* Returns the current scope.
|
|
5
|
+
*/
|
|
6
|
+
getScope(): Scope;
|
|
7
|
+
/**
|
|
8
|
+
* Runs the given function within the given scope.
|
|
9
|
+
* Any {@link getScope} call made within the function will return the given scope.
|
|
10
|
+
*/
|
|
11
|
+
runInScope<T>(scope: Scope, fn: () => T): T;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/internals/InjectionContext.ts"],"sourcesContent":["import type { Scope } from '../defs/scope.js';\n\nexport interface InjectionContext {\n /**\n * Returns the current scope.\n */\n getScope(): Scope;\n\n /**\n * Runs the given function within the given scope.\n * Any {@link getScope} call made within the function will return the given scope.\n */\n runInScope<T>(scope: Scope, fn: () => T): T;\n}"],"names":[],"mappings":"AAEA,WAWC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { InjectionContext } from './InjectionContext.js';
|
|
2
|
+
import type { Scope } from '../defs/index.js';
|
|
3
|
+
export declare class NodeContext implements InjectionContext {
|
|
4
|
+
private readonly _storage;
|
|
5
|
+
getScope(): Scope;
|
|
6
|
+
runInScope<T>(scope: Scope, fn: () => T): T;
|
|
7
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { globalScope$ } from '../global-scope.js';
|
|
3
|
+
export class NodeContext {
|
|
4
|
+
_storage = new AsyncLocalStorage();
|
|
5
|
+
getScope() {
|
|
6
|
+
return this._storage.getStore() ?? globalScope$();
|
|
7
|
+
}
|
|
8
|
+
runInScope(scope, fn) {
|
|
9
|
+
return this._storage.run(scope, fn);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=NodeContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/internals/NodeContext.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport { globalScope$ } from '../global-scope.js';\nimport type { InjectionContext } from './InjectionContext.js';\nimport type { Scope } from '../defs/index.js';\n\nexport class NodeContext implements InjectionContext {\n private readonly _storage = new AsyncLocalStorage<Scope>();\n\n getScope(): Scope {\n return this._storage.getStore() ?? globalScope$();\n }\n\n runInScope<T>(scope: Scope, fn: () => T): T {\n return this._storage.run(scope, fn);\n }\n}"],"names":["AsyncLocalStorage","globalScope$","NodeContext","_storage","getScope","getStore","runInScope","scope","fn","run"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,mBAAmB;AACrD,SAASC,YAAY,QAAQ,qBAAqB;AAIlD,OAAO,MAAMC;IACMC,WAAW,IAAIH,oBAA2B;IAE3DI,WAAkB;QAChB,OAAO,IAAI,CAACD,QAAQ,CAACE,QAAQ,MAAMJ;IACrC;IAEAK,WAAcC,KAAY,EAAEC,EAAW,EAAK;QAC1C,OAAO,IAAI,CAACL,QAAQ,CAACM,GAAG,CAACF,OAAOC;IAClC;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/internals/UnsupportedAsyncToken.ts"],"sourcesContent":["export class UnsupportedAsyncToken extends Error {}"],"names":["UnsupportedAsyncToken","Error"],"mappings":"AAAA,OAAO,MAAMA,8BAA8BC;AAAO"}
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export * from './common.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns current scope.
|
|
4
|
+
*
|
|
5
|
+
* @version 2.0.0
|
|
6
|
+
*/
|
|
7
|
+
export declare const currentScope$: () => import("./common.js").Scope;
|
|
8
|
+
/**
|
|
9
|
+
* Creates an instance of token, using and storing in scope.
|
|
10
|
+
*
|
|
11
|
+
* @version 1.0.0
|
|
12
|
+
*/
|
|
13
|
+
export declare const inject$: <T>(token: import("./common.js").Token<T>, scope?: import("./common.js").Scope) => T;
|
|
14
|
+
/**
|
|
15
|
+
* Runs a function in a given scope. Any {@link inject$} calls within the function will use the provided scope by default.
|
|
16
|
+
*
|
|
17
|
+
* @version 2.0.0
|
|
18
|
+
*/
|
|
19
|
+
export declare const runIn$: <T>(scope: import("./common.js").Scope, fn: () => T) => T;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new {@link ChildScope}, with a given parent defaulting to current scope
|
|
22
|
+
*
|
|
23
|
+
* @version 1.0.0
|
|
24
|
+
*/
|
|
25
|
+
export declare const scope$: (name: string, parent?: import("./common.js").Scope) => import("./common.js").ChildScope;
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createCurrentScope$ } from './current-scope.js';
|
|
2
|
+
import { createInject$ } from './inject.js';
|
|
3
|
+
import { NodeContext } from './internals/NodeContext.js';
|
|
4
|
+
import { createRunIn$ } from './run-in.js';
|
|
5
|
+
import { createScope$ } from './scope.js';
|
|
6
|
+
const context = new NodeContext();
|
|
7
|
+
export * from './common.js';
|
|
8
|
+
/**
|
|
9
|
+
* Returns current scope.
|
|
10
|
+
*
|
|
11
|
+
* @version 2.0.0
|
|
12
|
+
*/ export const currentScope$ = createCurrentScope$(context);
|
|
13
|
+
/**
|
|
14
|
+
* Creates an instance of token, using and storing in scope.
|
|
15
|
+
*
|
|
16
|
+
* @version 1.0.0
|
|
17
|
+
*/ export const inject$ = createInject$(context);
|
|
18
|
+
/**
|
|
19
|
+
* Runs a function in a given scope. Any {@link inject$} calls within the function will use the provided scope by default.
|
|
20
|
+
*
|
|
21
|
+
* @version 2.0.0
|
|
22
|
+
*/ export const runIn$ = createRunIn$(context);
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new {@link ChildScope}, with a given parent defaulting to current scope
|
|
25
|
+
*
|
|
26
|
+
* @version 1.0.0
|
|
27
|
+
*/ export const scope$ = createScope$(context);
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=node.js.map
|
package/dist/node.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/node.ts"],"sourcesContent":["import { createCurrentScope$ } from './current-scope.js';\nimport { createInject$ } from './inject.js';\nimport { NodeContext } from './internals/NodeContext.js';\nimport { createRunIn$ } from './run-in.js';\nimport { createScope$ } from './scope.js';\n\nconst context = new NodeContext();\n\nexport * from './common.js';\n\n/**\n * Returns current scope.\n *\n * @version 2.0.0\n */\nexport const currentScope$ = createCurrentScope$(context);\n\n/**\n * Creates an instance of token, using and storing in scope.\n *\n * @version 1.0.0\n */\nexport const inject$ = createInject$(context);\n\n/**\n * Runs a function in a given scope. Any {@link inject$} calls within the function will use the provided scope by default.\n *\n * @version 2.0.0\n */\nexport const runIn$ = createRunIn$(context);\n\n/**\n * Creates a new {@link ChildScope}, with a given parent defaulting to current scope\n *\n * @version 1.0.0\n */\nexport const scope$ = createScope$(context);"],"names":["createCurrentScope$","createInject$","NodeContext","createRunIn$","createScope$","context","currentScope$","inject$","runIn$","scope$"],"mappings":"AAAA,SAASA,mBAAmB,QAAQ,qBAAqB;AACzD,SAASC,aAAa,QAAQ,cAAc;AAC5C,SAASC,WAAW,QAAQ,6BAA6B;AACzD,SAASC,YAAY,QAAQ,cAAc;AAC3C,SAASC,YAAY,QAAQ,aAAa;AAE1C,MAAMC,UAAU,IAAIH;AAEpB,cAAc,cAAc;AAE5B;;;;CAIC,GACD,OAAO,MAAMI,gBAAgBN,oBAAoBK,SAAS;AAE1D;;;;CAIC,GACD,OAAO,MAAME,UAAUN,cAAcI,SAAS;AAE9C;;;;CAIC,GACD,OAAO,MAAMG,SAASL,aAAaE,SAAS;AAE5C;;;;CAIC,GACD,OAAO,MAAMI,SAASL,aAAaC,SAAS"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new {@link RootScope}
|
|
3
|
+
*
|
|
4
|
+
* @version 2.0.0
|
|
5
|
+
*/ export function rootScope$(name) {
|
|
6
|
+
const map = new Map();
|
|
7
|
+
return {
|
|
8
|
+
name,
|
|
9
|
+
get: (token)=>map.get(token),
|
|
10
|
+
set: (token, value)=>map.set(token, value),
|
|
11
|
+
clear: ()=>map.clear()
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=root-scope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/root-scope.ts"],"sourcesContent":["import type { RootScope, Token } from './defs/index.js';\n\n/**\n * Returns a new {@link RootScope}\n *\n * @version 2.0.0\n */\nexport function rootScope$(name: string): RootScope {\n const map = new Map();\n\n return {\n name,\n get: <T>(token: Token<T>) => map.get(token) as T,\n set: (token, value) => map.set(token, value),\n clear: () => map.clear(),\n };\n}\n"],"names":["rootScope$","name","map","Map","get","token","set","value","clear"],"mappings":"AAEA;;;;CAIC,GACD,OAAO,SAASA,WAAWC,IAAY;IACrC,MAAMC,MAAM,IAAIC;IAEhB,OAAO;QACLF;QACAG,KAAK,CAAIC,QAAoBH,IAAIE,GAAG,CAACC;QACrCC,KAAK,CAACD,OAAOE,QAAUL,IAAII,GAAG,CAACD,OAAOE;QACtCC,OAAO,IAAMN,IAAIM,KAAK;IACxB;AACF"}
|
package/dist/run-in.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/run-in.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/run-in.ts"],"sourcesContent":["import type { Scope } from './defs/index.js';\nimport type { InjectionContext } from './internals/InjectionContext.js';\n\n/** @internal */\nexport function createRunIn$(context: InjectionContext) {\n return function runIn$<T>(scope: Scope, fn: () => T): T {\n return context.runInScope(scope, fn);\n };\n}"],"names":["createRunIn$","context","runIn$","scope","fn","runInScope"],"mappings":"AAGA,cAAc,GACd,OAAO,SAASA,aAAaC,OAAyB;IACpD,OAAO,SAASC,OAAUC,KAAY,EAAEC,EAAW;QACjD,OAAOH,QAAQI,UAAU,CAACF,OAAOC;IACnC;AACF"}
|
package/dist/scope.d.ts
CHANGED
package/dist/scope.js
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
},
|
|
18
|
-
set: (token, value)=>map.set(token, value),
|
|
19
|
-
clear: ()=>map.clear()
|
|
1
|
+
/** @internal */ export function createScope$(context) {
|
|
2
|
+
return function scope$(name, parent = context.getScope()) {
|
|
3
|
+
const map = new Map();
|
|
4
|
+
return {
|
|
5
|
+
name,
|
|
6
|
+
parentScope: parent,
|
|
7
|
+
get (token) {
|
|
8
|
+
let value = map.get(token);
|
|
9
|
+
if (value === undefined) {
|
|
10
|
+
value = parent.get(token);
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
},
|
|
14
|
+
set: (token, value)=>map.set(token, value),
|
|
15
|
+
clear: ()=>map.clear()
|
|
16
|
+
};
|
|
20
17
|
};
|
|
21
18
|
}
|
|
22
19
|
|
package/dist/scope.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/scope.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../src/scope.ts"],"sourcesContent":["import type { InjectionContext } from './internals/InjectionContext.js';\nimport type { ChildScope, Scope, Token } from './defs/index.js';\n\n/** @internal */\nexport function createScope$(context: InjectionContext) {\n return function scope$(name: string, parent: Scope = context.getScope()): ChildScope {\n const map = new Map<Token, unknown>();\n\n return {\n name,\n parentScope: parent,\n get<T>(token: Token<T>) {\n let value = map.get(token) as T | undefined;\n\n if (value === undefined) {\n value = parent.get(token);\n }\n\n return value;\n },\n set: (token, value) => map.set(token, value),\n clear: () => map.clear(),\n };\n };\n}"],"names":["createScope$","context","scope$","name","parent","getScope","map","Map","parentScope","get","token","value","undefined","set","clear"],"mappings":"AAGA,cAAc,GACd,OAAO,SAASA,aAAaC,OAAyB;IACpD,OAAO,SAASC,OAAOC,IAAY,EAAEC,SAAgBH,QAAQI,QAAQ,EAAE;QACrE,MAAMC,MAAM,IAAIC;QAEhB,OAAO;YACLJ;YACAK,aAAaJ;YACbK,KAAOC,KAAe;gBACpB,IAAIC,QAAQL,IAAIG,GAAG,CAACC;gBAEpB,IAAIC,UAAUC,WAAW;oBACvBD,QAAQP,OAAOK,GAAG,CAACC;gBACrB;gBAEA,OAAOC;YACT;YACAE,KAAK,CAACH,OAAOC,QAAUL,IAAIO,GAAG,CAACH,OAAOC;YACtCG,OAAO,IAAMR,IAAIQ,KAAK;QACxB;IACF;AACF"}
|
package/dist/token.d.ts
CHANGED
package/dist/token.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates an injectable token, using init as instance creator.
|
|
3
|
-
*
|
|
4
|
-
* @version 1.0.0
|
|
1
|
+
/**
|
|
2
|
+
* Creates an injectable token, using init as an instance creator.
|
|
3
|
+
*
|
|
4
|
+
* @version 1.0.0
|
|
5
5
|
*/ export function token$(name, init) {
|
|
6
6
|
return Object.assign(Symbol(name), {
|
|
7
7
|
inject: init
|
package/dist/token.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/token.ts"],"sourcesContent":["import { type Scope, type SymbolToken } from './defs/index.js';\
|
|
1
|
+
{"version":3,"sources":["../src/token.ts"],"sourcesContent":["import { type Scope, type SymbolToken } from './defs/index.js';\n\n/**\n * Creates an injectable token, using init as an instance creator.\n *\n * @version 1.0.0\n */\nexport function token$<T>(name: string, init: TokenInitCallback<T>): SymbolToken<T> {\n return Object.assign(Symbol(name), {\n inject: init,\n });\n}\n\nexport type TokenInitCallback<out T> = (scope: Scope) => T;\n"],"names":["token$","name","init","Object","assign","Symbol","inject"],"mappings":"AAEA;;;;CAIC,GACD,OAAO,SAASA,OAAUC,IAAY,EAAEC,IAA0B;IAChE,OAAOC,OAAOC,MAAM,CAACC,OAAOJ,OAAO;QACjCK,QAAQJ;IACV;AACF"}
|