@lppedd/di-wise-neo 0.3.0

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.
Files changed (42) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +488 -0
  3. package/dist/cjs/index.d.ts +772 -0
  4. package/dist/cjs/index.js +1055 -0
  5. package/dist/cjs/index.js.map +1 -0
  6. package/dist/es/index.d.mts +772 -0
  7. package/dist/es/index.mjs +1037 -0
  8. package/dist/es/index.mjs.map +1 -0
  9. package/package.json +77 -0
  10. package/src/container.ts +292 -0
  11. package/src/decorators/autoRegister.ts +24 -0
  12. package/src/decorators/decorators.ts +47 -0
  13. package/src/decorators/index.ts +7 -0
  14. package/src/decorators/inject.ts +42 -0
  15. package/src/decorators/injectAll.ts +45 -0
  16. package/src/decorators/injectable.ts +61 -0
  17. package/src/decorators/optional.ts +39 -0
  18. package/src/decorators/optionalAll.ts +42 -0
  19. package/src/decorators/scoped.ts +32 -0
  20. package/src/defaultContainer.ts +519 -0
  21. package/src/errors.ts +47 -0
  22. package/src/index.ts +16 -0
  23. package/src/inject.ts +88 -0
  24. package/src/injectAll.ts +21 -0
  25. package/src/injectionContext.ts +46 -0
  26. package/src/injector.ts +117 -0
  27. package/src/metadata.ts +41 -0
  28. package/src/middleware.ts +85 -0
  29. package/src/optional.ts +65 -0
  30. package/src/optionalAll.ts +19 -0
  31. package/src/provider.ts +61 -0
  32. package/src/scope.ts +8 -0
  33. package/src/token.ts +84 -0
  34. package/src/tokenRegistry.ts +165 -0
  35. package/src/tokensRef.ts +46 -0
  36. package/src/utils/context.ts +19 -0
  37. package/src/utils/disposable.ts +10 -0
  38. package/src/utils/invariant.ts +6 -0
  39. package/src/utils/keyedStack.ts +26 -0
  40. package/src/utils/typeName.ts +28 -0
  41. package/src/utils/weakRefMap.ts +28 -0
  42. package/src/valueRef.ts +3 -0
