bunja 3.0.0 → 3.0.2
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/deno.json +1 -1
- package/dist/solid.cjs +15 -4
- package/dist/solid.js +16 -5
- package/package.json +1 -1
- package/solid.ts +19 -6
- package/solid_test.ts +166 -0
- package/test.ts +67 -0
package/deno.json
CHANGED
package/dist/solid.cjs
CHANGED
|
@@ -31,16 +31,27 @@ function createScopeFromContext(context, hash) {
|
|
|
31
31
|
bindScope(scope, context);
|
|
32
32
|
return scope;
|
|
33
33
|
}
|
|
34
|
-
const defaultReadScope = (scope) => {
|
|
35
|
-
return access((0, solid_js.useContext)(scopeContextMap.get(scope)));
|
|
36
|
-
};
|
|
37
34
|
function useBunja(bunja, scopeValuePairs) {
|
|
35
|
+
const owner = (0, solid_js.getOwner)();
|
|
38
36
|
const store = (0, solid_js.useContext)(BunjaStoreContext);
|
|
37
|
+
const defaultReadScope = (scope) => {
|
|
38
|
+
const context = scopeContextMap.get(scope);
|
|
39
|
+
return access((0, solid_js.runWithOwner)(owner, () => (0, solid_js.useContext)(context)));
|
|
40
|
+
};
|
|
39
41
|
const readScope = (0, solid_js.createMemo)(() => {
|
|
40
42
|
const pairs = access(scopeValuePairs);
|
|
41
43
|
return pairs ? require_bunja.createReadScopeFn(pairs, defaultReadScope) : defaultReadScope;
|
|
42
44
|
});
|
|
43
|
-
const entry = (0, solid_js.createMemo)(() =>
|
|
45
|
+
const entry = (0, solid_js.createMemo)(() => {
|
|
46
|
+
const read = readScope();
|
|
47
|
+
const scopes = /* @__PURE__ */ new Set();
|
|
48
|
+
const result = store.get(access(bunja), (scope) => {
|
|
49
|
+
scopes.add(scope);
|
|
50
|
+
return read(scope);
|
|
51
|
+
});
|
|
52
|
+
for (const scope of scopes) read(scope);
|
|
53
|
+
return result;
|
|
54
|
+
});
|
|
44
55
|
(0, solid_js.createEffect)(() => {
|
|
45
56
|
const cleanup = entry().mount();
|
|
46
57
|
(0, solid_js.onCleanup)(() => setTimeout(cleanup));
|
package/dist/solid.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createBunjaStore, createReadScopeFn, createScope } from "./bunja-BmPnlAec.js";
|
|
2
|
-
import { createComponent, createContext, createEffect, createMemo, createRoot, getOwner, onCleanup, useContext } from "solid-js";
|
|
2
|
+
import { createComponent, createContext, createEffect, createMemo, createRoot, getOwner, onCleanup, runWithOwner, useContext } from "solid-js";
|
|
3
3
|
|
|
4
4
|
//#region solid.ts
|
|
5
5
|
function access(maybeAccessor) {
|
|
@@ -29,16 +29,27 @@ function createScopeFromContext(context, hash) {
|
|
|
29
29
|
bindScope(scope, context);
|
|
30
30
|
return scope;
|
|
31
31
|
}
|
|
32
|
-
const defaultReadScope = (scope) => {
|
|
33
|
-
return access(useContext(scopeContextMap.get(scope)));
|
|
34
|
-
};
|
|
35
32
|
function useBunja(bunja, scopeValuePairs) {
|
|
33
|
+
const owner = getOwner();
|
|
36
34
|
const store = useContext(BunjaStoreContext);
|
|
35
|
+
const defaultReadScope = (scope) => {
|
|
36
|
+
const context = scopeContextMap.get(scope);
|
|
37
|
+
return access(runWithOwner(owner, () => useContext(context)));
|
|
38
|
+
};
|
|
37
39
|
const readScope = createMemo(() => {
|
|
38
40
|
const pairs = access(scopeValuePairs);
|
|
39
41
|
return pairs ? createReadScopeFn(pairs, defaultReadScope) : defaultReadScope;
|
|
40
42
|
});
|
|
41
|
-
const entry = createMemo(() =>
|
|
43
|
+
const entry = createMemo(() => {
|
|
44
|
+
const read = readScope();
|
|
45
|
+
const scopes = /* @__PURE__ */ new Set();
|
|
46
|
+
const result = store.get(access(bunja), (scope) => {
|
|
47
|
+
scopes.add(scope);
|
|
48
|
+
return read(scope);
|
|
49
|
+
});
|
|
50
|
+
for (const scope of scopes) read(scope);
|
|
51
|
+
return result;
|
|
52
|
+
});
|
|
42
53
|
createEffect(() => {
|
|
43
54
|
const cleanup = entry().mount();
|
|
44
55
|
onCleanup(() => setTimeout(cleanup));
|
package/package.json
CHANGED
package/solid.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
type JSX,
|
|
11
11
|
onCleanup,
|
|
12
12
|
type ParentProps,
|
|
13
|
+
runWithOwner,
|
|
13
14
|
useContext,
|
|
14
15
|
} from "solid-js";
|
|
15
16
|
import {
|
|
@@ -77,23 +78,35 @@ export function createScopeFromContext<T>(
|
|
|
77
78
|
return scope;
|
|
78
79
|
}
|
|
79
80
|
|
|
80
|
-
const defaultReadScope: ReadScope = <T>(scope: Scope<T>) => {
|
|
81
|
-
const context = scopeContextMap.get(scope as Scope<unknown>)!;
|
|
82
|
-
return access(useContext(context)) as T;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
81
|
export function useBunja<T>(
|
|
86
82
|
bunja: MaybeAccessor<Bunja<T, any> | BunjaGetRef<T, any>>,
|
|
87
83
|
scopeValuePairs?: MaybeAccessor<ScopeValuePairs>,
|
|
88
84
|
): Accessor<T> {
|
|
85
|
+
const owner = getOwner();
|
|
89
86
|
const store = useContext(BunjaStoreContext);
|
|
87
|
+
const defaultReadScope: ReadScope = <T>(scope: Scope<T>) => {
|
|
88
|
+
const context = scopeContextMap.get(scope as Scope<unknown>)!;
|
|
89
|
+
// Restore the consumer context without untracking its accessor.
|
|
90
|
+
return access(runWithOwner(owner, () => useContext(context))) as T;
|
|
91
|
+
};
|
|
90
92
|
const readScope = createMemo(() => {
|
|
91
93
|
const pairs = access(scopeValuePairs);
|
|
92
94
|
return pairs
|
|
93
95
|
? createReadScopeFn(pairs, defaultReadScope)
|
|
94
96
|
: defaultReadScope;
|
|
95
97
|
});
|
|
96
|
-
const entry = createMemo(() =>
|
|
98
|
+
const entry = createMemo(() => {
|
|
99
|
+
const read = readScope();
|
|
100
|
+
const scopes = new Set<Scope<unknown>>();
|
|
101
|
+
const result = store.get(access(bunja), (scope) => {
|
|
102
|
+
scopes.add(scope as Scope<unknown>);
|
|
103
|
+
return read(scope);
|
|
104
|
+
});
|
|
105
|
+
// Initial scope discovery can run inside the instance's untracked root.
|
|
106
|
+
// Subscribe here as well, including scopes reached through dependencies.
|
|
107
|
+
for (const scope of scopes) read(scope);
|
|
108
|
+
return result;
|
|
109
|
+
});
|
|
97
110
|
createEffect(() => {
|
|
98
111
|
const cleanup = entry().mount();
|
|
99
112
|
onCleanup(() => setTimeout(cleanup));
|
package/solid_test.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertEquals,
|
|
3
|
+
assertNotStrictEquals,
|
|
4
|
+
assertStrictEquals,
|
|
5
|
+
} from "@std/assert";
|
|
6
|
+
import {
|
|
7
|
+
createComponent,
|
|
8
|
+
createContext,
|
|
9
|
+
createRoot,
|
|
10
|
+
createSignal,
|
|
11
|
+
} from "solid-js";
|
|
12
|
+
import { bunja, createScope } from "./bunja.ts";
|
|
13
|
+
import {
|
|
14
|
+
bindScope,
|
|
15
|
+
BunjaStoreProvider,
|
|
16
|
+
createScopeFromContext,
|
|
17
|
+
useBunja,
|
|
18
|
+
} from "./solid.ts";
|
|
19
|
+
|
|
20
|
+
const settle = () => new Promise((resolve) => setTimeout(resolve, 20));
|
|
21
|
+
|
|
22
|
+
for (
|
|
23
|
+
const mode of ["context accessor", "bindScope accessor", "explicit binding"]
|
|
24
|
+
) {
|
|
25
|
+
const explicit = mode === "explicit binding";
|
|
26
|
+
Deno.test(`Solid ${mode} lifecycle`, async () => {
|
|
27
|
+
const Context = createContext<string | (() => string)>(() => "default");
|
|
28
|
+
const Scope = mode === "bindScope accessor"
|
|
29
|
+
? createScope<string>()
|
|
30
|
+
: createScopeFromContext(Context);
|
|
31
|
+
if (mode === "bindScope accessor") bindScope(Scope, Context);
|
|
32
|
+
const events: string[] = [];
|
|
33
|
+
const model = bunja(() => {
|
|
34
|
+
const id = bunja.use(Scope);
|
|
35
|
+
bunja.effect(() => {
|
|
36
|
+
events.push(`start:${id}`);
|
|
37
|
+
return () => {
|
|
38
|
+
events.push(`stop:${id}`);
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
return { id };
|
|
42
|
+
});
|
|
43
|
+
// Exercise transitive scope discovery, including the first unbaked instance.
|
|
44
|
+
const parent = bunja(() => bunja.use(model));
|
|
45
|
+
let value!: () => { id: string };
|
|
46
|
+
let other!: typeof value;
|
|
47
|
+
let change!: (id: string) => void;
|
|
48
|
+
let dispose!: () => void;
|
|
49
|
+
let unmount!: () => void;
|
|
50
|
+
createRoot((cleanup) => {
|
|
51
|
+
dispose = cleanup;
|
|
52
|
+
const [id, setId] = createSignal("a");
|
|
53
|
+
change = setId;
|
|
54
|
+
createComponent(BunjaStoreProvider, {
|
|
55
|
+
get children() {
|
|
56
|
+
return createComponent(Context.Provider, {
|
|
57
|
+
value: id,
|
|
58
|
+
get children() {
|
|
59
|
+
const bindings = explicit ? () => [Scope.bind(id())] : undefined;
|
|
60
|
+
value = useBunja(parent, bindings);
|
|
61
|
+
createRoot((cleanup) => {
|
|
62
|
+
unmount = cleanup;
|
|
63
|
+
other = useBunja(parent, bindings);
|
|
64
|
+
});
|
|
65
|
+
return null;
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
try {
|
|
72
|
+
await settle();
|
|
73
|
+
const first = value();
|
|
74
|
+
assertEquals(first.id, "a");
|
|
75
|
+
assertStrictEquals(other(), first);
|
|
76
|
+
assertEquals(events, ["start:a"]);
|
|
77
|
+
change("a");
|
|
78
|
+
await settle();
|
|
79
|
+
assertStrictEquals(value(), first);
|
|
80
|
+
for (const id of ["b", "a"]) {
|
|
81
|
+
const previous = value();
|
|
82
|
+
change(id);
|
|
83
|
+
await settle();
|
|
84
|
+
assertEquals(value().id, id);
|
|
85
|
+
assertNotStrictEquals(value(), previous);
|
|
86
|
+
assertStrictEquals(other(), value());
|
|
87
|
+
}
|
|
88
|
+
assertEquals(events, [
|
|
89
|
+
"start:a",
|
|
90
|
+
"start:b",
|
|
91
|
+
"stop:a",
|
|
92
|
+
"start:a",
|
|
93
|
+
"stop:b",
|
|
94
|
+
]);
|
|
95
|
+
unmount();
|
|
96
|
+
await settle();
|
|
97
|
+
assertEquals(events.length, 5);
|
|
98
|
+
} finally {
|
|
99
|
+
unmount();
|
|
100
|
+
dispose();
|
|
101
|
+
await settle();
|
|
102
|
+
}
|
|
103
|
+
assertEquals(events, [
|
|
104
|
+
"start:a",
|
|
105
|
+
"start:b",
|
|
106
|
+
"stop:a",
|
|
107
|
+
"start:a",
|
|
108
|
+
"stop:b",
|
|
109
|
+
"stop:a",
|
|
110
|
+
]);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
Deno.test("Solid bindScope, explicit precedence and mixed context fallback", async () => {
|
|
115
|
+
const Context = createContext<string | null | (() => string | null)>(null);
|
|
116
|
+
const Scope = createScope<string | null>();
|
|
117
|
+
bindScope(Scope, Context);
|
|
118
|
+
const Other = createContext((): string => "default");
|
|
119
|
+
const OtherScope = createScopeFromContext(Other);
|
|
120
|
+
const model = bunja(() => ({
|
|
121
|
+
id: bunja.use(Scope),
|
|
122
|
+
other: bunja.use(OtherScope),
|
|
123
|
+
}));
|
|
124
|
+
let value!: () => { id: string | null; other: string };
|
|
125
|
+
let change!: (value: string) => void;
|
|
126
|
+
let override!: (value: boolean) => void;
|
|
127
|
+
let dispose!: () => void;
|
|
128
|
+
createRoot((cleanup) => {
|
|
129
|
+
dispose = cleanup;
|
|
130
|
+
const [id, setId] = createSignal("a");
|
|
131
|
+
const [explicit, setExplicit] = createSignal(true);
|
|
132
|
+
change = setId;
|
|
133
|
+
override = setExplicit;
|
|
134
|
+
createComponent(BunjaStoreProvider, {
|
|
135
|
+
get children() {
|
|
136
|
+
return createComponent(Other.Provider, {
|
|
137
|
+
value: id,
|
|
138
|
+
get children() {
|
|
139
|
+
value = useBunja(
|
|
140
|
+
model,
|
|
141
|
+
() => explicit() ? [OtherScope.bind("override")] : [],
|
|
142
|
+
);
|
|
143
|
+
return null;
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
try {
|
|
150
|
+
await settle();
|
|
151
|
+
const first = value();
|
|
152
|
+
assertEquals(first, { id: null, other: "override" });
|
|
153
|
+
change("b");
|
|
154
|
+
await settle();
|
|
155
|
+
assertStrictEquals(value(), first);
|
|
156
|
+
override(false);
|
|
157
|
+
await settle();
|
|
158
|
+
assertEquals(value(), { id: null, other: "b" });
|
|
159
|
+
change("c");
|
|
160
|
+
await settle();
|
|
161
|
+
assertEquals(value(), { id: null, other: "c" });
|
|
162
|
+
} finally {
|
|
163
|
+
dispose();
|
|
164
|
+
await settle();
|
|
165
|
+
}
|
|
166
|
+
});
|
package/test.ts
CHANGED
|
@@ -81,6 +81,39 @@ Deno.test({
|
|
|
81
81
|
},
|
|
82
82
|
});
|
|
83
83
|
|
|
84
|
+
Deno.test({
|
|
85
|
+
name: "dependency effects mount and cleanup before consumer effects",
|
|
86
|
+
fn() {
|
|
87
|
+
const store = createBunjaStore();
|
|
88
|
+
const order: string[] = [];
|
|
89
|
+
const dependencyBunja = bunja(() => {
|
|
90
|
+
bunja.effect(() => {
|
|
91
|
+
order.push("dependency mount");
|
|
92
|
+
return () => order.push("dependency cleanup");
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
const consumerBunja = bunja(() => {
|
|
96
|
+
bunja.use(dependencyBunja);
|
|
97
|
+
bunja.effect(() => {
|
|
98
|
+
order.push("consumer mount");
|
|
99
|
+
return () => order.push("consumer cleanup");
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const { mount } = store.get(consumerBunja, readNull);
|
|
104
|
+
const cleanup = mount();
|
|
105
|
+
assertEquals(order, ["dependency mount", "consumer mount"]);
|
|
106
|
+
|
|
107
|
+
cleanup();
|
|
108
|
+
assertEquals(order, [
|
|
109
|
+
"dependency mount",
|
|
110
|
+
"consumer mount",
|
|
111
|
+
"dependency cleanup",
|
|
112
|
+
"consumer cleanup",
|
|
113
|
+
]);
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
|
|
84
117
|
Deno.test({
|
|
85
118
|
name: "A mount first, B mount later & A unmount first, B unmount later",
|
|
86
119
|
fn() {
|
|
@@ -480,6 +513,40 @@ Deno.test({
|
|
|
480
513
|
},
|
|
481
514
|
});
|
|
482
515
|
|
|
516
|
+
Deno.test({
|
|
517
|
+
name: "active bunja.will dependency effects cleanup before consumer effects",
|
|
518
|
+
fn() {
|
|
519
|
+
const store = createBunjaStore();
|
|
520
|
+
const order: string[] = [];
|
|
521
|
+
const dependencyBunja = bunja(() => {
|
|
522
|
+
bunja.effect(() => {
|
|
523
|
+
order.push("dependency mount");
|
|
524
|
+
return () => order.push("dependency cleanup");
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
const consumerBunja = bunja(() => {
|
|
528
|
+
const getDependency = bunja.will(dependencyBunja);
|
|
529
|
+
getDependency();
|
|
530
|
+
bunja.effect(() => {
|
|
531
|
+
order.push("consumer mount");
|
|
532
|
+
return () => order.push("consumer cleanup");
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
const { mount } = store.get(consumerBunja, readNull);
|
|
537
|
+
const cleanup = mount();
|
|
538
|
+
assertEquals(order, ["dependency mount", "consumer mount"]);
|
|
539
|
+
|
|
540
|
+
cleanup();
|
|
541
|
+
assertEquals(order, [
|
|
542
|
+
"dependency mount",
|
|
543
|
+
"consumer mount",
|
|
544
|
+
"dependency cleanup",
|
|
545
|
+
"consumer cleanup",
|
|
546
|
+
]);
|
|
547
|
+
},
|
|
548
|
+
});
|
|
549
|
+
|
|
483
550
|
Deno.test({
|
|
484
551
|
name: "bunja.will records optional refs once when thunk is used",
|
|
485
552
|
fn() {
|