@joist/di 4.0.0-next.5 → 4.0.0-next.50
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 +78 -38
- package/package.json +4 -7
- package/src/lib/context/injector.ts +5 -0
- package/src/lib/context/protocol.ts +78 -0
- package/src/lib/dom-injector.test.ts +53 -17
- package/src/lib/dom-injector.ts +48 -5
- package/src/lib/inject.test.ts +36 -22
- package/src/lib/inject.ts +6 -7
- package/src/lib/injectable-el.test.ts +29 -29
- package/src/lib/injectable-el.ts +53 -47
- package/src/lib/injectable.test.ts +47 -9
- package/src/lib/injectable.ts +45 -16
- package/src/lib/injector.test.ts +78 -64
- package/src/lib/injector.ts +54 -58
- package/src/lib/lifecycle.test.ts +70 -35
- package/src/lib/lifecycle.ts +33 -10
- package/src/lib/metadata.ts +17 -0
- package/src/lib/provider.ts +19 -11
- package/src/lib.ts +11 -6
- package/target/lib/context/injector.d.ts +3 -0
- package/target/lib/context/injector.js +3 -0
- package/target/lib/context/injector.js.map +1 -0
- package/target/lib/context/protocol.d.ts +18 -0
- package/target/lib/context/protocol.js +15 -0
- package/target/lib/context/protocol.js.map +1 -0
- package/target/lib/dom-injector.d.ts +5 -3
- package/target/lib/dom-injector.js +30 -5
- package/target/lib/dom-injector.js.map +1 -1
- package/target/lib/dom-injector.test.js +41 -17
- package/target/lib/dom-injector.test.js.map +1 -1
- package/target/lib/inject.d.ts +1 -1
- package/target/lib/inject.js +2 -3
- package/target/lib/inject.js.map +1 -1
- package/target/lib/inject.test.js +33 -22
- package/target/lib/inject.test.js.map +1 -1
- package/target/lib/injectable-el.d.ts +2 -334
- package/target/lib/injectable-el.js +39 -31
- package/target/lib/injectable-el.js.map +1 -1
- package/target/lib/injectable-el.test.js +29 -29
- package/target/lib/injectable-el.test.js.map +1 -1
- package/target/lib/injectable.d.ts +5 -7
- package/target/lib/injectable.js +26 -12
- package/target/lib/injectable.js.map +1 -1
- package/target/lib/injectable.test.js +82 -8
- package/target/lib/injectable.test.js.map +1 -1
- package/target/lib/injector.d.ts +10 -5
- package/target/lib/injector.js +26 -42
- package/target/lib/injector.js.map +1 -1
- package/target/lib/injector.test.js +75 -60
- package/target/lib/injector.test.js.map +1 -1
- package/target/lib/lifecycle.d.ts +5 -13
- package/target/lib/lifecycle.js +22 -6
- package/target/lib/lifecycle.js.map +1 -1
- package/target/lib/lifecycle.test.js +101 -38
- package/target/lib/lifecycle.test.js.map +1 -1
- package/target/lib/metadata.d.ts +8 -0
- package/target/lib/metadata.js +5 -0
- package/target/lib/metadata.js.map +1 -0
- package/target/lib/provider.d.ts +11 -8
- package/target/lib/provider.js +1 -0
- package/target/lib/provider.js.map +1 -1
- package/target/lib.d.ts +6 -6
- package/target/lib.js +6 -6
- package/target/lib.js.map +1 -1
package/README.md
CHANGED
|
@@ -16,10 +16,10 @@ Allows you to inject services into other class instances (including custom eleme
|
|
|
16
16
|
- [Hierarchical Injectors](#hierarchical-injectors)
|
|
17
17
|
- [Custom Elements](#custom-elements)
|
|
18
18
|
|
|
19
|
-
## Installation
|
|
19
|
+
## Installation
|
|
20
20
|
|
|
21
21
|
```BASH
|
|
22
|
-
npm i @joist/di
|
|
22
|
+
npm i @joist/di@next
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
## Injectors
|
|
@@ -102,7 +102,9 @@ test('should return json', async () => {
|
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
const app = new Injector(
|
|
105
|
+
const app = new Injector({
|
|
106
|
+
providers: [[HttpService, { use: MockHttpService }]]
|
|
107
|
+
});
|
|
106
108
|
const api = app.inject(ApiService);
|
|
107
109
|
|
|
108
110
|
const res = await api.getData();
|
|
@@ -130,7 +132,7 @@ class ConsoleLogger implements Logger {
|
|
|
130
132
|
}
|
|
131
133
|
|
|
132
134
|
@injectable({
|
|
133
|
-
providers: [
|
|
135
|
+
providers: [[Logger, { use: ConsoleLogger }]]
|
|
134
136
|
})
|
|
135
137
|
class MyService {}
|
|
136
138
|
```
|
|
@@ -180,14 +182,16 @@ class Feature {
|
|
|
180
182
|
}
|
|
181
183
|
|
|
182
184
|
const app = new Injector([
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
185
|
+
[
|
|
186
|
+
Feature,
|
|
187
|
+
{
|
|
188
|
+
factory(i) {
|
|
189
|
+
const logger = i.inject(Logger);
|
|
187
190
|
|
|
188
|
-
|
|
191
|
+
return new Feature(logger);
|
|
192
|
+
}
|
|
189
193
|
}
|
|
190
|
-
|
|
194
|
+
]
|
|
191
195
|
]);
|
|
192
196
|
```
|
|
193
197
|
|
|
@@ -200,10 +204,12 @@ In most cases a token is any constructable class. There are cases where you migh
|
|
|
200
204
|
const URL_TOKEN = new StaticToken<string>('app_url');
|
|
201
205
|
|
|
202
206
|
const app = new Injector([
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
+
[
|
|
208
|
+
URL_TOKEN,
|
|
209
|
+
{
|
|
210
|
+
factory: () => '/my-app-url/'
|
|
211
|
+
}
|
|
212
|
+
]
|
|
207
213
|
]);
|
|
208
214
|
```
|
|
209
215
|
|
|
@@ -228,17 +234,43 @@ const app = new Injector();
|
|
|
228
234
|
const url: string = await app.inject(URL_TOKEN);
|
|
229
235
|
```
|
|
230
236
|
|
|
237
|
+
This allows you to dynamically import services
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
const HttpService = new StaticToken('HTTP_SERVICE', () => {
|
|
241
|
+
return import('./http.service.js').then((m) => new m.HttpService());
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
class HackerNewsService {
|
|
245
|
+
#http = inject(HttpService);
|
|
246
|
+
|
|
247
|
+
async getData() {
|
|
248
|
+
const http = await this.#http();
|
|
249
|
+
|
|
250
|
+
const url = new URL('https://hacker-news.firebaseio.com/v0/beststories.json');
|
|
251
|
+
url.searchParams.set('limitToFirst', count.toString());
|
|
252
|
+
url.searchParams.set('orderBy', '"$key"');
|
|
253
|
+
|
|
254
|
+
return http.fetchJson<string[]>(url);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const url: string = await app.inject(URL_TOKEN);
|
|
259
|
+
```
|
|
260
|
+
|
|
231
261
|
## LifeCycle
|
|
232
262
|
|
|
233
263
|
To help provide more information to services that are being created, joist will call several life cycle hooks as services are created. These hooks are defined using the provided symbols so there is no risk of naming colisions.
|
|
234
264
|
|
|
235
265
|
```ts
|
|
236
266
|
class MyService {
|
|
237
|
-
|
|
267
|
+
@created()
|
|
268
|
+
onCreated() {
|
|
238
269
|
// called the first time a service is created. (not pulled from cache)
|
|
239
270
|
}
|
|
240
271
|
|
|
241
|
-
|
|
272
|
+
@injected()
|
|
273
|
+
onInjected() {
|
|
242
274
|
// called every time a service is returned, whether it is from cache or not
|
|
243
275
|
}
|
|
244
276
|
}
|
|
@@ -246,12 +278,16 @@ class MyService {
|
|
|
246
278
|
|
|
247
279
|
## Hierarchical Injectors
|
|
248
280
|
|
|
249
|
-
Injectors can be defined with a parent
|
|
281
|
+
Injectors can be defined with a parent. The top most parent will (by default) be where services are constructed and cached. Only if manually defined providers are found earlier in the chain will services be constructed lower. The injector resolution algorithm behaves as following.
|
|
250
282
|
|
|
251
283
|
1. Do I have a cached instance locally?
|
|
252
284
|
2. Do I have a local provider definition for the token?
|
|
253
|
-
3. Do I have a parent?
|
|
254
|
-
4.
|
|
285
|
+
3. Do I have a parent?
|
|
286
|
+
4. Does parent have a local instance or provider definition?
|
|
287
|
+
5. If parent exists but no instance found, create instance in parent.
|
|
288
|
+
6. If not parent, All clear, go ahead and construct and cache the requested service.
|
|
289
|
+
|
|
290
|
+
Having injectors resolve this way means that all children have access to services created by their parents.
|
|
255
291
|
|
|
256
292
|
```mermaid
|
|
257
293
|
graph TD
|
|
@@ -302,34 +338,38 @@ customElements.define('my-element', MyElement);
|
|
|
302
338
|
|
|
303
339
|
### Context Elements:
|
|
304
340
|
|
|
305
|
-
Context elements are where Hierarchical Injectors can really shine as they allow you to defined React/Preact esq "context" elements.
|
|
341
|
+
Context elements are where Hierarchical Injectors can really shine as they allow you to defined React/Preact esq "context" elements.
|
|
342
|
+
Since custom elements are treated the same as any other class they can define providers for their local scope. The `provideSelfAs` property will provide the current class for the tokens given.
|
|
343
|
+
This also makes it easy to attributes to define values for the service.
|
|
306
344
|
|
|
307
345
|
```TS
|
|
308
346
|
const app = new DOMInjector();
|
|
309
347
|
|
|
310
348
|
app.attach(document.body);
|
|
311
349
|
|
|
312
|
-
|
|
313
|
-
primary
|
|
314
|
-
|
|
350
|
+
interface ColorCtx {
|
|
351
|
+
primary: string;
|
|
352
|
+
secondary: string;
|
|
315
353
|
}
|
|
316
354
|
|
|
355
|
+
const COLOR_CTX = new StaticToken<ColorCtx>('COLOR_CTX')
|
|
356
|
+
|
|
317
357
|
@injectable({
|
|
318
|
-
|
|
319
|
-
{
|
|
320
|
-
provide: Colors,
|
|
321
|
-
use: class implements Colors {
|
|
322
|
-
primary = 'orange';
|
|
323
|
-
secondary = 'purple';
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
]
|
|
358
|
+
provideSelfAs: [COLOR_CTX]
|
|
327
359
|
})
|
|
328
|
-
class ColorCtx extends HTMLElement {
|
|
360
|
+
class ColorCtx extends HTMLElement implements ColorCtx {
|
|
361
|
+
get primary() {
|
|
362
|
+
return this.getAttribute("primary") ?? "red"
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
get secondary() {
|
|
366
|
+
return this.getAttribute("secondary") ?? "green"
|
|
367
|
+
}
|
|
368
|
+
}
|
|
329
369
|
|
|
330
370
|
@injectable()
|
|
331
371
|
class MyElement extends HTMLElement {
|
|
332
|
-
#colors = inject(
|
|
372
|
+
#colors = inject(COLOR_CTX);
|
|
333
373
|
|
|
334
374
|
connectedCallback() {
|
|
335
375
|
const { primary } = this.#colors();
|
|
@@ -338,17 +378,17 @@ class MyElement extends HTMLElement {
|
|
|
338
378
|
}
|
|
339
379
|
}
|
|
340
380
|
|
|
341
|
-
// Note: To use parent providers, the parent elements need to be defined first
|
|
381
|
+
// Note: To use parent providers, the parent elements need to be defined first!
|
|
342
382
|
customElements.define('color-ctx', ColorCtx);
|
|
343
383
|
customElements.define('my-element', MyElement);
|
|
344
384
|
```
|
|
345
385
|
|
|
346
386
|
```HTML
|
|
347
|
-
<!--
|
|
387
|
+
<!-- Error: No colors provided -->
|
|
348
388
|
<my-element></my-element>
|
|
349
389
|
|
|
350
|
-
<!--
|
|
351
|
-
<color-ctx>
|
|
390
|
+
<!-- colors come from ctx -->
|
|
391
|
+
<color-ctx primary="orange" secondard="blue">
|
|
352
392
|
<my-element></my-element>
|
|
353
393
|
</color-ctx>
|
|
354
394
|
```
|
package/package.json
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@joist/di",
|
|
3
|
-
"version": "4.0.0-next.
|
|
3
|
+
"version": "4.0.0-next.50",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./target/lib.js",
|
|
6
6
|
"module": "./target/lib.js",
|
|
7
7
|
"exports": {
|
|
8
|
-
".":
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"./*": {
|
|
12
|
-
"import": "./target/lib/*"
|
|
13
|
-
}
|
|
8
|
+
".": "./target/lib.js",
|
|
9
|
+
"./*": "./target/lib/*",
|
|
10
|
+
"./package.json": "./package.json"
|
|
14
11
|
},
|
|
15
12
|
"files": [
|
|
16
13
|
"src",
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A context key.
|
|
3
|
+
*
|
|
4
|
+
* A context key can be any type of object, including strings and symbols. The
|
|
5
|
+
* Context type brands the key type with the `__context__` property that
|
|
6
|
+
* carries the type of the value the context references.
|
|
7
|
+
*/
|
|
8
|
+
export type Context<KeyType, ValueType> = KeyType & { __context__: ValueType };
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* An unknown context type
|
|
12
|
+
*/
|
|
13
|
+
export type UnknownContext = Context<unknown, unknown>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A helper type which can extract a Context value type from a Context type
|
|
17
|
+
*/
|
|
18
|
+
export type ContextType<T extends UnknownContext> = T extends Context<
|
|
19
|
+
infer _,
|
|
20
|
+
infer V
|
|
21
|
+
>
|
|
22
|
+
? V
|
|
23
|
+
: never;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A function which creates a Context value object
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export function createContext<KeyType, ValueType>(key: KeyType) {
|
|
30
|
+
return key as Context<KeyType, ValueType>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A callback which is provided by a context requester and is called with the value satisfying the request.
|
|
35
|
+
* This callback can be called multiple times by context providers as the requested value is changed.
|
|
36
|
+
*/
|
|
37
|
+
export type ContextCallback<ValueType> = (
|
|
38
|
+
value: ValueType,
|
|
39
|
+
unsubscribe?: () => void,
|
|
40
|
+
) => void;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* An event fired by a context requester to signal it desires a named context.
|
|
44
|
+
*
|
|
45
|
+
* A provider should inspect the `context` property of the event to determine if it has a value that can
|
|
46
|
+
* satisfy the request, calling the `callback` with the requested value if so.
|
|
47
|
+
*
|
|
48
|
+
* If the requested context event contains a truthy `subscribe` value, then a provider can call the callback
|
|
49
|
+
* multiple times if the value is changed, if this is the case the provider should pass an `unsubscribe`
|
|
50
|
+
* function to the callback which requesters can invoke to indicate they no longer wish to receive these updates.
|
|
51
|
+
*/
|
|
52
|
+
export class ContextRequestEvent<T extends UnknownContext> extends Event {
|
|
53
|
+
context: T;
|
|
54
|
+
callback: ContextCallback<ContextType<T>>;
|
|
55
|
+
subscribe?: boolean;
|
|
56
|
+
|
|
57
|
+
public constructor(
|
|
58
|
+
context: T,
|
|
59
|
+
callback: ContextCallback<ContextType<T>>,
|
|
60
|
+
subscribe?: boolean,
|
|
61
|
+
) {
|
|
62
|
+
super("context-request", { bubbles: true, composed: true });
|
|
63
|
+
|
|
64
|
+
this.context = context;
|
|
65
|
+
this.callback = callback;
|
|
66
|
+
this.subscribe = subscribe;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
declare global {
|
|
71
|
+
interface HTMLElementEventMap {
|
|
72
|
+
/**
|
|
73
|
+
* A 'context-request' event can be emitted by any element which desires
|
|
74
|
+
* a context value to be injected by an external provider.
|
|
75
|
+
*/
|
|
76
|
+
"context-request": ContextRequestEvent<Context<unknown, unknown>>;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -1,28 +1,64 @@
|
|
|
1
|
-
import { assert } from
|
|
1
|
+
import { assert } from "chai";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { INJECTOR_CTX } from "./context/injector.js";
|
|
4
|
+
import {
|
|
5
|
+
ContextRequestEvent,
|
|
6
|
+
type UnknownContext,
|
|
7
|
+
} from "./context/protocol.js";
|
|
8
|
+
import { DOMInjector } from "./dom-injector.js";
|
|
9
|
+
import { Injector } from "./injector.js";
|
|
5
10
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
describe("DOMInjector", () => {
|
|
12
|
+
it("should respond to elements looking for an injector", () => {
|
|
13
|
+
const injector = new DOMInjector();
|
|
14
|
+
injector.attach(document.body);
|
|
9
15
|
|
|
10
|
-
|
|
16
|
+
const host = document.createElement("div");
|
|
17
|
+
document.body.append(host);
|
|
11
18
|
|
|
12
|
-
|
|
19
|
+
let parent: Injector | null = null;
|
|
13
20
|
|
|
14
|
-
|
|
15
|
-
|
|
21
|
+
host.dispatchEvent(
|
|
22
|
+
new ContextRequestEvent(INJECTOR_CTX, (i) => {
|
|
23
|
+
parent = i;
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
assert.equal(parent, injector);
|
|
28
|
+
|
|
29
|
+
injector.detach();
|
|
30
|
+
host.remove();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should send request looking for other injector contexts", () => {
|
|
34
|
+
const parent = new Injector();
|
|
35
|
+
const injector = new DOMInjector();
|
|
36
|
+
|
|
37
|
+
const cb = (e: ContextRequestEvent<UnknownContext>) => {
|
|
38
|
+
if (e.context === INJECTOR_CTX) {
|
|
39
|
+
e.callback(parent);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
document.body.addEventListener("context-request", cb);
|
|
44
|
+
|
|
45
|
+
injector.attach(document.body);
|
|
46
|
+
|
|
47
|
+
assert.equal(injector.parent, parent);
|
|
16
48
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
49
|
+
injector.detach();
|
|
50
|
+
document.body.removeEventListener("context-request", cb);
|
|
51
|
+
});
|
|
20
52
|
|
|
21
|
-
|
|
53
|
+
it("should throw an error if attempting to attach an already attached DOMInjector", () => {
|
|
54
|
+
const injector = new DOMInjector();
|
|
22
55
|
|
|
23
|
-
|
|
56
|
+
const el = document.createElement("div");
|
|
24
57
|
|
|
25
|
-
|
|
58
|
+
injector.attach(el);
|
|
26
59
|
|
|
27
|
-
|
|
60
|
+
assert.throw(() => {
|
|
61
|
+
injector.attach(el);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
28
64
|
});
|
package/src/lib/dom-injector.ts
CHANGED
|
@@ -1,11 +1,54 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { INJECTOR_CTX } from "./context/injector.js";
|
|
2
|
+
import {
|
|
3
|
+
ContextRequestEvent,
|
|
4
|
+
type UnknownContext,
|
|
5
|
+
} from "./context/protocol.js";
|
|
6
|
+
import { Injector } from "./injector.js";
|
|
2
7
|
|
|
3
8
|
export class DOMInjector extends Injector {
|
|
4
|
-
|
|
5
|
-
|
|
9
|
+
#element: HTMLElement | null = null;
|
|
10
|
+
#controller: AbortController | null = null;
|
|
11
|
+
|
|
12
|
+
get isAttached(): boolean {
|
|
13
|
+
return this.#element !== null && this.#controller !== null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
attach(element: HTMLElement): void {
|
|
17
|
+
if (this.isAttached) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`This DOMInjector is already attached to ${this.#element}. Detach first before attaching again`,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
this.#element = element;
|
|
24
|
+
this.#controller = new AbortController();
|
|
25
|
+
|
|
26
|
+
this.#element.addEventListener(
|
|
27
|
+
"context-request",
|
|
28
|
+
(e: ContextRequestEvent<UnknownContext>) => {
|
|
29
|
+
if (e.context === INJECTOR_CTX) {
|
|
30
|
+
if (e.target !== element) {
|
|
31
|
+
e.stopPropagation();
|
|
32
|
+
|
|
33
|
+
e.callback(this);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{ signal: this.#controller.signal },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
this.#element.dispatchEvent(
|
|
41
|
+
new ContextRequestEvent(INJECTOR_CTX, (parent) => {
|
|
42
|
+
this.parent = parent;
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
6
45
|
}
|
|
7
46
|
|
|
8
|
-
detach(
|
|
9
|
-
|
|
47
|
+
detach(): void {
|
|
48
|
+
if (this.#controller) {
|
|
49
|
+
this.#controller.abort();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.#element = null;
|
|
10
53
|
}
|
|
11
54
|
}
|
package/src/lib/inject.test.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { assert } from
|
|
1
|
+
import { assert } from "chai";
|
|
2
2
|
|
|
3
|
-
import { inject } from
|
|
4
|
-
import { injectable } from
|
|
5
|
-
import { Injector } from
|
|
6
|
-
import { StaticToken } from
|
|
3
|
+
import { inject } from "./inject.js";
|
|
4
|
+
import { injectable } from "./injectable.js";
|
|
5
|
+
import { Injector } from "./injector.js";
|
|
6
|
+
import { StaticToken } from "./provider.js";
|
|
7
7
|
|
|
8
|
-
it(
|
|
8
|
+
it("should throw error if called in constructor", () => {
|
|
9
9
|
assert.throws(() => {
|
|
10
10
|
class FooService {
|
|
11
|
-
value =
|
|
11
|
+
value = "1";
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
@injectable()
|
|
@@ -23,12 +23,22 @@ it('should throw error if called in constructor', () => {
|
|
|
23
23
|
const parent = new Injector();
|
|
24
24
|
|
|
25
25
|
parent.inject(BarService);
|
|
26
|
-
},
|
|
26
|
+
}, "BarService is either not injectable or a service is being called in the constructor.");
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
-
it(
|
|
29
|
+
it("should throw error if static token is unavailable", () => {
|
|
30
|
+
assert.throws(() => {
|
|
31
|
+
const TOKEN = new StaticToken("test");
|
|
32
|
+
|
|
33
|
+
const parent = new Injector();
|
|
34
|
+
|
|
35
|
+
parent.inject(TOKEN);
|
|
36
|
+
}, 'Provider not found for "test"');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should use the calling injector as parent", () => {
|
|
30
40
|
class FooService {
|
|
31
|
-
value =
|
|
41
|
+
value = "1";
|
|
32
42
|
}
|
|
33
43
|
|
|
34
44
|
@injectable()
|
|
@@ -36,25 +46,29 @@ it('should use the calling injector as parent', () => {
|
|
|
36
46
|
foo = inject(FooService);
|
|
37
47
|
}
|
|
38
48
|
|
|
39
|
-
const parent = new Injector(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
const parent = new Injector({
|
|
50
|
+
providers: [
|
|
51
|
+
[
|
|
52
|
+
FooService,
|
|
53
|
+
{
|
|
54
|
+
use: class extends FooService {
|
|
55
|
+
value = "100";
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
],
|
|
60
|
+
});
|
|
47
61
|
|
|
48
|
-
assert.strictEqual(parent.inject(BarService).foo().value,
|
|
62
|
+
assert.strictEqual(parent.inject(BarService).foo().value, "100");
|
|
49
63
|
});
|
|
50
64
|
|
|
51
|
-
it(
|
|
52
|
-
const TOKEN = new StaticToken(
|
|
65
|
+
it("should inject a static token", () => {
|
|
66
|
+
const TOKEN = new StaticToken("test", () => "Hello World");
|
|
53
67
|
|
|
54
68
|
@injectable()
|
|
55
69
|
class HelloWorld {
|
|
56
70
|
hello = inject(TOKEN);
|
|
57
71
|
}
|
|
58
72
|
|
|
59
|
-
assert.strictEqual(new HelloWorld().hello(),
|
|
73
|
+
assert.strictEqual(new HelloWorld().hello(), "Hello World");
|
|
60
74
|
});
|
package/src/lib/inject.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { injectables } from './injector.js';
|
|
1
|
+
import { injectables } from "./injector.js";
|
|
2
|
+
import type { InjectionToken } from "./provider.js";
|
|
4
3
|
|
|
5
4
|
export type Injected<T> = () => T;
|
|
6
5
|
|
|
7
|
-
export function inject<This extends object, T>(
|
|
6
|
+
export function inject<This extends object, T>(
|
|
7
|
+
token: InjectionToken<T>,
|
|
8
|
+
): Injected<T> {
|
|
8
9
|
return function (this: This) {
|
|
9
10
|
const injector = injectables.get(this);
|
|
10
11
|
|
|
11
12
|
if (injector === undefined) {
|
|
12
|
-
const name = Object.getPrototypeOf(this.constructor).name;
|
|
13
|
-
|
|
14
13
|
throw new Error(
|
|
15
|
-
`${name} is either not injectable or a service is being called in the constructor. \n Either add the @injectable() to your class or use the
|
|
14
|
+
`${this.constructor.name} is either not injectable or a service is being called in the constructor. \n Either add the @injectable() to your class or use the @injected callback method.`,
|
|
16
15
|
);
|
|
17
16
|
}
|
|
18
17
|
|