@bodil/dom 0.1.10 → 0.2.1

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.
@@ -1,85 +0,0 @@
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
- }