@bodil/dom 0.1.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/LICENCE.md +288 -0
- package/dist/component.d.ts +147 -0
- package/dist/component.js +405 -0
- package/dist/component.js.map +1 -0
- package/dist/component.test.d.ts +1 -0
- package/dist/component.test.js +141 -0
- package/dist/component.test.js.map +1 -0
- package/dist/css.d.ts +14 -0
- package/dist/css.js +63 -0
- package/dist/css.js.map +1 -0
- package/dist/decorators/attribute.d.ts +26 -0
- package/dist/decorators/attribute.js +115 -0
- package/dist/decorators/attribute.js.map +1 -0
- package/dist/decorators/attribute.test.d.ts +1 -0
- package/dist/decorators/attribute.test.js +148 -0
- package/dist/decorators/attribute.test.js.map +1 -0
- package/dist/decorators/connect.d.ts +9 -0
- package/dist/decorators/connect.js +44 -0
- package/dist/decorators/connect.js.map +1 -0
- package/dist/decorators/connect.test.d.ts +1 -0
- package/dist/decorators/connect.test.js +196 -0
- package/dist/decorators/connect.test.js.map +1 -0
- package/dist/decorators/reactive.d.ts +7 -0
- package/dist/decorators/reactive.js +45 -0
- package/dist/decorators/reactive.js.map +1 -0
- package/dist/decorators/reactive.test.d.ts +1 -0
- package/dist/decorators/reactive.test.js +113 -0
- package/dist/decorators/reactive.test.js.map +1 -0
- package/dist/decorators/require.d.ts +3 -0
- package/dist/decorators/require.js +6 -0
- package/dist/decorators/require.js.map +1 -0
- package/dist/decorators/require.test.d.ts +1 -0
- package/dist/decorators/require.test.js +117 -0
- package/dist/decorators/require.test.js.map +1 -0
- package/dist/dom.d.ts +92 -0
- package/dist/dom.js +354 -0
- package/dist/dom.js.map +1 -0
- package/dist/emitter.d.ts +10 -0
- package/dist/emitter.js +17 -0
- package/dist/emitter.js.map +1 -0
- package/dist/event.d.ts +70 -0
- package/dist/event.js +14 -0
- package/dist/event.js.map +1 -0
- package/dist/event.test.d.ts +1 -0
- package/dist/event.test.js +20 -0
- package/dist/event.test.js.map +1 -0
- package/dist/geometry.d.ts +48 -0
- package/dist/geometry.js +117 -0
- package/dist/geometry.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/scheduler.d.ts +2 -0
- package/dist/scheduler.js +54 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/slot.d.ts +31 -0
- package/dist/slot.js +67 -0
- package/dist/slot.js.map +1 -0
- package/dist/test-global.d.ts +0 -0
- package/dist/test-global.js +5 -0
- package/dist/test-global.js.map +1 -0
- package/package.json +116 -0
- package/src/component.test.ts +90 -0
- package/src/component.ts +607 -0
- package/src/css.ts +69 -0
- package/src/decorators/attribute.test.ts +77 -0
- package/src/decorators/attribute.ts +197 -0
- package/src/decorators/connect.test.ts +119 -0
- package/src/decorators/connect.ts +85 -0
- package/src/decorators/reactive.test.ts +45 -0
- package/src/decorators/reactive.ts +80 -0
- package/src/decorators/require.test.ts +57 -0
- package/src/decorators/require.ts +11 -0
- package/src/dom.ts +456 -0
- package/src/emitter.ts +32 -0
- package/src/event.test.ts +22 -0
- package/src/event.ts +74 -0
- package/src/geometry.ts +147 -0
- package/src/index.ts +12 -0
- package/src/scheduler.ts +58 -0
- package/src/slot.ts +95 -0
- package/src/test-global.ts +5 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { customElement } from "lit/decorators.js";
|
|
2
|
+
import { expect, test } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { attribute, Component } from "../component";
|
|
5
|
+
import { Signal } from "@bodil/signal";
|
|
6
|
+
|
|
7
|
+
test("@attribute", async () => {
|
|
8
|
+
@customElement("attribute-test-class")
|
|
9
|
+
class AttributeTestClass extends Component {
|
|
10
|
+
@attribute accessor wibble: string | undefined = "Joe";
|
|
11
|
+
@attribute({ type: Number }) accessor wobble: number | undefined = 1;
|
|
12
|
+
@attribute({ type: Boolean }) accessor noMeansNo: boolean | undefined = false;
|
|
13
|
+
@attribute({ name: "wolp", reactive: false }) accessor welp: string | undefined = "Joe";
|
|
14
|
+
@attribute({ reflect: false }) accessor hide: string | undefined = "no";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const t = document.createElement("attribute-test-class") as AttributeTestClass;
|
|
18
|
+
document.body.append(t);
|
|
19
|
+
await t.updateComplete;
|
|
20
|
+
|
|
21
|
+
const wibble = Signal.computed(() => t.wibble);
|
|
22
|
+
expect(t.wibble).toBe("Joe");
|
|
23
|
+
expect(t.getAttribute("wibble")).toBe("Joe");
|
|
24
|
+
expect(wibble.get()).toBe("Joe");
|
|
25
|
+
t.wibble = "Mike";
|
|
26
|
+
expect(t.wibble).toBe("Mike");
|
|
27
|
+
expect(t.getAttribute("wibble")).toBe("Mike");
|
|
28
|
+
expect(wibble.get()).toBe("Mike");
|
|
29
|
+
t.removeAttribute("wibble");
|
|
30
|
+
expect(t.wibble).toBeUndefined();
|
|
31
|
+
expect(t.getAttribute("wibble")).toBeNull();
|
|
32
|
+
expect(wibble.get()).toBe(undefined);
|
|
33
|
+
t.setAttribute("wibble", "Robert");
|
|
34
|
+
expect(t.wibble).toBe("Robert");
|
|
35
|
+
expect(t.getAttribute("wibble")).toBe("Robert");
|
|
36
|
+
expect(wibble.get()).toBe("Robert");
|
|
37
|
+
t.wibble = undefined;
|
|
38
|
+
expect(t.wibble).toBeUndefined();
|
|
39
|
+
expect(t.getAttribute("wibble")).toBeNull();
|
|
40
|
+
expect(wibble.get()).toBe(undefined);
|
|
41
|
+
|
|
42
|
+
expect(t.wobble).toBe(1);
|
|
43
|
+
expect(t.getAttribute("wobble")).toBe("1");
|
|
44
|
+
t.wobble = 2;
|
|
45
|
+
expect(t.wobble).toBe(2);
|
|
46
|
+
expect(t.getAttribute("wobble")).toBe("2");
|
|
47
|
+
t.setAttribute("wobble", "3");
|
|
48
|
+
expect(t.wobble).toBe(3);
|
|
49
|
+
expect(t.getAttribute("wobble")).toBe("3");
|
|
50
|
+
|
|
51
|
+
expect(t.noMeansNo).toBe(false);
|
|
52
|
+
expect(t.getAttribute("no-means-no")).toBeNull();
|
|
53
|
+
t.noMeansNo = true;
|
|
54
|
+
expect(t.noMeansNo).toBe(true);
|
|
55
|
+
expect(t.getAttribute("no-means-no")).toBe("");
|
|
56
|
+
t.removeAttribute("no-means-no");
|
|
57
|
+
expect(t.noMeansNo).toBe(false);
|
|
58
|
+
expect(t.getAttribute("no-means-no")).toBeNull();
|
|
59
|
+
t.setAttribute("no-means-no", "come and see the violence inherent in the system!");
|
|
60
|
+
expect(t.noMeansNo).toBe(true);
|
|
61
|
+
expect(t.getAttribute("no-means-no")).toBe("");
|
|
62
|
+
|
|
63
|
+
const welp = Signal.computed(() => t.welp);
|
|
64
|
+
expect(t.welp).toBe("Joe");
|
|
65
|
+
expect(t.getAttribute("wolp")).toBe("Joe");
|
|
66
|
+
expect(welp.get()).toBe("Joe");
|
|
67
|
+
t.welp = "Robert";
|
|
68
|
+
expect(t.welp).toBe("Robert");
|
|
69
|
+
expect(t.getAttribute("wolp")).toBe("Robert");
|
|
70
|
+
expect(welp.get()).toBe("Joe");
|
|
71
|
+
|
|
72
|
+
expect(t.hide).toBe("no");
|
|
73
|
+
expect(t.getAttribute("hide")).toBeNull();
|
|
74
|
+
t.setAttribute("hide", "yes");
|
|
75
|
+
expect(t.hide).toBe("yes");
|
|
76
|
+
expect(t.getAttribute("hide")).toBe("yes");
|
|
77
|
+
});
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/unified-signatures */
|
|
2
|
+
|
|
3
|
+
import { isNullish, unreachable } from "@bodil/core/assert";
|
|
4
|
+
import { Signal } from "@bodil/signal";
|
|
5
|
+
|
|
6
|
+
import type { Component } from "../component";
|
|
7
|
+
import { signalForObject } from "./reactive";
|
|
8
|
+
|
|
9
|
+
export const attributeConfig = Symbol("attributeConfig");
|
|
10
|
+
|
|
11
|
+
export type AttributeType = typeof String | typeof Number | typeof Boolean;
|
|
12
|
+
|
|
13
|
+
export type AttributeOptions = {
|
|
14
|
+
name: string | undefined;
|
|
15
|
+
reflect: boolean;
|
|
16
|
+
reactive: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type AttributeConfig = {
|
|
20
|
+
type: AttributeType | undefined;
|
|
21
|
+
property: string;
|
|
22
|
+
} & AttributeOptions;
|
|
23
|
+
|
|
24
|
+
export function toAttribute(value: unknown, type: AttributeType): string | null {
|
|
25
|
+
if (isNullish(value)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
switch (type) {
|
|
29
|
+
case Number:
|
|
30
|
+
return `${value as number}`;
|
|
31
|
+
case Boolean:
|
|
32
|
+
return (value as boolean) ? "" : null;
|
|
33
|
+
case String:
|
|
34
|
+
return value as string;
|
|
35
|
+
default:
|
|
36
|
+
unreachable();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function fromAttribute(value: string | null, type: typeof Number): number | undefined;
|
|
41
|
+
export function fromAttribute(value: string | null, type: typeof Boolean): boolean;
|
|
42
|
+
export function fromAttribute(value: string | null, type: typeof String): string | undefined;
|
|
43
|
+
export function fromAttribute(
|
|
44
|
+
value: string | null,
|
|
45
|
+
type: AttributeType,
|
|
46
|
+
): string | number | boolean | undefined;
|
|
47
|
+
export function fromAttribute(
|
|
48
|
+
value: string | null,
|
|
49
|
+
type: AttributeType,
|
|
50
|
+
): string | number | boolean | undefined {
|
|
51
|
+
switch (type) {
|
|
52
|
+
case Number: {
|
|
53
|
+
const num = value === null ? undefined : Number(value);
|
|
54
|
+
if (num !== undefined && Number.isNaN(num)) {
|
|
55
|
+
throw new TypeError(
|
|
56
|
+
`numeric attribute value ${JSON.stringify(value)} parsed as NaN`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return num;
|
|
60
|
+
}
|
|
61
|
+
case Boolean:
|
|
62
|
+
return value !== null;
|
|
63
|
+
case String:
|
|
64
|
+
return value ?? undefined;
|
|
65
|
+
default:
|
|
66
|
+
unreachable();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function detectAttributeType(value: unknown): AttributeType | undefined {
|
|
71
|
+
switch (typeof value) {
|
|
72
|
+
case "string":
|
|
73
|
+
return String;
|
|
74
|
+
case "number":
|
|
75
|
+
return Number;
|
|
76
|
+
case "boolean":
|
|
77
|
+
return Boolean;
|
|
78
|
+
default:
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function toKebabCase(s: string): string {
|
|
84
|
+
return s.replace(/((?<=[a-z\d])[A-Z]|(?<=[A-Z\d])[A-Z](?=[a-z]))/g, "-$1").toLowerCase();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Accessor with number type
|
|
88
|
+
export function attribute<C extends Component, T extends number | undefined>(
|
|
89
|
+
options: Partial<AttributeOptions> & { type: typeof Number },
|
|
90
|
+
): (
|
|
91
|
+
value: ClassAccessorDecoratorTarget<C, T>,
|
|
92
|
+
context: ClassAccessorDecoratorContext<C, T>,
|
|
93
|
+
) => ClassAccessorDecoratorResult<C, T>;
|
|
94
|
+
|
|
95
|
+
// Accessor with boolean type
|
|
96
|
+
export function attribute<C extends Component, T extends boolean | undefined>(
|
|
97
|
+
options: Partial<AttributeOptions> & { type: typeof Boolean },
|
|
98
|
+
): (
|
|
99
|
+
value: ClassAccessorDecoratorTarget<C, T>,
|
|
100
|
+
context: ClassAccessorDecoratorContext<C, T>,
|
|
101
|
+
) => ClassAccessorDecoratorResult<C, T>;
|
|
102
|
+
|
|
103
|
+
// Accessor with string type
|
|
104
|
+
export function attribute<C extends Component, T extends string | undefined>(
|
|
105
|
+
options: Partial<AttributeOptions>,
|
|
106
|
+
): (
|
|
107
|
+
value: ClassAccessorDecoratorTarget<C, T>,
|
|
108
|
+
context: ClassAccessorDecoratorContext<C, T>,
|
|
109
|
+
) => ClassAccessorDecoratorResult<C, T>;
|
|
110
|
+
|
|
111
|
+
// Accessor with no options
|
|
112
|
+
export function attribute<C extends Component, T extends string | undefined>(
|
|
113
|
+
value: ClassAccessorDecoratorTarget<C, T>,
|
|
114
|
+
context: ClassAccessorDecoratorContext<C, T>,
|
|
115
|
+
): ClassAccessorDecoratorResult<C, T>;
|
|
116
|
+
|
|
117
|
+
export function attribute<C extends Component, T extends string | number | boolean | undefined>(
|
|
118
|
+
valueOrOptions?: ClassAccessorDecoratorTarget<C, T> | Partial<AttributeOptions>,
|
|
119
|
+
context?: ClassAccessorDecoratorContext<C, T>,
|
|
120
|
+
):
|
|
121
|
+
| ClassAccessorDecoratorResult<C, T>
|
|
122
|
+
| ((
|
|
123
|
+
value: ClassAccessorDecoratorTarget<C, T>,
|
|
124
|
+
context: ClassAccessorDecoratorContext<C, T>,
|
|
125
|
+
) => ClassAccessorDecoratorResult<C, T>) {
|
|
126
|
+
if (context !== undefined) {
|
|
127
|
+
return attribute({})(valueOrOptions as any, context as any) as ClassAccessorDecoratorResult<
|
|
128
|
+
C,
|
|
129
|
+
T
|
|
130
|
+
>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return (value, context) => {
|
|
134
|
+
if (typeof context.name !== "string") {
|
|
135
|
+
throw new TypeError(
|
|
136
|
+
`Can't declare a ${typeof context.name} as an attribute (${JSON.stringify(context.name)})`,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const options: AttributeConfig = {
|
|
141
|
+
...{
|
|
142
|
+
type: String,
|
|
143
|
+
property: context.name,
|
|
144
|
+
reactive: true,
|
|
145
|
+
reflect: true,
|
|
146
|
+
},
|
|
147
|
+
...((valueOrOptions as AttributeConfig) ?? {}),
|
|
148
|
+
};
|
|
149
|
+
let attrs = context.metadata[attributeConfig] as Map<string, AttributeConfig>;
|
|
150
|
+
if (attrs === undefined) {
|
|
151
|
+
context.metadata[attributeConfig] = attrs = new Map();
|
|
152
|
+
}
|
|
153
|
+
const key = options.name ?? toKebabCase(context.name);
|
|
154
|
+
attrs.set(key, options);
|
|
155
|
+
|
|
156
|
+
if (options.reactive) {
|
|
157
|
+
const getSig = (obj: object) =>
|
|
158
|
+
signalForObject(obj, context.name, () =>
|
|
159
|
+
Signal((value as ClassAccessorDecoratorTarget<unknown, T>).get.call(obj), {
|
|
160
|
+
equals: Object.is,
|
|
161
|
+
}),
|
|
162
|
+
) as Signal.State<T>;
|
|
163
|
+
return {
|
|
164
|
+
get(): T {
|
|
165
|
+
return getSig(this).get();
|
|
166
|
+
},
|
|
167
|
+
set(newValue: T) {
|
|
168
|
+
options.type ??= detectAttributeType(newValue);
|
|
169
|
+
getSig(this).set(newValue);
|
|
170
|
+
if (options.reflect) {
|
|
171
|
+
(this as any).syncAttribute(key);
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
init(newValue: T): T {
|
|
175
|
+
options.type ??= detectAttributeType(newValue);
|
|
176
|
+
return newValue;
|
|
177
|
+
},
|
|
178
|
+
} as ClassAccessorDecoratorResult<C, T>;
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
get(): T {
|
|
182
|
+
return value.get.call(this);
|
|
183
|
+
},
|
|
184
|
+
set(newValue: T) {
|
|
185
|
+
options.type ??= detectAttributeType(newValue);
|
|
186
|
+
value.set.call(this, newValue);
|
|
187
|
+
if (options.reflect) {
|
|
188
|
+
(this as any).syncAttribute(key);
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
init(newValue: T): T {
|
|
192
|
+
options.type ??= detectAttributeType(newValue);
|
|
193
|
+
return newValue;
|
|
194
|
+
},
|
|
195
|
+
} as ClassAccessorDecoratorResult<C, T>;
|
|
196
|
+
};
|
|
197
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { sleep } from "@bodil/core/async";
|
|
2
|
+
import { Signal } from "@bodil/signal";
|
|
3
|
+
import { customElement } from "lit/decorators.js";
|
|
4
|
+
import { expect, test } from "vitest";
|
|
5
|
+
|
|
6
|
+
import { Component, connect, connectEffect } from "../component";
|
|
7
|
+
import type { ConnectFunction } from "./connect";
|
|
8
|
+
|
|
9
|
+
test("@connect", () => {
|
|
10
|
+
let methodConnected = 0,
|
|
11
|
+
methodDisconnected = 0;
|
|
12
|
+
let fieldConnected = 0,
|
|
13
|
+
fieldDisconnected = 0;
|
|
14
|
+
|
|
15
|
+
@customElement("connected-test-class")
|
|
16
|
+
class ConnectedTestClass extends Component {
|
|
17
|
+
@connect onConnected() {
|
|
18
|
+
methodConnected++;
|
|
19
|
+
return () => {
|
|
20
|
+
methodDisconnected++;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@connect field: ConnectFunction = () => {
|
|
25
|
+
fieldConnected++;
|
|
26
|
+
return () => {
|
|
27
|
+
fieldDisconnected++;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const c = document.createElement("connected-test-class") as ConnectedTestClass;
|
|
33
|
+
expect(methodConnected).toBe(0);
|
|
34
|
+
expect(methodDisconnected).toBe(0);
|
|
35
|
+
expect(fieldConnected).toEqual(methodConnected);
|
|
36
|
+
expect(fieldDisconnected).toEqual(methodDisconnected);
|
|
37
|
+
document.body.append(c);
|
|
38
|
+
expect(methodConnected).toBe(1);
|
|
39
|
+
expect(methodDisconnected).toBe(0);
|
|
40
|
+
expect(fieldConnected).toEqual(methodConnected);
|
|
41
|
+
expect(fieldDisconnected).toEqual(methodDisconnected);
|
|
42
|
+
c.remove();
|
|
43
|
+
expect(methodConnected).toBe(1);
|
|
44
|
+
expect(methodDisconnected).toBe(1);
|
|
45
|
+
expect(fieldConnected).toEqual(methodConnected);
|
|
46
|
+
expect(fieldDisconnected).toEqual(methodDisconnected);
|
|
47
|
+
document.body.append(c);
|
|
48
|
+
expect(methodConnected).toBe(2);
|
|
49
|
+
expect(methodDisconnected).toBe(1);
|
|
50
|
+
expect(fieldConnected).toEqual(methodConnected);
|
|
51
|
+
expect(fieldDisconnected).toEqual(methodDisconnected);
|
|
52
|
+
c.remove();
|
|
53
|
+
expect(methodConnected).toBe(2);
|
|
54
|
+
expect(methodDisconnected).toBe(2);
|
|
55
|
+
expect(fieldConnected).toEqual(methodConnected);
|
|
56
|
+
expect(fieldDisconnected).toEqual(methodDisconnected);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("@connectEffect", async () => {
|
|
60
|
+
let methodRun = 0,
|
|
61
|
+
methodDisposed = 0;
|
|
62
|
+
let fieldRun = 0,
|
|
63
|
+
fieldDisposed = 0;
|
|
64
|
+
const signal = Signal(1);
|
|
65
|
+
|
|
66
|
+
@customElement("connect-effect-test-class")
|
|
67
|
+
class ConnectEffectTestClass extends Component {
|
|
68
|
+
@connectEffect onConnected() {
|
|
69
|
+
methodRun += signal.get();
|
|
70
|
+
return () => {
|
|
71
|
+
methodDisposed++;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@connectEffect field: ConnectFunction = () => {
|
|
76
|
+
fieldRun += signal.get();
|
|
77
|
+
return () => {
|
|
78
|
+
fieldDisposed++;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const c = document.createElement("connect-effect-test-class") as ConnectEffectTestClass;
|
|
84
|
+
expect(methodRun).toBe(0);
|
|
85
|
+
expect(methodDisposed).toBe(0);
|
|
86
|
+
expect(fieldRun).toEqual(methodRun);
|
|
87
|
+
expect(fieldDisposed).toEqual(methodDisposed);
|
|
88
|
+
document.body.append(c);
|
|
89
|
+
await c.updateComplete;
|
|
90
|
+
expect(methodRun).toBe(1);
|
|
91
|
+
expect(methodDisposed).toBe(0);
|
|
92
|
+
expect(fieldRun).toEqual(methodRun);
|
|
93
|
+
expect(fieldDisposed).toEqual(methodDisposed);
|
|
94
|
+
c.remove();
|
|
95
|
+
expect(methodRun).toBe(1);
|
|
96
|
+
expect(methodDisposed).toBe(1);
|
|
97
|
+
expect(fieldRun).toEqual(methodRun);
|
|
98
|
+
expect(fieldDisposed).toEqual(methodDisposed);
|
|
99
|
+
document.body.append(c);
|
|
100
|
+
expect(methodRun).toBe(2);
|
|
101
|
+
expect(methodDisposed).toBe(1);
|
|
102
|
+
expect(fieldRun).toEqual(methodRun);
|
|
103
|
+
expect(fieldDisposed).toEqual(methodDisposed);
|
|
104
|
+
signal.set(2);
|
|
105
|
+
// wait for the effect to run
|
|
106
|
+
await sleep(1);
|
|
107
|
+
// run should be bumped by 2, the new value of the signal, and
|
|
108
|
+
// disposed should be bumped by 1 because the previous effect is
|
|
109
|
+
// disposed.
|
|
110
|
+
expect(methodRun).toBe(4);
|
|
111
|
+
expect(methodDisposed).toBe(2);
|
|
112
|
+
expect(fieldRun).toEqual(methodRun);
|
|
113
|
+
expect(fieldDisposed).toEqual(methodDisposed);
|
|
114
|
+
c.remove();
|
|
115
|
+
expect(methodRun).toBe(4);
|
|
116
|
+
expect(methodDisposed).toBe(3);
|
|
117
|
+
expect(fieldRun).toEqual(methodRun);
|
|
118
|
+
expect(fieldDisposed).toEqual(methodDisposed);
|
|
119
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Disposifiable } from "@bodil/core/disposable";
|
|
2
|
+
import { Signal } from "@bodil/signal";
|
|
3
|
+
|
|
4
|
+
import type { Component } from "../component";
|
|
5
|
+
|
|
6
|
+
export type ConnectFunctionReturnValue =
|
|
7
|
+
| Disposifiable
|
|
8
|
+
| Iterable<Disposifiable | undefined>
|
|
9
|
+
| undefined
|
|
10
|
+
| void;
|
|
11
|
+
|
|
12
|
+
export type ConnectFunction = () => ConnectFunctionReturnValue;
|
|
13
|
+
|
|
14
|
+
const connectedCache = new WeakMap<Component, Set<ConnectFunction>>();
|
|
15
|
+
|
|
16
|
+
function addConnectedJob(obj: Component, job: ConnectFunction) {
|
|
17
|
+
let jobs = connectedCache.get(obj);
|
|
18
|
+
if (jobs === undefined) {
|
|
19
|
+
jobs = new Set();
|
|
20
|
+
connectedCache.set(obj, jobs);
|
|
21
|
+
}
|
|
22
|
+
jobs.add(job);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function connectedJobs(obj: Component): Iterable<ConnectFunction> {
|
|
26
|
+
return connectedCache.get(obj) ?? [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function connect<C extends Component>(
|
|
30
|
+
value: ConnectFunction,
|
|
31
|
+
context: ClassMethodDecoratorContext<C, ConnectFunction>,
|
|
32
|
+
): undefined;
|
|
33
|
+
export function connect<C extends Component>(
|
|
34
|
+
value: undefined,
|
|
35
|
+
context: ClassFieldDecoratorContext<C, ConnectFunction>,
|
|
36
|
+
): (value: ConnectFunction) => ConnectFunction;
|
|
37
|
+
export function connect<C extends Component>(
|
|
38
|
+
value: ConnectFunction | undefined,
|
|
39
|
+
context:
|
|
40
|
+
| ClassMethodDecoratorContext<C, ConnectFunction>
|
|
41
|
+
| ClassFieldDecoratorContext<C, ConnectFunction>,
|
|
42
|
+
): ((value: ConnectFunction) => void) | undefined {
|
|
43
|
+
switch (context.kind) {
|
|
44
|
+
case "method":
|
|
45
|
+
context.addInitializer(function (this: C) {
|
|
46
|
+
addConnectedJob(this, value!.bind(this));
|
|
47
|
+
});
|
|
48
|
+
return;
|
|
49
|
+
case "field":
|
|
50
|
+
return function (this: C, value: ConnectFunction) {
|
|
51
|
+
addConnectedJob(this, value.bind(this));
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function connectEffect<C extends Component>(
|
|
57
|
+
value: ConnectFunction,
|
|
58
|
+
context: ClassMethodDecoratorContext<C, ConnectFunction>,
|
|
59
|
+
): undefined;
|
|
60
|
+
export function connectEffect<C extends Component>(
|
|
61
|
+
value: undefined,
|
|
62
|
+
context: ClassFieldDecoratorContext<C, ConnectFunction>,
|
|
63
|
+
): (value: ConnectFunction) => ConnectFunction;
|
|
64
|
+
export function connectEffect<C extends Component>(
|
|
65
|
+
value: ConnectFunction | undefined,
|
|
66
|
+
context:
|
|
67
|
+
| ClassMethodDecoratorContext<C, ConnectFunction>
|
|
68
|
+
| ClassFieldDecoratorContext<C, ConnectFunction>,
|
|
69
|
+
): ((value: ConnectFunction) => void) | undefined {
|
|
70
|
+
switch (context.kind) {
|
|
71
|
+
case "method":
|
|
72
|
+
context.addInitializer(function (this: C) {
|
|
73
|
+
addConnectedJob(this, function (this: Component) {
|
|
74
|
+
return Signal.effect(value!.bind(this) as any);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
case "field":
|
|
79
|
+
return function (this: C, value: ConnectFunction) {
|
|
80
|
+
addConnectedJob(this, function (this: Component) {
|
|
81
|
+
return Signal.effect(value.bind(this) as any);
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Signal } from "@bodil/signal";
|
|
2
|
+
import { expect, test } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { Component, reactive } from "../component";
|
|
5
|
+
import { customElement } from "lit/decorators.js";
|
|
6
|
+
|
|
7
|
+
test("@reactive", () => {
|
|
8
|
+
const name = Signal("Joe");
|
|
9
|
+
class ReactiveTestClass {
|
|
10
|
+
@reactive get name(): string {
|
|
11
|
+
return name.get();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
@reactive accessor counter = 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const instance = new ReactiveTestClass();
|
|
18
|
+
|
|
19
|
+
expect(instance.name).toBe("Joe");
|
|
20
|
+
name.set("Robert");
|
|
21
|
+
expect(instance.name).toBe("Robert");
|
|
22
|
+
|
|
23
|
+
const counter = Signal.computed(() => instance.counter);
|
|
24
|
+
expect(counter.get()).toBe(0);
|
|
25
|
+
instance.counter += 5;
|
|
26
|
+
expect(counter.get()).toBe(5);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("@reactive Component fields", () => {
|
|
30
|
+
@customElement("reactive-test-class")
|
|
31
|
+
class ReactiveTestClass extends Component {
|
|
32
|
+
@reactive name?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// const instance = new ReactiveTestClass();
|
|
36
|
+
const instance = document.createElement("reactive-test-class") as ReactiveTestClass;
|
|
37
|
+
instance.$signal("name");
|
|
38
|
+
|
|
39
|
+
const name = Signal.computed(() => instance.name);
|
|
40
|
+
expect(instance.name).toBeUndefined();
|
|
41
|
+
expect(name.get()).toBeUndefined();
|
|
42
|
+
instance.name = "Joe";
|
|
43
|
+
expect(instance.name).toBe("Joe");
|
|
44
|
+
expect(name.get()).toBe("Joe");
|
|
45
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Signal } from "@bodil/signal";
|
|
2
|
+
|
|
3
|
+
import type { Component } from "../component";
|
|
4
|
+
|
|
5
|
+
export const reactiveFields = Symbol("reactiveFields");
|
|
6
|
+
|
|
7
|
+
const signalCache = new WeakMap<object, Record<PropertyKey, Signal<unknown>>>();
|
|
8
|
+
|
|
9
|
+
export function signalForObject(
|
|
10
|
+
obj: object,
|
|
11
|
+
key: string | symbol,
|
|
12
|
+
createSignal: () => Signal<unknown>,
|
|
13
|
+
): Signal<unknown> {
|
|
14
|
+
let sigs = signalCache.get(obj);
|
|
15
|
+
if (sigs === undefined) {
|
|
16
|
+
sigs = {};
|
|
17
|
+
signalCache.set(obj, sigs);
|
|
18
|
+
}
|
|
19
|
+
if (Object.hasOwn(sigs, key)) {
|
|
20
|
+
return sigs[key];
|
|
21
|
+
}
|
|
22
|
+
const sig = createSignal();
|
|
23
|
+
sigs[key] = sig;
|
|
24
|
+
return sig;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function reactive<C extends object, T>(
|
|
28
|
+
value: ClassAccessorDecoratorTarget<C, T>,
|
|
29
|
+
context: ClassAccessorDecoratorContext<C, T>,
|
|
30
|
+
): ClassAccessorDecoratorResult<C, T>;
|
|
31
|
+
export function reactive<C extends object, T>(
|
|
32
|
+
value: () => T,
|
|
33
|
+
context: ClassGetterDecoratorContext<C, T>,
|
|
34
|
+
): () => T;
|
|
35
|
+
export function reactive<C extends Component, T>(
|
|
36
|
+
value: undefined,
|
|
37
|
+
context: ClassFieldDecoratorContext<C, T>,
|
|
38
|
+
): undefined;
|
|
39
|
+
export function reactive<C extends object, T>(
|
|
40
|
+
value: ClassAccessorDecoratorTarget<C, T> | (() => T) | undefined,
|
|
41
|
+
context:
|
|
42
|
+
| ClassAccessorDecoratorContext<C, T>
|
|
43
|
+
| ClassGetterDecoratorContext<C, T>
|
|
44
|
+
| ClassFieldDecoratorContext<C, T>,
|
|
45
|
+
): ClassAccessorDecoratorResult<C, T> | ((this: C) => T) | undefined {
|
|
46
|
+
switch (context.kind) {
|
|
47
|
+
case "accessor": {
|
|
48
|
+
const getSig = (obj: object) =>
|
|
49
|
+
signalForObject(obj, context.name, () =>
|
|
50
|
+
Signal((value as ClassAccessorDecoratorTarget<unknown, T>).get.call(obj), {
|
|
51
|
+
equals: Object.is,
|
|
52
|
+
}),
|
|
53
|
+
) as Signal.State<T>;
|
|
54
|
+
return {
|
|
55
|
+
get() {
|
|
56
|
+
return getSig(this).get();
|
|
57
|
+
},
|
|
58
|
+
set(value: T) {
|
|
59
|
+
getSig(this).set(value);
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
case "getter": {
|
|
65
|
+
const getSig = (obj: object) =>
|
|
66
|
+
signalForObject(obj, context.name, () =>
|
|
67
|
+
Signal.computed(() => (value as () => T).call(obj), { equals: Object.is }),
|
|
68
|
+
);
|
|
69
|
+
return function get(this: C) {
|
|
70
|
+
return getSig(this).get() as T;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
case "field": {
|
|
75
|
+
context.metadata[reactiveFields] ??= new Set<string | symbol>();
|
|
76
|
+
(context.metadata[reactiveFields] as Set<string | symbol>).add(context.name);
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { sleep } from "@bodil/core/async";
|
|
2
|
+
import { customElement } from "lit/decorators.js";
|
|
3
|
+
import { expect, test } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { attribute, Component, reactive, require } from "../component";
|
|
6
|
+
|
|
7
|
+
test("@require", async () => {
|
|
8
|
+
let inits = 0,
|
|
9
|
+
updates = 0;
|
|
10
|
+
|
|
11
|
+
@customElement("require-test-class")
|
|
12
|
+
class RequireTestClass extends Component {
|
|
13
|
+
@require @reactive foo?: string;
|
|
14
|
+
@require @attribute accessor bar: string | undefined;
|
|
15
|
+
|
|
16
|
+
protected override initialised() {
|
|
17
|
+
inits++;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
protected override updated() {
|
|
21
|
+
updates++;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const c = document.createElement("require-test-class") as RequireTestClass;
|
|
26
|
+
c.foo = "foo";
|
|
27
|
+
document.body.append(c);
|
|
28
|
+
await sleep(1);
|
|
29
|
+
// no updates yet when only foo is set
|
|
30
|
+
expect(inits).toBe(0);
|
|
31
|
+
expect(updates).toBe(0);
|
|
32
|
+
c.bar = "bar";
|
|
33
|
+
await sleep(1);
|
|
34
|
+
// foo and bar are both set, should cause an update
|
|
35
|
+
expect(inits).toBe(1);
|
|
36
|
+
expect(updates).toBe(1);
|
|
37
|
+
c.foo = undefined;
|
|
38
|
+
await sleep(1);
|
|
39
|
+
// foo has gone undefined, shouldn't update
|
|
40
|
+
expect(inits).toBe(1);
|
|
41
|
+
expect(updates).toBe(1);
|
|
42
|
+
c.foo = "wibble";
|
|
43
|
+
await sleep(1);
|
|
44
|
+
// foo has stopped being undefined, should update
|
|
45
|
+
expect(inits).toBe(2);
|
|
46
|
+
expect(updates).toBe(2);
|
|
47
|
+
c.bar = "bar";
|
|
48
|
+
await sleep(1);
|
|
49
|
+
// bar has been redeclared with the same value, shouldn't update
|
|
50
|
+
expect(inits).toBe(2);
|
|
51
|
+
expect(updates).toBe(2);
|
|
52
|
+
c.bar = "wibble";
|
|
53
|
+
await sleep(1);
|
|
54
|
+
// bar has a new value, should update
|
|
55
|
+
expect(inits).toBe(3);
|
|
56
|
+
expect(updates).toBe(3);
|
|
57
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Component } from "../component";
|
|
2
|
+
|
|
3
|
+
export const requiredProperties = Symbol("requiredProperties");
|
|
4
|
+
|
|
5
|
+
export function require<C extends Component, T>(
|
|
6
|
+
_value: unknown,
|
|
7
|
+
context: ClassFieldDecoratorContext<C, T> | ClassAccessorDecoratorContext<C, T>,
|
|
8
|
+
): undefined {
|
|
9
|
+
context.metadata[requiredProperties] ??= new Set();
|
|
10
|
+
(context.metadata[requiredProperties] as Set<string | symbol>).add(context.name);
|
|
11
|
+
}
|