@joist/di 2.0.0-next.8 → 2.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 CHANGED
@@ -1,6 +1,7 @@
1
1
  # Di
2
2
 
3
- Dependency Injection in ~800 bytes.
3
+ Dependency Injection in ~800 bytes. The Joist Dependency Injector is a small inversion of control (IOC) container that resolves dependencies lazyily.
4
+ This means that it passes functions around and that dependencies are not initialized untill they are called.
4
5
 
5
6
  #### Installation:
6
7
 
@@ -11,7 +12,7 @@ npm i @joist/di
11
12
  #### Example:
12
13
 
13
14
  ```TS
14
- import { Injector } from '@joist/di';
15
+ import { Injector, Injected } from '@joist/di';
15
16
 
16
17
  class FooService {
17
18
  sayHello() {
@@ -20,12 +21,12 @@ class FooService {
20
21
  }
21
22
 
22
23
  class BarService {
23
- static deps = [FooService];
24
+ static inject = [FooService];
24
25
 
25
- constructor(private foo: FooService) {}
26
+ constructor(private foo: Injected<FooService>) { }
26
27
 
27
28
  sayHello() {
28
- return this.foo.sayHello();
29
+ return this.foo().sayHello();
29
30
  }
30
31
  }
31
32
 
@@ -37,7 +38,7 @@ app.get(BarService).sayHello(); // Hello from BarService and Hello from FooServi
37
38
  #### Override A Service:
38
39
 
39
40
  ```TS
40
- import { Injector, inject } from '@joist/di';
41
+ import { Injector, inject, Injected } from '@joist/di';
41
42
 
