katagami 2.2.0 → 3.0.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.
- package/README.md +99 -66
- package/dist/container/index.d.ts +3 -80
- package/dist/disposable/index.d.ts +4 -4
- package/dist/index.cjs +110 -51
- package/dist/index.d.ts +1 -1
- package/dist/index.js +118 -31
- package/dist/lazy/index.d.ts +5 -7
- package/dist/scope/index.d.ts +1 -0
- package/package.json +3 -8
- package/dist/index-g50fxds1.js +0 -34
- package/dist/scope/index.cjs +0 -190
- package/dist/scope/index.js +0 -131
package/dist/scope/index.cjs
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
6
|
-
var __toCommonJS = (from) => {
|
|
7
|
-
var entry = __moduleCache.get(from), desc;
|
|
8
|
-
if (entry)
|
|
9
|
-
return entry;
|
|
10
|
-
entry = __defProp({}, "__esModule", { value: true });
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
12
|
-
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
13
|
-
get: () => from[key],
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
}));
|
|
16
|
-
__moduleCache.set(from, entry);
|
|
17
|
-
return entry;
|
|
18
|
-
};
|
|
19
|
-
var __export = (target, all) => {
|
|
20
|
-
for (var name in all)
|
|
21
|
-
__defProp(target, name, {
|
|
22
|
-
get: all[name],
|
|
23
|
-
enumerable: true,
|
|
24
|
-
configurable: true,
|
|
25
|
-
set: (newValue) => all[name] = () => newValue
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// src/scope/index.ts
|
|
30
|
-
var exports_scope = {};
|
|
31
|
-
__export(exports_scope, {
|
|
32
|
-
createScope: () => createScope,
|
|
33
|
-
Scope: () => Scope
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(exports_scope);
|
|
36
|
-
|
|
37
|
-
// src/error/index.ts
|
|
38
|
-
class ContainerError extends Error {
|
|
39
|
-
constructor(message) {
|
|
40
|
-
super(message);
|
|
41
|
-
this.name = "ContainerError";
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// src/internal.ts
|
|
46
|
-
var INTERNALS = Symbol("katagami.internals");
|
|
47
|
-
|
|
48
|
-
// src/resolver/index.ts
|
|
49
|
-
function tokenToString(token) {
|
|
50
|
-
if (typeof token === "function") {
|
|
51
|
-
return token.name || "anonymous function";
|
|
52
|
-
}
|
|
53
|
-
if (typeof token === "symbol") {
|
|
54
|
-
return token.toString();
|
|
55
|
-
}
|
|
56
|
-
return String(token);
|
|
57
|
-
}
|
|
58
|
-
function buildCircularPath(resolvingTokens, token) {
|
|
59
|
-
const path = [];
|
|
60
|
-
let found = false;
|
|
61
|
-
for (const t of resolvingTokens) {
|
|
62
|
-
if (t === token) {
|
|
63
|
-
found = true;
|
|
64
|
-
}
|
|
65
|
-
if (found) {
|
|
66
|
-
path.push(tokenToString(t));
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
path.push(tokenToString(token));
|
|
70
|
-
return path.join(" -> ");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// src/scope/index.ts
|
|
74
|
-
function createScope(source) {
|
|
75
|
-
const internals = source[INTERNALS];
|
|
76
|
-
if (internals.isDisposed()) {
|
|
77
|
-
throw new ContainerError("Cannot create a scope from a disposed container.");
|
|
78
|
-
}
|
|
79
|
-
return new Scope(internals.registrations, internals.singletonCache);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
class Scope {
|
|
83
|
-
registrations;
|
|
84
|
-
singletonCache;
|
|
85
|
-
scopedCache;
|
|
86
|
-
resolvingTokens;
|
|
87
|
-
disposed = false;
|
|
88
|
-
[INTERNALS];
|
|
89
|
-
constructor(registrations, singletonCache) {
|
|
90
|
-
this.registrations = registrations;
|
|
91
|
-
this.singletonCache = singletonCache;
|
|
92
|
-
this.scopedCache = new Map;
|
|
93
|
-
this.resolvingTokens = new Set;
|
|
94
|
-
this[INTERNALS] = {
|
|
95
|
-
isDisposed: () => this.disposed,
|
|
96
|
-
markDisposed: () => {
|
|
97
|
-
this.disposed = true;
|
|
98
|
-
},
|
|
99
|
-
ownCache: this.scopedCache,
|
|
100
|
-
registrations: this.registrations,
|
|
101
|
-
singletonCache: this.singletonCache
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
resolve(token) {
|
|
105
|
-
return this.resolveToken(token, true);
|
|
106
|
-
}
|
|
107
|
-
tryResolve(token) {
|
|
108
|
-
return this.resolveToken(token, false);
|
|
109
|
-
}
|
|
110
|
-
resolveAll(token) {
|
|
111
|
-
return this.resolveAllTokens(token, true);
|
|
112
|
-
}
|
|
113
|
-
tryResolveAll(token) {
|
|
114
|
-
return this.resolveAllTokens(token, false);
|
|
115
|
-
}
|
|
116
|
-
resolveToken(token, required) {
|
|
117
|
-
if (this.disposed) {
|
|
118
|
-
throw new ContainerError("Cannot resolve from a disposed scope.");
|
|
119
|
-
}
|
|
120
|
-
const registrations = this.registrations.get(token);
|
|
121
|
-
if (registrations === undefined || registrations.length === 0) {
|
|
122
|
-
if (required) {
|
|
123
|
-
throw new ContainerError(`Token "${tokenToString(token)}" is not registered.`);
|
|
124
|
-
}
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
const registration = registrations[registrations.length - 1];
|
|
128
|
-
const singletonCached = this.singletonCache.get(registration);
|
|
129
|
-
if (singletonCached !== undefined) {
|
|
130
|
-
return singletonCached;
|
|
131
|
-
}
|
|
132
|
-
const scopedCached = this.scopedCache.get(registration);
|
|
133
|
-
if (scopedCached !== undefined) {
|
|
134
|
-
return scopedCached;
|
|
135
|
-
}
|
|
136
|
-
if (this.resolvingTokens.has(token)) {
|
|
137
|
-
throw new ContainerError(`Circular dependency detected: ${buildCircularPath(this.resolvingTokens, token)}`);
|
|
138
|
-
}
|
|
139
|
-
this.resolvingTokens.add(token);
|
|
140
|
-
try {
|
|
141
|
-
const instance = registration.factory(this);
|
|
142
|
-
if (registration.lifetime === "singleton") {
|
|
143
|
-
this.singletonCache.set(registration, instance);
|
|
144
|
-
} else if (registration.lifetime === "scoped") {
|
|
145
|
-
this.scopedCache.set(registration, instance);
|
|
146
|
-
}
|
|
147
|
-
return instance;
|
|
148
|
-
} finally {
|
|
149
|
-
this.resolvingTokens.delete(token);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
resolveAllTokens(token, required) {
|
|
153
|
-
if (this.disposed) {
|
|
154
|
-
throw new ContainerError("Cannot resolve from a disposed scope.");
|
|
155
|
-
}
|
|
156
|
-
const registrations = this.registrations.get(token);
|
|
157
|
-
if (registrations === undefined || registrations.length === 0) {
|
|
158
|
-
if (required) {
|
|
159
|
-
throw new ContainerError(`Token "${tokenToString(token)}" is not registered.`);
|
|
160
|
-
}
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
if (this.resolvingTokens.has(token)) {
|
|
164
|
-
throw new ContainerError(`Circular dependency detected: ${buildCircularPath(this.resolvingTokens, token)}`);
|
|
165
|
-
}
|
|
166
|
-
this.resolvingTokens.add(token);
|
|
167
|
-
try {
|
|
168
|
-
return registrations.map((registration) => {
|
|
169
|
-
const reg = registration;
|
|
170
|
-
const singletonCached = this.singletonCache.get(registration);
|
|
171
|
-
if (singletonCached !== undefined) {
|
|
172
|
-
return singletonCached;
|
|
173
|
-
}
|
|
174
|
-
const scopedCached = this.scopedCache.get(registration);
|
|
175
|
-
if (scopedCached !== undefined) {
|
|
176
|
-
return scopedCached;
|
|
177
|
-
}
|
|
178
|
-
const instance = reg.factory(this);
|
|
179
|
-
if (reg.lifetime === "singleton") {
|
|
180
|
-
this.singletonCache.set(registration, instance);
|
|
181
|
-
} else if (reg.lifetime === "scoped") {
|
|
182
|
-
this.scopedCache.set(registration, instance);
|
|
183
|
-
}
|
|
184
|
-
return instance;
|
|
185
|
-
});
|
|
186
|
-
} finally {
|
|
187
|
-
this.resolvingTokens.delete(token);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
package/dist/scope/index.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ContainerError,
|
|
3
|
-
buildCircularPath,
|
|
4
|
-
tokenToString
|
|
5
|
-
} from "../index-g50fxds1.js";
|
|
6
|
-
import {
|
|
7
|
-
INTERNALS
|
|
8
|
-
} from "../index-jx8b52m0.js";
|
|
9
|
-
|
|
10
|
-
// src/scope/index.ts
|
|
11
|
-
function createScope(source) {
|
|
12
|
-
const internals = source[INTERNALS];
|
|
13
|
-
if (internals.isDisposed()) {
|
|
14
|
-
throw new ContainerError("Cannot create a scope from a disposed container.");
|
|
15
|
-
}
|
|
16
|
-
return new Scope(internals.registrations, internals.singletonCache);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
class Scope {
|
|
20
|
-
registrations;
|
|
21
|
-
singletonCache;
|
|
22
|
-
scopedCache;
|
|
23
|
-
resolvingTokens;
|
|
24
|
-
disposed = false;
|
|
25
|
-
[INTERNALS];
|
|
26
|
-
constructor(registrations, singletonCache) {
|
|
27
|
-
this.registrations = registrations;
|
|
28
|
-
this.singletonCache = singletonCache;
|
|
29
|
-
this.scopedCache = new Map;
|
|
30
|
-
this.resolvingTokens = new Set;
|
|
31
|
-
this[INTERNALS] = {
|
|
32
|
-
isDisposed: () => this.disposed,
|
|
33
|
-
markDisposed: () => {
|
|
34
|
-
this.disposed = true;
|
|
35
|
-
},
|
|
36
|
-
ownCache: this.scopedCache,
|
|
37
|
-
registrations: this.registrations,
|
|
38
|
-
singletonCache: this.singletonCache
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
resolve(token) {
|
|
42
|
-
return this.resolveToken(token, true);
|
|
43
|
-
}
|
|
44
|
-
tryResolve(token) {
|
|
45
|
-
return this.resolveToken(token, false);
|
|
46
|
-
}
|
|
47
|
-
resolveAll(token) {
|
|
48
|
-
return this.resolveAllTokens(token, true);
|
|
49
|
-
}
|
|
50
|
-
tryResolveAll(token) {
|
|
51
|
-
return this.resolveAllTokens(token, false);
|
|
52
|
-
}
|
|
53
|
-
resolveToken(token, required) {
|
|
54
|
-
if (this.disposed) {
|
|
55
|
-
throw new ContainerError("Cannot resolve from a disposed scope.");
|
|
56
|
-
}
|
|
57
|
-
const registrations = this.registrations.get(token);
|
|
58
|
-
if (registrations === undefined || registrations.length === 0) {
|
|
59
|
-
if (required) {
|
|
60
|
-
throw new ContainerError(`Token "${tokenToString(token)}" is not registered.`);
|
|
61
|
-
}
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
const registration = registrations[registrations.length - 1];
|
|
65
|
-
const singletonCached = this.singletonCache.get(registration);
|
|
66
|
-
if (singletonCached !== undefined) {
|
|
67
|
-
return singletonCached;
|
|
68
|
-
}
|
|
69
|
-
const scopedCached = this.scopedCache.get(registration);
|
|
70
|
-
if (scopedCached !== undefined) {
|
|
71
|
-
return scopedCached;
|
|
72
|
-
}
|
|
73
|
-
if (this.resolvingTokens.has(token)) {
|
|
74
|
-
throw new ContainerError(`Circular dependency detected: ${buildCircularPath(this.resolvingTokens, token)}`);
|
|
75
|
-
}
|
|
76
|
-
this.resolvingTokens.add(token);
|
|
77
|
-
try {
|
|
78
|
-
const instance = registration.factory(this);
|
|
79
|
-
if (registration.lifetime === "singleton") {
|
|
80
|
-
this.singletonCache.set(registration, instance);
|
|
81
|
-
} else if (registration.lifetime === "scoped") {
|
|
82
|
-
this.scopedCache.set(registration, instance);
|
|
83
|
-
}
|
|
84
|
-
return instance;
|
|
85
|
-
} finally {
|
|
86
|
-
this.resolvingTokens.delete(token);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
resolveAllTokens(token, required) {
|
|
90
|
-
if (this.disposed) {
|
|
91
|
-
throw new ContainerError("Cannot resolve from a disposed scope.");
|
|
92
|
-
}
|
|
93
|
-
const registrations = this.registrations.get(token);
|
|
94
|
-
if (registrations === undefined || registrations.length === 0) {
|
|
95
|
-
if (required) {
|
|
96
|
-
throw new ContainerError(`Token "${tokenToString(token)}" is not registered.`);
|
|
97
|
-
}
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
if (this.resolvingTokens.has(token)) {
|
|
101
|
-
throw new ContainerError(`Circular dependency detected: ${buildCircularPath(this.resolvingTokens, token)}`);
|
|
102
|
-
}
|
|
103
|
-
this.resolvingTokens.add(token);
|
|
104
|
-
try {
|
|
105
|
-
return registrations.map((registration) => {
|
|
106
|
-
const reg = registration;
|
|
107
|
-
const singletonCached = this.singletonCache.get(registration);
|
|
108
|
-
if (singletonCached !== undefined) {
|
|
109
|
-
return singletonCached;
|
|
110
|
-
}
|
|
111
|
-
const scopedCached = this.scopedCache.get(registration);
|
|
112
|
-
if (scopedCached !== undefined) {
|
|
113
|
-
return scopedCached;
|
|
114
|
-
}
|
|
115
|
-
const instance = reg.factory(this);
|
|
116
|
-
if (reg.lifetime === "singleton") {
|
|
117
|
-
this.singletonCache.set(registration, instance);
|
|
118
|
-
} else if (reg.lifetime === "scoped") {
|
|
119
|
-
this.scopedCache.set(registration, instance);
|
|
120
|
-
}
|
|
121
|
-
return instance;
|
|
122
|
-
});
|
|
123
|
-
} finally {
|
|
124
|
-
this.resolvingTokens.delete(token);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
export {
|
|
129
|
-
createScope,
|
|
130
|
-
Scope
|
|
131
|
-
};
|