@@ -0,0 +1,46 @@
1
+ import { assert } from "./errors";
2
+ import type { Token, Tokens } from "./token";
3
+
4
+ export interface TokensRef<Value = any> {
5
+ readonly getRefTokens: () => Set<Token<Value>>;
6
+ }
7
+
8
+ export interface TokenRef<Value = any> {
9
+ readonly getRefToken: () => Token<Value>;
10
+ }
11
+
12
+ /**
13
+ * Allows referencing a token that is declared later in the file by wrapping it in a function.
14
+ */
15
+ export function forwardRef<Value>(token: () => Tokens<Value>): TokensRef<Value>;
16
+ export function forwardRef<Value>(token: () => Token<Value>): TokenRef<Value>;
17
+ export function forwardRef<Value>(
18
+ token: () => Token<Value> | Tokens<Value>,
19
+ ): TokensRef<Value> & TokenRef<Value> {
20
+ return {
21
+ getRefTokens: (): Set<Token<Value>> => {
22
+ // Normalize the single token here, so that we don't have
23
+ // to do it at every getRefTokens call site
24
+ const tokenOrTokens = token();
25
+ const tokensArray = Array.isArray(tokenOrTokens) ? tokenOrTokens : [tokenOrTokens];
26
+ return new Set(tokensArray);
27
+ },
28
+ getRefToken: () => {
29
+ const tokenOrTokens = token();
30
+ assert(!Array.isArray(tokenOrTokens), "internal error: ref tokens should be a single token");
31
+ return tokenOrTokens;
32
+ },
33
+ };
34
+ }
35
+
36
+ // @internal
37
+ export function isTokensRef(value: any): value is TokensRef {
38
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
39
+ return value && typeof value === "object" && typeof value.getRefTokens === "function";
40
+ }
41
+
42
+ // @internal
43
+ export function isTokenRef(value: any): value is TokenRef {
44
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
45
+ return value && typeof value === "object" && typeof value.getRefToken === "function";
46
+ }
@@ -0,0 +1,19 @@
1
+ // @internal
2
+ export function createInjectionContext<T extends {}>(): readonly [
3
+ (next: T) => () => T | null,
4
+ () => T | null,
5
+ ] {
6
+ let current: T | null = null;
7
+
8
+ function provide(next: T): () => T | null {
9
+ const prev = current;
10
+ current = next;
11
+ return () => (current = prev);
12
+ }
13
+
14
+ function use(): T | null {
15
+ return current;
16
+ }
17
+
18
+ return [provide, use] as const;
19
+ }
@@ -0,0 +1,10 @@
1
+ // @internal
2
+ export interface Disposable {
3
+ dispose(): void;
4
+ }
5
+
6
+ // @internal
7
+ export function isDisposable(value: any): value is Disposable {
8
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
9
+ return value && typeof value === "object" && typeof value.dispose === "function";
10
+ }
@@ -0,0 +1,6 @@
1
+ // @internal
2
+ export function invariant(condition: unknown): asserts condition {
3
+ if (!condition) {
4
+ throw new Error("invariant violation");
5
+ }
6
+ }
@@ -0,0 +1,26 @@
1
+ import { invariant } from "./invariant";
2
+
3
+ // @internal
4
+ export class KeyedStack<K extends object, V> {
5
+ private readonly myEntries = new Array<{ key: K; value: V }>();
6
+ private readonly myKeys = new WeakSet<K>();
7
+
8
+ has(key: K): boolean {
9
+ return this.myKeys.has(key);
10
+ }
11
+
12
+ peek(): V | undefined {
13
+ const entry = this.myEntries.at(-1);
14
+ return entry?.value;
15
+ }
16
+
17
+ push(key: K, value: V): () => void {
18
+ invariant(!this.has(key));
19
+ this.myKeys.add(key);
20
+ this.myEntries.push({ key, value });
21
+ return () => {
22
+ this.myEntries.pop();
23
+ this.myKeys.delete(key);
24
+ };
25
+ }
26
+ }
@@ -0,0 +1,28 @@
1
+ // @internal
2
+ export function getTypeName(value: unknown): string {
3
+ if (typeof value == "string") {
4
+ return `"${value}"`;
5
+ }
6
+
7
+ if (typeof value == "function") {
8
+ return (value.name && `typeof ${value.name}`) || "Function";
9
+ }
10
+
11
+ if (typeof value == "object") {
12
+ if (value === null) {
13
+ return "null";
14
+ }
15
+
16
+ const proto: object | null = Object.getPrototypeOf(value);
17
+
18
+ if (proto && proto !== Object.prototype) {
19
+ const constructor: unknown = proto.constructor;
20
+
21
+ if (typeof constructor == "function" && constructor.name) {
22
+ return constructor.name;
23
+ }
24
+ }
25
+ }
26
+
27
+ return typeof value;
28
+ }
@@ -0,0 +1,28 @@
1
+ import { invariant } from "./invariant";
2
+
3
+ // @internal
4
+ export class WeakRefMap<K extends WeakKey, V extends object> {
5
+ private readonly myMap = new WeakMap<K, WeakRef<V>>();
6
+
7
+ get(key: K): V | undefined {
8
+ const ref = this.myMap.get(key);
9
+
10
+ if (ref) {
11
+ const value = ref.deref();
12
+
13
+ if (value) {
14
+ return value;
15
+ }
16
+
17
+ this.myMap.delete(key);
18
+ }
19
+ }
20
+
21
+ set(key: K, value: V): () => void {
22
+ invariant(!this.get(key));
23
+ this.myMap.set(key, new WeakRef(value));
24
+ return () => {
25
+ this.myMap.delete(key);
26
+ };
27
+ }
28
+ }
@@ -0,0 +1,3 @@
1
+ export interface ValueRef<T = any> {
2
+ readonly current: T;
3
+ }