bunja 3.0.1 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@disjukr/bunja",
3
- "version": "3.0.1",
3
+ "version": "3.0.2",
4
4
  "license": "Zlib",
5
5
  "exports": {
6
6
  ".": "./bunja.ts",
package/dist/solid.cjs CHANGED
@@ -31,18 +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) {
38
35
  const owner = (0, solid_js.getOwner)();
39
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
+ };
40
41
  const readScope = (0, solid_js.createMemo)(() => {
41
42
  const pairs = access(scopeValuePairs);
42
- const impl = pairs ? require_bunja.createReadScopeFn(pairs, defaultReadScope) : defaultReadScope;
43
- return (scope) => (0, solid_js.runWithOwner)(owner, () => impl(scope));
43
+ return pairs ? require_bunja.createReadScopeFn(pairs, defaultReadScope) : defaultReadScope;
44
+ });
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;
44
54
  });
45
- const entry = (0, solid_js.createMemo)(() => store.get(access(bunja), readScope()));
46
55
  (0, solid_js.createEffect)(() => {
47
56
  const cleanup = entry().mount();
48
57
  (0, solid_js.onCleanup)(() => setTimeout(cleanup));
package/dist/solid.js CHANGED
@@ -29,18 +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) {
36
33
  const owner = getOwner();
37
34
  const store = useContext(BunjaStoreContext);
35
+ const defaultReadScope = (scope) => {
36
+ const context = scopeContextMap.get(scope);
37
+ return access(runWithOwner(owner, () => useContext(context)));
38
+ };
38
39
  const readScope = createMemo(() => {
39
40
  const pairs = access(scopeValuePairs);
40
- const impl = pairs ? createReadScopeFn(pairs, defaultReadScope) : defaultReadScope;
41
- return (scope) => runWithOwner(owner, () => impl(scope));
41
+ return pairs ? createReadScopeFn(pairs, defaultReadScope) : defaultReadScope;
42
+ });
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;
42
52
  });
43
- const entry = createMemo(() => store.get(access(bunja), readScope()));
44
53
  createEffect(() => {
45
54
  const cleanup = entry().mount();
46
55
  onCleanup(() => setTimeout(cleanup));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bunja",
3
3
  "type": "module",
4
- "version": "3.0.1",
4
+ "version": "3.0.2",
5
5
  "description": "State Lifetime Manager",
6
6
  "main": "dist/bunja.cjs",
7
7
  "module": "dist/bunja.js",
package/solid.ts CHANGED
@@ -78,25 +78,35 @@ export function createScopeFromContext<T>(
78
78
  return scope;
79
79
  }
80
80
 
81
- const defaultReadScope: ReadScope = <T>(scope: Scope<T>) => {
82
- const context = scopeContextMap.get(scope as Scope<unknown>)!;
83
- return access(useContext(context)) as T;
84
- };
85
-
86
81
  export function useBunja<T>(
87
82
  bunja: MaybeAccessor<Bunja<T, any> | BunjaGetRef<T, any>>,
88
83
  scopeValuePairs?: MaybeAccessor<ScopeValuePairs>,
89
84
  ): Accessor<T> {
90
85
  const owner = getOwner();
91
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
+ };
92
92
  const readScope = createMemo(() => {
93
93
  const pairs = access(scopeValuePairs);
94
- const impl = pairs
94
+ return pairs
95
95
  ? createReadScopeFn(pairs, defaultReadScope)
96
96
  : defaultReadScope;
97
- return <T>(scope: Scope<T>) => runWithOwner(owner, () => impl(scope)) as T;
98
97
  });
99
- const entry = createMemo(() => store.get(access(bunja), readScope()));
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
+ });
100
110
  createEffect(() => {
101
111
  const cleanup = entry().mount();
102
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
+ });