Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Website #1070

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open

Website #1070

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Joist

**Sponsored by:**
Expand Down
155 changes: 146 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "joist",
"version": "4.0.0-next.41",
"version": "4.0.0-next.42",
"description": "",
"type": "module",
"author": "",
"author": "deebloo",
"license": "MIT",
"workspaces": [
"packages/**",
"integration/**"
"integration/**",
"website"
],
"scripts": {
"prepare": "husky"
Expand Down
2 changes: 1 addition & 1 deletion packages/di/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@joist/di",
"version": "4.0.0-next.40",
"version": "4.0.0-next.42",
"type": "module",
"main": "./target/lib.js",
"module": "./target/lib.js",
Expand Down
22 changes: 13 additions & 9 deletions packages/di/src/lib/dom-injector.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from 'chai';

import { DOMInjector } from './dom-injector.js';
import { INJECTOR_CTX } from './context/injector.js';
import { Injector } from './injector.js';
Expand Down Expand Up @@ -29,20 +30,23 @@ describe('DOMInjector', () => {
it('should send request looking for other injector contexts', () => {
const parent = new Injector();
const injector = new DOMInjector();

const cb = (e: any) => {
if (e.context === INJECTOR_CTX) {
e.callback(parent);
}
};

document.body.addEventListener('context-request', cb);
const controller = new AbortController();

document.body.addEventListener(
'context-request',
(e: any) => {
if (e.context === INJECTOR_CTX) {
e.callback(parent);
}
},
{ signal: controller.signal }
);

injector.attach(document.body);

assert.equal(injector.parent, parent);

injector.detach();
document.body.removeEventListener('context-request', cb);
controller.abort();
});
});
34 changes: 19 additions & 15 deletions packages/di/src/lib/dom-injector.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
import { ContextRequestEvent } from './context/protocol.js';
import { ContextRequestEvent, UnknownContext } from './context/protocol.js';
import { INJECTOR_CTX } from './context/injector.js';
import { Injector } from './injector.js';

export class DOMInjector extends Injector {
#contextCallback = (e: ContextRequestEvent<{ __context__: unknown }>) => {
if (e.context === INJECTOR_CTX) {
if (e.target !== this.#element) {
e.stopPropagation();

e.callback(this);
}
}
};

#element: HTMLElement | null = null;
#controller: AbortController | null = null;

attach(element: HTMLElement): void {
this.#element = element;
this.#controller = new AbortController();

this.#element.addEventListener('context-request', this.#contextCallback);
this.#element.addEventListener(
'context-request',
(e: ContextRequestEvent<UnknownContext>) => {
if (e.context === INJECTOR_CTX) {
if (e.target !== this.#element) {
e.stopPropagation();

e.callback(this);
}
}
},
{ signal: this.#controller.signal }
);

this.#element.dispatchEvent(
new ContextRequestEvent(INJECTOR_CTX, (parent) => {
this.setParent(parent);
this.parent = parent;
})
);
}

detach(): void {
if (this.#element) {
this.#element.removeEventListener('context-request', this.#contextCallback);
if (this.#controller) {
this.#controller.abort();
}
}
}
Loading
Loading