42
43
  class FooService {
43
44
  sayHello() {
@@ -46,28 +47,26 @@ class FooService {
46
47
  }
47
48
 
48
49
  class BarService {
49
- static deps = [FooService];
50
+ static inject = [FooService];
50
51
 
51
- constructor(private foo: FooService) {}
52
+ constructor(private foo: Injected<FooService>) {}
52
53
 
53
54
  sayHello() {
54
- return 'Hello From BarService and ' + this.foo.sayHello();
55
+ return 'Hello From BarService and ' + this.foo().sayHello();
55
56
  }
56
57
  }
57
58
 
58
59
  // Override FooService with an alternate implementation
59
- const app = new Injector({
60
- providers: [
61
- {
62
- provide: FooService,
63
- use: class extends FooService {
64
- sayHello() {
65
- return 'IT HAS BEEN OVERRIDEN'
66
- }
60
+ const app = new Injector([
61
+ {
62
+ provide: FooService,
63
+ use: class extends FooService {
64
+ sayHello() {
65
+ return 'IT HAS BEEN OVERRIDEN'
67
66
  }
68
67
  }
69
- ]
70
- });
68
+ }
69
+ ]);
71
70
 
72
71
  app.get(BarService).sayHello(); // Hello from BarService and IT HAS BEEN OVERRIDEN
73
72
  ```
@@ -95,54 +94,110 @@ Since the browser will be what initializes your custom elements we need to be ab
95
94
 
96
95
  The `@injectable` decorator allows the Joist Dependency Injector to pass arguments to your custom element when instances of your element is created.
97
96
 
98
- `@injectable` is on required when you will not be able to manually create instances via an injector.
97
+ `@injectable` also injects your services in a lazy way. Instead of passing direct instances of your services it passes a function that be called when you need your service instance. This allows the injector to look for parent injectors which are only availabel after connectedCallback.
99
98
 
100
99
  #### Inject dependency into your custom element constructor
101
100
 
102
101
  ```TS
103
- import { inject, service, injectable } from '@joist/di';
102
+ import { injectable, Injected } from '@joist/di';
104
103
 
105
- @service
106
104
  class MyService {}
107
105
 
108
106
  @injectable
109
107
  class MyElement extends HTMLElement {
110
- static deps = [MyService];
108
+ static inject = [MyService];
111
109
 
112
- constructor(public myService: MyService) {}
110
+ constructor(public myService: Injected<MyService>) {}
113
111
  }
114
112
 
115
113
  customElements.define('my-element', MyElement);
116
114
  ```
117
115
 
118
- #### Define providers and overrides
116
+ #### Define global providers and overrides
119
117
 
120
118
  This allows your to override services for different environments or scenarios
121
119
 
122
120
  ```TS
123
- import { defineEnvironment, injectable } from '@joist/di';
121
+ import { environment, injectable, Injected } from '@joist/di';
124
122
 
125
123
  class Config {
126
124
  apiUrl = 'http://localhost:4000/api/'
127
125
  }
128
126
 
129
- defineEnvironment([
130
- {
131
- provide: Config,
132
- use: class extends Config {
133
- apiUrl = 'http://real-api/api/'
134
- }
127
+ environment().providers.push({
128
+ provide: Config,
129
+ use: class {
130
+ apiUrl = 'http://real-api/api/'
135
131
  }
136
- ]);
132
+ });
137
133
 
138
134
  @injectable
139
135
  class MyElement extends HTMLElement {
140
- static deps = [Config];
136
+ static inject = [Config];
141
137
 
142
- constructor(config: Config) {
143
- console.log(config.apiUrl); // http://real-api/api/
138
+ constructor(config: Injected<Config>) {
139
+ console.log(config().apiUrl); // http://real-api/api/
144
140
  }
145
141
  }
146
142
 
147
143
  customElements.define('my-element', MyElement);
148
144
  ```
145
+
146
+ ## Context
147
+
148
+ The Joist injector is hierarchical meaning that you can define context for just one part of the DOM tree.
149
+
150
+ ### NOTE:
151
+
152
+ When using context elements it is important that they are registered BEFORE your other elements.
153
+ If child elements are upgraded before the context element they won't be able to find the context scope.
154
+
155
+ ```TS
156
+ import { injectable, Injected } from '@joist/di';
157
+
158
+ class Colors {
159
+ primary = 'red';
160
+ secodnary = 'green';
161
+ }
162
+
163
+ @injectable
164
+ class ColorCtx extends HTMLElement {
165
+ static providers = [
166
+ {
167
+ provide: Colors,
168
+ use: class implements Colors {
169
+ primary = 'orange';
170
+ secondary = 'purple';
171
+ },
172
+ },
173
+ ];
174
+ }
175
+
176
+ @injectable
177
+ class MyElement extends HTMLElement {
178
+ static inject = [Colors];
179
+
180
+ constructor(public colors: Injected<Colors>) {
181
+ super();
182
+ }
183
+
184
+ connectedCallback() {
185
+ const { primary } = this.colors();
186
+
187
+ this.style.background = primary;
188
+ }
189
+ }
190
+
191
+ customElements.define('color-ctx', ColorCtx);
192
+ customElements.define('my-element', MyElement);
193
+ ```
194
+
195
+ ```HTML
196
+ <!-- Default Colors -->
197
+ <my-element></my-element>
198
+
199
+ <!-- Special color ctx -->
200
+ <color-ctx>
201
+ <my-element></my-element>
202
+ </color-ctx>
203
+ ```
package/package.json CHANGED
@@ -1,18 +1,33 @@
1
1
  {
2
2
  "name": "@joist/di",
3
- "version": "2.0.0-next.8",
3
+ "version": "2.0.0",
4
4
  "main": "./target/build/lib.js",
5
5
  "module": "./target/build/lib.js",
6
6
  "exports": {
7
7
  ".": {
8
8
  "import": "./target/build/lib.js"
9
+ },
10
+ "./environment.js": {
11
+ "import": "./target/build/lib/environment.js"
12
+ },
13
+ "./injectable.js": {
14
+ "import": "./target/build/lib/injectable.js"
15
+ },
16
+ "./injector.js": {
17
+ "import": "./target/build/lib/injector.js"
18
+ },
19
+ "./provider.js": {
20
+ "import": "./target/build/lib/provider.js"
21
+ },
22
+ "./service.js": {
23
+ "import": "./target/build/lib/service.js"
9
24
  }
10
25
  },
11
26
  "files": [
12
27
  "target/build"
13
28
  ],
14
29
  "sideEffects": false,
15
- "description": "Dependency Injection in ~800 bytes",
30
+ "description": "Dependency Injection for Vanilla JS classes",
16
31
  "repository": {
17
32
  "type": "git",
18
33
  "url": "git+https://github.com/deebloo/joist.git"
@@ -20,7 +35,8 @@
20
35
  "keywords": [
21
36
  "TypeScript",
22
37
  "DI",
23
- "Dependency Injection"
38
+ "Dependency Injection",
39
+ "WebComponents"
24
40
  ],
25
41
  "author": "deebloo",
26
42
  "license": "MIT",
@@ -34,5 +50,5 @@
34
50
  "test": "tsc -p tsconfig.test.json && wtr --config ../../wtr.config.mjs --port 8001",
35
51
  "build": "tsc -p tsconfig.build.json"
36
52
  },
37
- "gitHead": "1212ee3fec6c88bed79dc5ff97076c088f91c852"
53
+ "gitHead": "ed9e8f0cb4c6213ba255278a173c3dc7f45aee89"
38
54
  }
@@ -0,0 +1,3 @@
1
+ import { Injector } from '../injector';
2
+ export declare function environment(): Injector;
3
+ export declare function clearEnvironment(): void;
@@ -0,0 +1,9 @@
1
+ import { Injector } from '../injector';
2
+ const rootInjector = new Injector();
3
+ export function environment() {
4
+ return rootInjector;
5
+ }
6
+ export function clearEnvironment() {
7
+ rootInjector.providers = [];
8
+ rootInjector.instances = new WeakMap();
9
+ }
@@ -0,0 +1,317 @@
1
+ import { Provider, ProviderToken } from '../provider';
2
+ import { Injector } from '../injector';
3
+ export interface Injectable {
4
+ inject?: ProviderToken<any>[];
5
+ providers?: Provider<any>[];
6
+ new (...args: any[]): HTMLElement;
7
+ }
8
+ export declare function injectable<T extends Injectable>(CustomElement: T): {
9
+ new (...args: any[]): {
10
+ injector: Injector;
11
+ connectedCallback(): void;
12
+ disconnectedCallback(): void;
13
+ accessKey: string;
14
+ readonly accessKeyLabel: string;
15
+ autocapitalize: string;
16
+ dir: string;
17
+ draggable: boolean;
18
+ hidden: boolean;
19
+ innerText: string;
20
+ lang: string;
21
+ readonly offsetHeight: number;
22
+ readonly offsetLeft: number;
23
+ readonly offsetParent: Element | null;
24
+ readonly offsetTop: number;
25
+ readonly offsetWidth: number;
26
+ outerText: string;
27
+ spellcheck: boolean;
28
+ title: string;
29
+ translate: boolean;
30
+ attachInternals(): ElementInternals;
31
+ click(): void;
32
+ addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions | undefined): void;
33
+ addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined): void;
34
+ removeEventListener<K_1 extends keyof HTMLElementEventMap>(type: K_1, listener: (this: HTMLElement, ev: HTMLElementEventMap[K_1]) => any, options?: boolean | EventListenerOptions | undefined): void;
35
+ removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions | undefined): void;
36
+ readonly attributes: NamedNodeMap;
37
+ readonly classList: DOMTokenList;
38
+ className: string;
39
+ readonly clientHeight: number;
40
+ readonly clientLeft: number;
41
+ readonly clientTop: number;
42
+ readonly clientWidth: number;
43
+ id: string;
44
+ readonly localName: string;
45
+ readonly namespaceURI: string | null;
46
+ onfullscreenchange: ((this: Element, ev: Event) => any) | null;
47
+ onfullscreenerror: ((this: Element, ev: Event) => any) | null;
48
+ outerHTML: string;
49
+ readonly ownerDocument: Document;
50
+ readonly part: DOMTokenList;
51
+ readonly prefix: string | null;
52
+ readonly scrollHeight: number;
53
+ scrollLeft: number;
54
+ scrollTop: number;
55
+ readonly scrollWidth: number;
56
+ readonly shadowRoot: ShadowRoot | null;
57
+ slot: string;
58
+ readonly tagName: string;
59
+ attachShadow(init: ShadowRootInit): ShadowRoot;
60
+ closest<K_2 extends keyof HTMLElementTagNameMap>(selector: K_2): HTMLElementTagNameMap[K_2] | null;
61
+ closest<K_3 extends keyof SVGElementTagNameMap>(selector: K_3): SVGElementTagNameMap[K_3] | null;
62
+ closest<E extends Element = Element>(selectors: string): E | null;
63
+ getAttribute(qualifiedName: string): string | null;
64
+ getAttributeNS(namespace: string | null, localName: string): string | null;
65
+ getAttributeNames(): string[];
66
+ getAttributeNode(qualifiedName: string): Attr | null;
67
+ getAttributeNodeNS(namespace: string | null, localName: string): Attr | null;
68
+ getBoundingClientRect(): DOMRect;
69
+ getClientRects(): DOMRectList;
70
+ getElementsByClassName(classNames: string): HTMLCollectionOf<Element>;
71
+ getElementsByTagName<K_4 extends keyof HTMLElementTagNameMap>(qualifiedName: K_4): HTMLCollectionOf<HTMLElementTagNameMap[K_4]>;
72
+ getElementsByTagName<K_5 extends keyof SVGElementTagNameMap>(qualifiedName: K_5): HTMLCollectionOf<SVGElementTagNameMap[K_5]>;
73
+ getElementsByTagName(qualifiedName: string): HTMLCollectionOf<Element>;
74
+ getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollectionOf<HTMLElement>;
75
+ getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollectionOf<SVGElement>;
76
+ getElementsByTagNameNS(namespace: string | null, localName: string): HTMLCollectionOf<Element>;
77
+ hasAttribute(qualifiedName: string): boolean;
78
+ hasAttributeNS(namespace: string | null, localName: string): boolean;
79
+ hasAttributes(): boolean;
80
+ hasPointerCapture(pointerId: number): boolean;
81
+ insertAdjacentElement(where: InsertPosition, element: Element): Element | null;
82
+ insertAdjacentHTML(position: InsertPosition, text: string): void;
83
+ insertAdjacentText(where: InsertPosition, data: string): void;
84
+ matches(selectors: string): boolean;
85
+ releasePointerCapture(pointerId: number): void;
86
+ removeAttribute(qualifiedName: string): void;
87
+ removeAttributeNS(namespace: string | null, localName: string): void;
88
+ removeAttributeNode(attr: Attr): Attr;
89
+ requestFullscreen(options?: FullscreenOptions | undefined): Promise<void>;
90
+ requestPointerLock(): void;
91
+ scroll(options?: ScrollToOptions | undefined): void;
92
+ scroll(x: number, y: number): void;
93
+ scrollBy(options?: ScrollToOptions | undefined): void;
94
+ scrollBy(x: number, y: number): void;
95
+ scrollIntoView(arg?: boolean | ScrollIntoViewOptions | undefined): void;
96
+ scrollTo(options?: ScrollToOptions | undefined): void;
97
+ scrollTo(x: number, y: number): void;
98
+ setAttribute(qualifiedName: string, value: string): void;
99
+ setAttributeNS(namespace: string | null, qualifiedName: string, value: string): void;
100
+ setAttributeNode(attr: Attr): Attr | null;
101
+ setAttributeNodeNS(attr: Attr): Attr | null;
102
+ setPointerCapture(pointerId: number): void;
103
+ toggleAttribute(qualifiedName: string, force?: boolean | undefined): boolean;
104
+ webkitMatchesSelector(selectors: string): boolean;
105
+ readonly baseURI: string;
106
+ readonly childNodes: NodeListOf<ChildNode>;
107
+ readonly firstChild: ChildNode | null;
108
+ readonly isConnected: boolean;
109
+ readonly lastChild: ChildNode | null;
110
+ readonly nextSibling: ChildNode | null;
111
+ readonly nodeName: string;
112
+ readonly nodeType: number;
113
+ nodeValue: string | null;
114
+ readonly parentElement: HTMLElement | null;
115
+ readonly parentNode: ParentNode | null;
116
+ readonly previousSibling: ChildNode | null;
117
+ textContent: string | null;
118
+ appendChild<T_1 extends Node>(node: T_1): T_1;
119
+ cloneNode(deep?: boolean | undefined): Node;
120
+ compareDocumentPosition(other: Node): number;
121
+ contains(other: Node | null): boolean;
122
+ getRootNode(options?: GetRootNodeOptions | undefined): Node;
123
+ hasChildNodes(): boolean;
124
+ insertBefore<T_2 extends Node>(node: T_2, child: Node | null): T_2;
125
+ isDefaultNamespace(namespace: string | null): boolean;
126
+ isEqualNode(otherNode: Node | null): boolean;
127
+ isSameNode(otherNode: Node | null): boolean;
128
+ lookupNamespaceURI(prefix: string | null): string | null;
129
+ lookupPrefix(namespace: string | null): string | null;
130
+ normalize(): void;
131
+ removeChild<T_3 extends Node>(child: T_3): T_3;
132
+ replaceChild<T_4 extends Node>(node: Node, child: T_4): T_4;
133
+ readonly ATTRIBUTE_NODE: number;
134
+ readonly CDATA_SECTION_NODE: number;
135
+ readonly COMMENT_NODE: number;
136
+ readonly DOCUMENT_FRAGMENT_NODE: number;
137
+ readonly DOCUMENT_NODE: number;
138
+ readonly DOCUMENT_POSITION_CONTAINED_BY: number;
139
+ readonly DOCUMENT_POSITION_CONTAINS: number;
140
+ readonly DOCUMENT_POSITION_DISCONNECTED: number;
141
+ readonly DOCUMENT_POSITION_FOLLOWING: number;
142
+ readonly DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: number;
143
+ readonly DOCUMENT_POSITION_PRECEDING: number;
144
+ readonly DOCUMENT_TYPE_NODE: number;
145
+ readonly ELEMENT_NODE: number;
146
+ readonly ENTITY_NODE: number;
147
+ readonly ENTITY_REFERENCE_NODE: number;
148
+ readonly NOTATION_NODE: number;
149
+ readonly PROCESSING_INSTRUCTION_NODE: number;
150
+ readonly TEXT_NODE: number;
151
+ dispatchEvent(event: Event): boolean;
152
+ ariaAtomic: string | null;
153
+ ariaAutoComplete: string | null;
154
+ ariaBusy: string | null;
155
+ ariaChecked: string | null;
156
+ ariaColCount: string | null;
157
+ ariaColIndex: string | null;
158
+ ariaColSpan: string | null;
159
+ ariaCurrent: string | null;
160
+ ariaDisabled: string | null;
161
+ ariaExpanded: string | null;
162
+ ariaHasPopup: string | null;
163
+ ariaHidden: string | null;
164
+ ariaKeyShortcuts: string | null;
165
+ ariaLabel: string | null;
166
+ ariaLevel: string | null;
167
+ ariaLive: string | null;
168
+ ariaModal: string | null;
169
+ ariaMultiLine: string | null;
170
+ ariaMultiSelectable: string | null;
171
+ ariaOrientation: string | null;
172
+ ariaPlaceholder: string | null;
173
+ ariaPosInSet: string | null;
174
+ ariaPressed: string | null;
175
+ ariaReadOnly: string | null;
176
+ ariaRequired: string | null;
177
+ ariaRoleDescription: string | null;
178
+ ariaRowCount: string | null;
179
+ ariaRowIndex: string | null;
180
+ ariaRowSpan: string | null;
181
+ ariaSelected: string | null;
182
+ ariaSetSize: string | null;
183
+ ariaSort: string | null;
184
+ ariaValueMax: string | null;
185
+ ariaValueMin: string | null;
186
+ ariaValueNow: string | null;
187
+ ariaValueText: string | null;
188
+ animate(keyframes: PropertyIndexedKeyframes | Keyframe[] | null, options?: number | KeyframeAnimationOptions | undefined): Animation;
189
+ getAnimations(options?: GetAnimationsOptions | undefined): Animation[];
190
+ after(...nodes: (string | Node)[]): void;
191
+ before(...nodes: (string | Node)[]): void;
192
+ remove(): void;
193
+ replaceWith(...nodes: (string | Node)[]): void;
194
+ innerHTML: string;
195
+ readonly nextElementSibling: Element | null;
196
+ readonly previousElementSibling: Element | null;
197
+ readonly childElementCount: number;
198
+ readonly children: HTMLCollection;
199
+ readonly firstElementChild: Element | null;
200
+ readonly lastElementChild: Element | null;
201
+ append(...nodes: (string | Node)[]): void;
202
+ prepend(...nodes: (string | Node)[]): void;
203
+ querySelector<K_6 extends keyof HTMLElementTagNameMap>(selectors: K_6): HTMLElementTagNameMap[K_6] | null;
204
+ querySelector<K_7 extends keyof SVGElementTagNameMap>(selectors: K_7): SVGElementTagNameMap[K_7] | null;
205
+ querySelector<E_1 extends Element = Element>(selectors: string): E_1 | null;
206
+ querySelectorAll<K_8 extends keyof HTMLElementTagNameMap>(selectors: K_8): NodeListOf<HTMLElementTagNameMap[K_8]>;
207
+ querySelectorAll<K_9 extends keyof SVGElementTagNameMap>(selectors: K_9): NodeListOf<SVGElementTagNameMap[K_9]>;
208
+ querySelectorAll<E_2 extends Element = Element>(selectors: string): NodeListOf<E_2>;
209
+ replaceChildren(...nodes: (string | Node)[]): void;
210
+ readonly assignedSlot: HTMLSlotElement | null;
211
+ oncopy: ((this: DocumentAndElementEventHandlers, ev: ClipboardEvent) => any) | null;
212
+ oncut: ((this: DocumentAndElementEventHandlers, ev: ClipboardEvent) => any) | null;
213
+ onpaste: ((this: DocumentAndElementEventHandlers, ev: ClipboardEvent) => any) | null;
214
+ readonly style: CSSStyleDeclaration;
215
+ contentEditable: string;
216
+ enterKeyHint: string;
217
+ inputMode: string;
218
+ readonly isContentEditable: boolean;
219
+ onabort: ((this: GlobalEventHandlers, ev: UIEvent) => any) | null;
220
+ onanimationcancel: ((this: GlobalEventHandlers, ev: AnimationEvent) => any) | null;
221
+ onanimationend: ((this: GlobalEventHandlers, ev: AnimationEvent) => any) | null;
222
+ onanimationiteration: ((this: GlobalEventHandlers, ev: AnimationEvent) => any) | null;
223
+ onanimationstart: ((this: GlobalEventHandlers, ev: AnimationEvent) => any) | null;
224
+ onauxclick: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
225
+ onblur: ((this: GlobalEventHandlers, ev: FocusEvent) => any) | null;
226
+ oncanplay: ((this: GlobalEventHandlers, ev: Event) => any) | null;
227
+ oncanplaythrough: ((this: GlobalEventHandlers, ev: Event) => any) | null;
228
+ onchange: ((this: GlobalEventHandlers, ev: Event) => any) | null;
229
+ onclick: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
230
+ onclose: ((this: GlobalEventHandlers, ev: Event) => any) | null;
231
+ oncontextmenu: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
232
+ oncuechange: ((this: GlobalEventHandlers, ev: Event) => any) | null;
233
+ ondblclick: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
234
+ ondrag: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null;
235
+ ondragend: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null;
236
+ ondragenter: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null;
237
+ ondragleave: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null;
238
+ ondragover: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null;
239
+ ondragstart: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null;
240
+ ondrop: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null;
241
+ ondurationchange: ((this: GlobalEventHandlers, ev: Event) => any) | null;
242
+ onemptied: ((this: GlobalEventHandlers, ev: Event) => any) | null;
243
+ onended: ((this: GlobalEventHandlers, ev: Event) => any) | null;
244
+ onerror: OnErrorEventHandler;
245
+ onfocus: ((this: GlobalEventHandlers, ev: FocusEvent) => any) | null;
246
+ onformdata: ((this: GlobalEventHandlers, ev: FormDataEvent) => any) | null;
247
+ ongotpointercapture: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null;
248
+ oninput: ((this: GlobalEventHandlers, ev: Event) => any) | null;
249
+ oninvalid: ((this: GlobalEventHandlers, ev: Event) => any) | null;
250
+ onkeydown: ((this: GlobalEventHandlers, ev: KeyboardEvent) => any) | null;
251
+ onkeypress: ((this: GlobalEventHandlers, ev: KeyboardEvent) => any) | null;
252
+ onkeyup: ((this: GlobalEventHandlers, ev: KeyboardEvent) => any) | null;
253
+ onload: ((this: GlobalEventHandlers, ev: Event) => any) | null;
254
+ onloadeddata: ((this: GlobalEventHandlers, ev: Event) => any) | null;
255
+ onloadedmetadata: ((this: GlobalEventHandlers, ev: Event) => any) | null;
256
+ onloadstart: ((this: GlobalEventHandlers, ev: Event) => any) | null;
257
+ onlostpointercapture: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null;
258
+ onmousedown: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
259
+ onmouseenter: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
260
+ onmouseleave: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
261
+ onmousemove: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
262
+ onmouseout: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
263
+ onmouseover: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
264
+ onmouseup: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null;
265
+ onpause: ((this: GlobalEventHandlers, ev: Event) => any) | null;
266
+ onplay: ((this: GlobalEventHandlers, ev: Event) => any) | null;
267
+ onplaying: ((this: GlobalEventHandlers, ev: Event) => any) | null;
268
+ onpointercancel: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null;
269
+ onpointerdown: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null;
270
+ onpointerenter: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null;
271
+ onpointerleave: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null;
272
+ onpointermove: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null;
273
+ onpointerout: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null;
274
+ onpointerover: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null;
275
+ onpointerup: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null;
276
+ onprogress: ((this: GlobalEventHandlers, ev: ProgressEvent<EventTarget>) => any) | null;
277
+ onratechange: ((this: GlobalEventHandlers, ev: Event) => any) | null;
278
+ onreset: ((this: GlobalEventHandlers, ev: Event) => any) | null;
279
+ onresize: ((this: GlobalEventHandlers, ev: UIEvent) => any) | null;
280
+ onscroll: ((this: GlobalEventHandlers, ev: Event) => any) | null;
281
+ onsecuritypolicyviolation: ((this: GlobalEventHandlers, ev: SecurityPolicyViolationEvent) => any) | null;
282
+ onseeked: ((this: GlobalEventHandlers, ev: Event) => any) | null;
283
+ onseeking: ((this: GlobalEventHandlers, ev: Event) => any) | null;
284
+ onselect: ((this: GlobalEventHandlers, ev: Event) => any) | null;
285
+ onselectionchange: ((this: GlobalEventHandlers, ev: Event) => any) | null;
286
+ onselectstart: ((this: GlobalEventHandlers, ev: Event) => any) | null;
287
+ onslotchange: ((this: GlobalEventHandlers, ev: Event) => any) | null;
288
+ onstalled: ((this: GlobalEventHandlers, ev: Event) => any) | null;
289
+ onsubmit: ((this: GlobalEventHandlers, ev: SubmitEvent) => any) | null;
290
+ onsuspend: ((this: GlobalEventHandlers, ev: Event) => any) | null;
291
+ ontimeupdate: ((this: GlobalEventHandlers, ev: Event) => any) | null;
292
+ ontoggle: ((this: GlobalEventHandlers, ev: Event) => any) | null;
293
+ ontouchcancel?: ((this: GlobalEventHandlers, ev: TouchEvent) => any) | null | undefined;
294
+ ontouchend?: ((this: GlobalEventHandlers, ev: TouchEvent) => any) | null | undefined;
295
+ ontouchmove?: ((this: GlobalEventHandlers, ev: TouchEvent) => any) | null | undefined;
296
+ ontouchstart?: ((this: GlobalEventHandlers, ev: TouchEvent) => any) | null | undefined;
297
+ ontransitioncancel: ((this: GlobalEventHandlers, ev: TransitionEvent) => any) | null;
298
+ ontransitionend: ((this: GlobalEventHandlers, ev: TransitionEvent) => any) | null;
299
+ ontransitionrun: ((this: GlobalEventHandlers, ev: TransitionEvent) => any) | null;
300
+ ontransitionstart: ((this: GlobalEventHandlers, ev: TransitionEvent) => any) | null;
301
+ onvolumechange: ((this: GlobalEventHandlers, ev: Event) => any) | null;
302
+ onwaiting: ((this: GlobalEventHandlers, ev: Event) => any) | null;
303
+ onwebkitanimationend: ((this: GlobalEventHandlers, ev: Event) => any) | null;
304
+ onwebkitanimationiteration: ((this: GlobalEventHandlers, ev: Event) => any) | null;
305
+ onwebkitanimationstart: ((this: GlobalEventHandlers, ev: Event) => any) | null;
306
+ onwebkittransitionend: ((this: GlobalEventHandlers, ev: Event) => any) | null;
307
+ onwheel: ((this: GlobalEventHandlers, ev: WheelEvent) => any) | null;
308
+ autofocus: boolean;
309
+ readonly dataset: DOMStringMap;
310
+ nonce?: string | undefined;
311
+ tabIndex: number;
312
+ blur(): void;
313
+ focus(options?: FocusOptions | undefined): void;
314
+ };
315
+ inject?: ProviderToken<any>[] | undefined;
316
+ providers?: Provider<any>[] | undefined;
317
+ } & T;
@@ -0,0 +1,49 @@
1
+ import { Injector } from '../injector';
2
+ import { environment } from './environment';
3
+ export function injectable(CustomElement) {
4
+ const { inject, providers } = CustomElement;
5
+ return class InjectableElement extends CustomElement {
6
+ constructor(...args) {
7
+ const injector = new Injector(providers, environment());
8
+ if (args.length || !inject) {
9
+ super(...args);
10
+ }
11
+ else {
12
+ super(...inject.map((dep) => () => injector.get(dep)));
13
+ }
14
+ this.injector = injector;
15
+ this.addEventListener('finddiroot', (e) => {
16
+ const parentInjector = findInjectorRoot(e);
17
+ if (parentInjector) {
18
+ this.injector.parent = parentInjector;
19
+ }
20
+ if (super.connectedCallback) {
21
+ super.connectedCallback();
22
+ }
23
+ });
24
+ }
25
+ connectedCallback() {
26
+ // only mark as an injector root if element defines providers
27
+ if (providers) {
28
+ this.setAttribute('joist-injector-root', 'true');
29
+ }
30
+ this.dispatchEvent(new Event('finddiroot'));
31
+ }
32
+ disconnectedCallback() {
33
+ delete this.injector.parent;
34
+ if (super.disconnectedCallback) {
35
+ super.disconnectedCallback();
36
+ }
37
+ }
38
+ };
39
+ }
40
+ function findInjectorRoot(e) {
41
+ const path = e.composedPath();
42
+ const parentInjector = path.find((el) => {
43
+ return el instanceof HTMLElement && el !== e.target && el.hasAttribute('joist-injector-root');
44
+ });
45
+ if (parentInjector) {
46
+ return Reflect.get(parentInjector, 'injector');
47
+ }
48
+ return null;
49
+ }
@@ -0,0 +1,2 @@
1
+ export { environment } from './dom/environment';
2
+ export { injectable } from './dom/injectable';
@@ -0,0 +1,2 @@
1
+ export { environment } from './dom/environment';
2
+ export { injectable } from './dom/injectable';
@@ -1,5 +1,3 @@
1
- import { Injector } from './injector';
2
- import { Provider } from './provider';
3
- export declare function defineEnvironment(providers?: Provider<any>[]): Injector;
4
- export declare function getEnvironmentRef(): Injector;
1
+ import { Injector } from './injector.js';
2
+ export declare function environment(): Injector;
5
3
  export declare function clearEnvironment(): void;