bansa 0.0.10 → 0.0.12
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/dist/index.browser.js +1 -1
- package/dist/index.d.ts +5 -8
- package/dist/index.js +42 -51
- package/dist/react.d.ts +3 -2
- package/dist/react.js +3 -5
- package/package.json +7 -7
- package/tests/bansa.test.ts +116 -2
package/dist/index.browser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var i=function(){};i.prototype.get=function(){if(this.o||(I(this),V(this)),this.state.promise)throw this.state.promise;if(this.state.error)throw this.state.error;return this.state.value};i.prototype.watch=function(e){return this.o||x(this),(this.i??=new Set).add(e),()=>{this.i.delete(e),this.i.size||V(this)}};i.prototype.subscribe=function(e){let t={y:e,c:{get signal(){return(t.t??=_()).signal}}};if(!this.o)x(this);else if(!this.state.promise&&!this.state.error)try{e(this.state.value,t.c)}catch(o){y(o)}return(this.s??=new Set).add(t),()=>{this.s.delete(t),t.t&&(t.t.abort(),t.t=void 0),this.s.size||V(this)}};i.prototype[Symbol.toPrimitive]=function(){return this.state.value};var c=function(e,t){this.u=e,this.f=t?.equals,this.state={promise:void 0,error:void 0,value:e},this.state.value=this.a=e};c.prototype.set=function(e){let t=e instanceof Function?e(this.a):e;S(t,this.state.value,this.f)||(this.a=t,d(this))};c.prototype.m=!0;c.prototype.o=!0;c.prototype.n=!1;Object.setPrototypeOf(c.prototype,i.prototype);var f=function(e,t){this.u=e,this.f=t?.equals,this.h=t?.persist,this.state={promise:P,error:void 0,value:void 0};let o=this;this.c={get signal(){return(o.t??=_()).signal}}};f.prototype.m=!1;f.prototype.o=!1;f.prototype.n=!1;f.prototype.l=0;Object.setPrototypeOf(f.prototype,i.prototype);var A=()=>A,D=()=>{};Object.setPrototypeOf(A,new Proxy(A,{get:(e,t)=>t===Symbol.toPrimitive?D:A}));var P=Promise.resolve(),m=(e,t)=>e instanceof Function?new f(e,t):new c(e,t),C=e=>m((t,o)=>{let r,n,a=e((s,h)=>{try{return t(s,h)}catch(l){if(!l)throw l;w(l)?(r??=[]).push(l):n=l}return A},o);if(n)throw n;if(r)throw Promise.all(r);return a},{equals:q}),L=(e,t)=>{let o=new WeakMap;if(t)for(let[n,a]of t)o.set(n,a instanceof i?a:m(a));let r=((n,a=!0)=>{let s=o.get(n);return a&&!(s||e&&(s=e(n,!1)))&&o.set(n,s=n.u instanceof Function?m((h,l)=>n.u((O,G)=>h(r(O),G),l)):m(n.u)),s});return r},q=(e,t)=>{if(typeof e!="object"||typeof t!="object"||!e||!t)return!1;let o=e.constructor;if(o!==t.constructor)return!1;if(o===Array){let n=e.length;if(n!==t.length)return!1;for(;(n=n-1|0)>=0;)if(!Object.is(e[n],t[n]))return!1;return!0}let r=0;for(let n in e){if(!(n in t&&Object.is(e[n],t[n])))return!1;r=r+1|0}for(let n in t)if((r=r-1|0)<0)return!1;return!0},b=!1,u=[],x=e=>{e.p||(e.p=!0,d(e))},d=e=>{e.n||(e.n=!0,u.push(e),b||(b=!0,queueMicrotask(T)))},T=()=>{b=!1;{let t=u;u=[];for(let o of t)o.state.promise=void 0,o.state.error=o.A,o.state.value=o.a,k(o)}let e=u;u=[];for(let t=e.length;t--;){let o=e[t];o.d=!1,o.p&&(o.n=!0,I(o)),o.n&&g(o)}},g=e=>{if(e.n=!1,e.r)for(let t of e.r)t.p=!0;if(e.i)for(let t of e.i)t();if(e.s&&!e.state.promise&&!e.state.error)for(let t of e.s){t.t&&(t.t.abort(),t.t=void 0);try{t.y(e.state.value,t.c)}catch(o){y(o)}}},k=e=>{if(!e.d){if(e.d=!0,e.r)for(let t of e.r)k(t);u.push(e)}},p=class{e;constructor(t){this.e=t}},I=e=>{let t=++e.l;e.o=!0,e.p=!1,e.state.promise=void 0,e.t&&(e.t.abort(),e.t=void 0);let o=e.V;o&&(e.V=new Set);try{let r=e.u((n,a=!0)=>{if(t!==e.l)throw void 0;if(e!==n&&(n.o||(I(n),n.n&&(n.n=!1,g(n))),o?.delete(n),(e.V??=new Set).add(n),(n.r??=new Set).add(e)),!a)return n.state;if(n.state.promise)throw new p(n.state.promise);if(n.state.error)throw new p(n.state.error);return n.state.value},e.c);w(r)?(e.state.promise=r,r.then(n=>{t===e.l&&(S(n,e.state.value,e.f)?e.state.promise=void 0:(e.a=n,e.A=void 0,d(e)))},n=>{t===e.l&&(n instanceof Promise?e.state.promise=void 0:(n instanceof p?n=n.e:y(n),e.A=n,d(e)))})):(++e.l,e.state.error=void 0,S(r,e.state.value,e.f)?e.n=!1:e.state.value=e.a=r)}catch(r){++e.l,r?(r instanceof p?r=r.e:y(r),w(r)?e.state.promise=r:e.state.error=r):e.n=!1}if(o)for(let r of o)r.r.delete(e),V(r)},v=!1,V=e=>{if(!e.m&&!e.h&&!e.r?.size&&!e.i?.size&&!e.s?.size){if(!v){setTimeout(()=>{v=!0,V(e),v=!1},0);return}if(e.state.promise=P,e.a=e.A=e.state.error=e.state.value=void 0,e.n=e.p=e.o=!1,e.t&&(e.t.abort(),e.t=void 0),e.V){for(let t of e.V)t.r.delete(e),V(t);e.V.clear()}}},S=(e,t,o)=>Object.is(e,t)||o!==void 0&&t!==void 0&&o(e,t),w=e=>typeof e?.then=="function",_=()=>{let e=new AbortController,t=e.signal,o=new Promise(r=>{t.then=n=>o.then(n),t.addEventListener("abort",r,{once:!0,passive:!0})});return{abort:()=>e.abort(),signal:t}},y=e=>{queueMicrotask(()=>{throw e})};export{m as $,C as $$,L as createScope,P as inactive};
|
package/dist/index.d.ts
CHANGED
|
@@ -65,18 +65,15 @@ export type AtomScope = {
|
|
|
65
65
|
<Value>(baseAtom: PrimitiveAtom<Value>): PrimitiveAtom<Value>;
|
|
66
66
|
<Value>(baseAtom: DerivedAtom<Value>): DerivedAtom<Value>;
|
|
67
67
|
<Value>(baseAtom: Atom<Value>): Atom<Value>;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
<Value>(baseAtom: DerivedAtom<Value>): DerivedAtom<Value>;
|
|
72
|
-
<Value>(baseAtom: Atom<Value>): Atom<Value>;
|
|
73
|
-
};
|
|
68
|
+
<Value>(baseAtom: PrimitiveAtom<Value>, create: false): PrimitiveAtom<Value> | undefined;
|
|
69
|
+
<Value>(baseAtom: DerivedAtom<Value>, create: false): DerivedAtom<Value> | undefined;
|
|
70
|
+
<Value>(baseAtom: Atom<Value>, create: false): Atom<Value> | undefined;
|
|
74
71
|
};
|
|
75
72
|
export type SetLike<Key> = Key[] | Set<Key> | (Key extends object ? WeakSet<Key> : never);
|
|
76
73
|
export type MapLike<Key, Value> = Map<Key, Value> | (Key extends object ? WeakMap<Key, Value> : never) | (Key extends string | number | symbol ? Record<Key, Value> : never);
|
|
77
74
|
export declare const inactive: Promise<void>;
|
|
78
75
|
export declare const $: CreateAtom;
|
|
79
76
|
export declare const $$: <Value>(init: AtomGetter<Value>) => DerivedAtom<Value>;
|
|
80
|
-
type
|
|
81
|
-
export declare const createScope:
|
|
77
|
+
export type AtomValuePair<Value> = [Atom<Value>, Value | Atom<Value>];
|
|
78
|
+
export declare const createScope: (parentScope?: AtomScope | null, atomValuePairs?: AtomValuePair<unknown>[]) => AtomScope;
|
|
82
79
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -13,10 +13,10 @@ AtomPrototype.prototype.watch = function(watcher) {
|
|
|
13
13
|
if (!this.c) {
|
|
14
14
|
requestActivate(this);
|
|
15
15
|
}
|
|
16
|
-
(this.
|
|
16
|
+
(this.h ??= /* @__PURE__ */ new Set()).add(watcher);
|
|
17
17
|
return () => {
|
|
18
|
-
this.
|
|
19
|
-
if (!this.
|
|
18
|
+
this.h.delete(watcher);
|
|
19
|
+
if (!this.h.size) {
|
|
20
20
|
disableAtom(this);
|
|
21
21
|
}
|
|
22
22
|
};
|
|
@@ -39,14 +39,14 @@ AtomPrototype.prototype.subscribe = function(subscriber) {
|
|
|
39
39
|
logError(e);
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
(this.
|
|
42
|
+
(this.i ??= /* @__PURE__ */ new Set()).add(atomSubscriber);
|
|
43
43
|
return () => {
|
|
44
|
-
this.
|
|
44
|
+
this.i.delete(atomSubscriber);
|
|
45
45
|
if (atomSubscriber.a) {
|
|
46
46
|
atomSubscriber.a.abort();
|
|
47
47
|
atomSubscriber.a = void 0;
|
|
48
48
|
}
|
|
49
|
-
if (!this.
|
|
49
|
+
if (!this.i.size) {
|
|
50
50
|
disableAtom(this);
|
|
51
51
|
}
|
|
52
52
|
};
|
|
@@ -55,19 +55,19 @@ AtomPrototype.prototype[Symbol.toPrimitive] = function() {
|
|
|
55
55
|
return this.state.value;
|
|
56
56
|
};
|
|
57
57
|
const PrimitiveAtomPrototype = function(init, options) {
|
|
58
|
-
this.
|
|
58
|
+
this.j = init;
|
|
59
59
|
this.n = options?.equals;
|
|
60
60
|
this.state = {
|
|
61
61
|
promise: void 0,
|
|
62
62
|
error: void 0,
|
|
63
63
|
value: init
|
|
64
64
|
};
|
|
65
|
-
this.state.value = this.
|
|
65
|
+
this.state.value = this.f = init;
|
|
66
66
|
};
|
|
67
67
|
PrimitiveAtomPrototype.prototype.set = function(value) {
|
|
68
|
-
const nextValue = value instanceof Function ? value(this.
|
|
68
|
+
const nextValue = value instanceof Function ? value(this.f) : value;
|
|
69
69
|
if (!equals(nextValue, this.state.value, this.n)) {
|
|
70
|
-
this.
|
|
70
|
+
this.f = nextValue;
|
|
71
71
|
requestPropagate(this);
|
|
72
72
|
}
|
|
73
73
|
};
|
|
@@ -79,7 +79,7 @@ Object.setPrototypeOf(
|
|
|
79
79
|
AtomPrototype.prototype
|
|
80
80
|
);
|
|
81
81
|
const DerivedAtomPrototype = function(init, options) {
|
|
82
|
-
this.
|
|
82
|
+
this.j = init;
|
|
83
83
|
this.n = options?.equals;
|
|
84
84
|
this.s = options?.persist;
|
|
85
85
|
this.state = {
|
|
@@ -97,7 +97,7 @@ const DerivedAtomPrototype = function(init, options) {
|
|
|
97
97
|
DerivedAtomPrototype.prototype.p = false;
|
|
98
98
|
DerivedAtomPrototype.prototype.c = false;
|
|
99
99
|
DerivedAtomPrototype.prototype.b = false;
|
|
100
|
-
DerivedAtomPrototype.prototype.
|
|
100
|
+
DerivedAtomPrototype.prototype.g = 0;
|
|
101
101
|
Object.setPrototypeOf(DerivedAtomPrototype.prototype, AtomPrototype.prototype);
|
|
102
102
|
const ouroboros = () => ouroboros;
|
|
103
103
|
const toUndefined = () => void 0;
|
|
@@ -116,9 +116,9 @@ const $ = (init, options) => {
|
|
|
116
116
|
const $$ = (init) => $((get, options) => {
|
|
117
117
|
let promises;
|
|
118
118
|
let error;
|
|
119
|
-
const result = init((atom) => {
|
|
119
|
+
const result = init((atom, unwrap) => {
|
|
120
120
|
try {
|
|
121
|
-
return get(atom);
|
|
121
|
+
return get(atom, unwrap);
|
|
122
122
|
} catch (e) {
|
|
123
123
|
if (!e) {
|
|
124
124
|
throw e;
|
|
@@ -137,31 +137,22 @@ const $$ = (init) => $((get, options) => {
|
|
|
137
137
|
}, {
|
|
138
138
|
equals: shallowEquals
|
|
139
139
|
});
|
|
140
|
-
const createScope = (parentScope,
|
|
140
|
+
const createScope = (parentScope, atomValuePairs) => {
|
|
141
141
|
const scopeMap = /* @__PURE__ */ new WeakMap();
|
|
142
|
-
if (
|
|
143
|
-
for (const [atom, value] of
|
|
144
|
-
scopeMap.set(atom, $(value));
|
|
142
|
+
if (atomValuePairs) {
|
|
143
|
+
for (const [atom, value] of atomValuePairs) {
|
|
144
|
+
scopeMap.set(atom, value instanceof AtomPrototype ? value : $(value));
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
let scopedAtom = scopeMap.get(atom);
|
|
155
|
-
if (!scopedAtom) {
|
|
156
|
-
if (parentScope) scopedAtom = parentScope(atom);
|
|
157
|
-
else if (parentScope !== null) scopedAtom = atom;
|
|
158
|
-
else scopeMap.set(atom, scopedAtom = $(atom.f));
|
|
159
|
-
}
|
|
160
|
-
return get(scopedAtom);
|
|
161
|
-
}, options)
|
|
162
|
-
);
|
|
163
|
-
return $(baseAtom.f);
|
|
147
|
+
const scope = ((baseAtom, create = true) => {
|
|
148
|
+
let scopedAtom = scopeMap.get(baseAtom);
|
|
149
|
+
if (create && !(scopedAtom || parentScope && (scopedAtom = parentScope(baseAtom, false)))) scopeMap.set(baseAtom, scopedAtom = baseAtom.j instanceof Function ? $((get, options) => baseAtom.j(
|
|
150
|
+
(atom, unwrap) => get(scope(atom), unwrap),
|
|
151
|
+
options
|
|
152
|
+
)) : $(baseAtom.j));
|
|
153
|
+
return scopedAtom;
|
|
164
154
|
});
|
|
155
|
+
return scope;
|
|
165
156
|
};
|
|
166
157
|
const shallowEquals = (a, b) => {
|
|
167
158
|
if (typeof a !== "object" || typeof b !== "object" || !a || !b) return false;
|
|
@@ -207,7 +198,7 @@ const updateAtoms = () => {
|
|
|
207
198
|
for (const atom of updatedAtoms) {
|
|
208
199
|
atom.state.promise = void 0;
|
|
209
200
|
atom.state.error = atom.o;
|
|
210
|
-
atom.state.value = atom.
|
|
201
|
+
atom.state.value = atom.f;
|
|
211
202
|
mark(atom);
|
|
212
203
|
}
|
|
213
204
|
}
|
|
@@ -232,13 +223,13 @@ const propagate = (atom) => {
|
|
|
232
223
|
child.k = true;
|
|
233
224
|
}
|
|
234
225
|
}
|
|
235
|
-
if (atom.
|
|
236
|
-
for (const watcher of atom.
|
|
226
|
+
if (atom.h) {
|
|
227
|
+
for (const watcher of atom.h) {
|
|
237
228
|
watcher();
|
|
238
229
|
}
|
|
239
230
|
}
|
|
240
|
-
if (atom.
|
|
241
|
-
for (const subscriber of atom.
|
|
231
|
+
if (atom.i && !atom.state.promise && !atom.state.error) {
|
|
232
|
+
for (const subscriber of atom.i) {
|
|
242
233
|
if (subscriber.a) {
|
|
243
234
|
subscriber.a.abort();
|
|
244
235
|
subscriber.a = void 0;
|
|
@@ -269,7 +260,7 @@ class Wrapped {
|
|
|
269
260
|
}
|
|
270
261
|
}
|
|
271
262
|
const execute = (atom) => {
|
|
272
|
-
const counter = ++atom.
|
|
263
|
+
const counter = ++atom.g;
|
|
273
264
|
atom.c = true;
|
|
274
265
|
atom.k = false;
|
|
275
266
|
atom.state.promise = void 0;
|
|
@@ -282,9 +273,9 @@ const execute = (atom) => {
|
|
|
282
273
|
atom.l = /* @__PURE__ */ new Set();
|
|
283
274
|
}
|
|
284
275
|
try {
|
|
285
|
-
const value = atom.
|
|
276
|
+
const value = atom.j(
|
|
286
277
|
(anotherAtom, unwrap = true) => {
|
|
287
|
-
if (counter !== atom.
|
|
278
|
+
if (counter !== atom.g) throw void 0;
|
|
288
279
|
if (atom !== anotherAtom) {
|
|
289
280
|
if (!anotherAtom.c) {
|
|
290
281
|
execute(anotherAtom);
|
|
@@ -310,18 +301,18 @@ const execute = (atom) => {
|
|
|
310
301
|
atom.state.promise = value;
|
|
311
302
|
value.then(
|
|
312
303
|
(value2) => {
|
|
313
|
-
if (counter === atom.
|
|
304
|
+
if (counter === atom.g) {
|
|
314
305
|
if (equals(value2, atom.state.value, atom.n)) {
|
|
315
306
|
atom.state.promise = void 0;
|
|
316
307
|
} else {
|
|
317
|
-
atom.
|
|
308
|
+
atom.f = value2;
|
|
318
309
|
atom.o = void 0;
|
|
319
310
|
requestPropagate(atom);
|
|
320
311
|
}
|
|
321
312
|
}
|
|
322
313
|
},
|
|
323
314
|
(e) => {
|
|
324
|
-
if (counter === atom.
|
|
315
|
+
if (counter === atom.g) {
|
|
325
316
|
if (e instanceof Promise) {
|
|
326
317
|
atom.state.promise = void 0;
|
|
327
318
|
} else {
|
|
@@ -337,16 +328,16 @@ const execute = (atom) => {
|
|
|
337
328
|
}
|
|
338
329
|
);
|
|
339
330
|
} else {
|
|
340
|
-
++atom.
|
|
331
|
+
++atom.g;
|
|
341
332
|
atom.state.error = void 0;
|
|
342
333
|
if (equals(value, atom.state.value, atom.n)) {
|
|
343
334
|
atom.b = false;
|
|
344
335
|
} else {
|
|
345
|
-
atom.state.value = atom.
|
|
336
|
+
atom.state.value = atom.f = value;
|
|
346
337
|
}
|
|
347
338
|
}
|
|
348
339
|
} catch (e) {
|
|
349
|
-
++atom.
|
|
340
|
+
++atom.g;
|
|
350
341
|
if (!e) {
|
|
351
342
|
atom.b = false;
|
|
352
343
|
} else {
|
|
@@ -371,7 +362,7 @@ const execute = (atom) => {
|
|
|
371
362
|
};
|
|
372
363
|
let disabling = false;
|
|
373
364
|
const disableAtom = (atom) => {
|
|
374
|
-
if (!atom.p && !atom.s && !atom.d?.size && !atom.
|
|
365
|
+
if (!atom.p && !atom.s && !atom.d?.size && !atom.h?.size && !atom.i?.size) {
|
|
375
366
|
if (!disabling) {
|
|
376
367
|
setTimeout(() => {
|
|
377
368
|
disabling = true;
|
|
@@ -381,7 +372,7 @@ const disableAtom = (atom) => {
|
|
|
381
372
|
return;
|
|
382
373
|
}
|
|
383
374
|
atom.state.promise = inactive;
|
|
384
|
-
atom.
|
|
375
|
+
atom.f = atom.o = atom.state.error = atom.state.value = void 0;
|
|
385
376
|
atom.b = atom.k = atom.c = false;
|
|
386
377
|
if (atom.a) {
|
|
387
378
|
atom.a.abort();
|
package/dist/react.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { $ } from '.';
|
|
2
|
-
import type { Atom, AtomGetter, AtomOptions, AtomScope, DerivedAtom, PrimitiveAtom } from '.';
|
|
2
|
+
import type { Atom, AtomGetter, AtomOptions, AtomScope, AtomValuePair, DerivedAtom, PrimitiveAtom } from '.';
|
|
3
3
|
export declare const ScopeContext: import("react").Context<AtomScope>;
|
|
4
|
-
export declare const ScopeProvider: ({ children }: {
|
|
4
|
+
export declare const ScopeProvider: ({ value, children }: {
|
|
5
|
+
value: AtomValuePair<unknown>[];
|
|
5
6
|
children: React.ReactNode;
|
|
6
7
|
}) => import("react/jsx-runtime").JSX.Element;
|
|
7
8
|
export declare const useAtomValue: <Value>(atom: Atom<Value>) => Value;
|
package/dist/react.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { createContext, use, useContext, useState, useSyncExternalStore } from "react";
|
|
3
3
|
import { $, createScope } from ".";
|
|
4
|
-
const ScopeContext = createContext(
|
|
5
|
-
|
|
6
|
-
);
|
|
7
|
-
const ScopeProvider = ({ children }) => {
|
|
4
|
+
const ScopeContext = createContext((x) => x);
|
|
5
|
+
const ScopeProvider = ({ value, children }) => {
|
|
8
6
|
const parentScope = useContext(ScopeContext);
|
|
9
|
-
const scope = useState(() => createScope(parentScope))[0];
|
|
7
|
+
const scope = useState(() => createScope(parentScope, value))[0];
|
|
10
8
|
return /* @__PURE__ */ jsx(ScopeContext.Provider, { value: scope, children });
|
|
11
9
|
};
|
|
12
10
|
const useAtomValue = (atom) => (atom = useContext(ScopeContext)(atom), useSyncExternalStore(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bansa",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,12 +17,12 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@biomejs/biome": "^2.
|
|
21
|
-
"@types/react": "^19.
|
|
22
|
-
"@types/react-dom": "^19.
|
|
23
|
-
"esbuild": "^0.
|
|
24
|
-
"typescript": "^5.9.
|
|
25
|
-
"vitest": "^
|
|
20
|
+
"@biomejs/biome": "^2.3.8",
|
|
21
|
+
"@types/react": "^19.2.7",
|
|
22
|
+
"@types/react-dom": "^19.2.3",
|
|
23
|
+
"esbuild": "^0.27.0",
|
|
24
|
+
"typescript": "^5.9.3",
|
|
25
|
+
"vitest": "^4.0.14"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"@types/react": ">=17.0.0",
|
package/tests/bansa.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { $, type ThenableSignal } from '../src/index';
|
|
2
|
+
import { $, createScope, type ThenableSignal } from '../src/index';
|
|
3
3
|
|
|
4
4
|
const flushMicrotasks = () =>
|
|
5
5
|
new Promise((resolve) => {
|
|
@@ -476,7 +476,7 @@ describe('Atom Library - Advanced Tests', () => {
|
|
|
476
476
|
await flushMicrotasks(); // Additional wait for promise resolution
|
|
477
477
|
|
|
478
478
|
expect(errorAtom.state.error).toBe(error);
|
|
479
|
-
expect(() => errorAtom.get()).
|
|
479
|
+
expect(() => errorAtom.get()).toThrowError(error);
|
|
480
480
|
});
|
|
481
481
|
|
|
482
482
|
it('multiple subscribers receive updates', async () => {
|
|
@@ -951,6 +951,120 @@ describe('Bansa Documentation Examples as Tests', () => {
|
|
|
951
951
|
});
|
|
952
952
|
});
|
|
953
953
|
|
|
954
|
+
// --- 스코프 테스트 ---
|
|
955
|
+
describe('Scope', () => {
|
|
956
|
+
it('scope with initial values', async () => {
|
|
957
|
+
const $x = $(0);
|
|
958
|
+
const $y = $(1);
|
|
959
|
+
const $x2 = $(100);
|
|
960
|
+
const scope = createScope(null, [
|
|
961
|
+
[$x, $x2],
|
|
962
|
+
[$y, 101],
|
|
963
|
+
]);
|
|
964
|
+
|
|
965
|
+
const $y2 = scope($y);
|
|
966
|
+
expect(scope($x)).toBe($x2);
|
|
967
|
+
expect($y2).not.toBe($y);
|
|
968
|
+
expect($y2).toBe(scope($y));
|
|
969
|
+
|
|
970
|
+
expect($x.get()).toBe(0);
|
|
971
|
+
expect($y.get()).toBe(1);
|
|
972
|
+
expect($x2.get()).toBe(100);
|
|
973
|
+
expect($y2.get()).toBe(101);
|
|
974
|
+
});
|
|
975
|
+
|
|
976
|
+
it('scope with updates (1)', async () => {
|
|
977
|
+
const $x = $(0);
|
|
978
|
+
const $y = $((get) => get($x) + 1);
|
|
979
|
+
const scope = createScope();
|
|
980
|
+
const $x2 = scope($x);
|
|
981
|
+
const $y2 = scope($y);
|
|
982
|
+
|
|
983
|
+
expect($x.get()).toBe(0);
|
|
984
|
+
expect($y.get()).toBe(1);
|
|
985
|
+
expect($x2.get()).toBe(0);
|
|
986
|
+
expect($y2.get()).toBe(1);
|
|
987
|
+
|
|
988
|
+
$x.set(10);
|
|
989
|
+
await flushMicrotasks();
|
|
990
|
+
|
|
991
|
+
expect($x.get()).toBe(10);
|
|
992
|
+
expect($y.get()).toBe(11);
|
|
993
|
+
expect($x2.get()).toBe(0);
|
|
994
|
+
expect($y2.get()).toBe(1);
|
|
995
|
+
|
|
996
|
+
$x2.set(100);
|
|
997
|
+
await flushMicrotasks();
|
|
998
|
+
|
|
999
|
+
expect($x.get()).toBe(10);
|
|
1000
|
+
expect($y.get()).toBe(11);
|
|
1001
|
+
expect($x2.get()).toBe(100);
|
|
1002
|
+
expect($y2.get()).toBe(101);
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
it('scope with updates (2)', async () => {
|
|
1006
|
+
const $x = $(0);
|
|
1007
|
+
const $y = $((get) => get($x) + 1);
|
|
1008
|
+
const scope = createScope(null, [
|
|
1009
|
+
[$x, 100],
|
|
1010
|
+
]);
|
|
1011
|
+
const $x2 = scope($x);
|
|
1012
|
+
const $y2 = scope($y);
|
|
1013
|
+
|
|
1014
|
+
expect($x.get()).toBe(0);
|
|
1015
|
+
expect($y.get()).toBe(1);
|
|
1016
|
+
expect($x2.get()).toBe(100);
|
|
1017
|
+
expect($y2.get()).toBe(101);
|
|
1018
|
+
|
|
1019
|
+
$x.set(10);
|
|
1020
|
+
await flushMicrotasks();
|
|
1021
|
+
|
|
1022
|
+
expect($x.get()).toBe(10);
|
|
1023
|
+
expect($y.get()).toBe(11);
|
|
1024
|
+
expect($x2.get()).toBe(100);
|
|
1025
|
+
expect($y2.get()).toBe(101);
|
|
1026
|
+
|
|
1027
|
+
$x2.set(1000);
|
|
1028
|
+
await flushMicrotasks();
|
|
1029
|
+
|
|
1030
|
+
expect($x.get()).toBe(10);
|
|
1031
|
+
expect($y.get()).toBe(11);
|
|
1032
|
+
expect($x2.get()).toBe(1000);
|
|
1033
|
+
expect($y2.get()).toBe(1001);
|
|
1034
|
+
});
|
|
1035
|
+
|
|
1036
|
+
it('scope with updates (3)', async () => {
|
|
1037
|
+
const $x = $(0);
|
|
1038
|
+
const $y = $((get) => get($x) + 1);
|
|
1039
|
+
const $x2 = $(100);
|
|
1040
|
+
const scope = createScope(null, [
|
|
1041
|
+
[$x, $x2],
|
|
1042
|
+
]);
|
|
1043
|
+
const $y2 = scope($y);
|
|
1044
|
+
|
|
1045
|
+
expect($x.get()).toBe(0);
|
|
1046
|
+
expect($y.get()).toBe(1);
|
|
1047
|
+
expect($x2.get()).toBe(100);
|
|
1048
|
+
expect($y2.get()).toBe(101);
|
|
1049
|
+
|
|
1050
|
+
$x.set(10);
|
|
1051
|
+
await flushMicrotasks();
|
|
1052
|
+
|
|
1053
|
+
expect($x.get()).toBe(10);
|
|
1054
|
+
expect($y.get()).toBe(11);
|
|
1055
|
+
expect($x2.get()).toBe(100);
|
|
1056
|
+
expect($y2.get()).toBe(101);
|
|
1057
|
+
|
|
1058
|
+
$x2.set(1000);
|
|
1059
|
+
await flushMicrotasks();
|
|
1060
|
+
|
|
1061
|
+
expect($x.get()).toBe(10);
|
|
1062
|
+
expect($y.get()).toBe(11);
|
|
1063
|
+
expect($x2.get()).toBe(1000);
|
|
1064
|
+
expect($y2.get()).toBe(1001);
|
|
1065
|
+
});
|
|
1066
|
+
});
|
|
1067
|
+
|
|
954
1068
|
// --- "스크롤 방향 감지" 예제 테스트 ---
|
|
955
1069
|
/*
|
|
956
1070
|
describe('Scroll Direction Detection Example', () => {
|