mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-27 16:19:59 -07:00
website: up signals + added compare folder to all groups
This commit is contained in:
@@ -1,18 +1,13 @@
|
||||
// src/error.ts
|
||||
var NotReadyError = class extends Error {
|
||||
};
|
||||
var NotReadyError = class extends Error {};
|
||||
var NoOwnerError = class extends Error {
|
||||
constructor() {
|
||||
super(
|
||||
""
|
||||
);
|
||||
super("");
|
||||
}
|
||||
};
|
||||
var ContextNotFoundError = class extends Error {
|
||||
constructor() {
|
||||
super(
|
||||
""
|
||||
);
|
||||
super("");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -50,14 +45,12 @@ var Owner = class {
|
||||
h = defaultContext;
|
||||
f = null;
|
||||
constructor(signal = false) {
|
||||
if (currentOwner && !signal)
|
||||
currentOwner.append(this);
|
||||
if (currentOwner && !signal) currentOwner.append(this);
|
||||
}
|
||||
append(child) {
|
||||
child.k = this;
|
||||
child.j = this;
|
||||
if (this.g)
|
||||
this.g.j = child;
|
||||
if (this.g) this.g.j = child;
|
||||
child.g = this.g;
|
||||
this.g = child;
|
||||
if (child.h !== this.h) {
|
||||
@@ -68,9 +61,10 @@ var Owner = class {
|
||||
}
|
||||
}
|
||||
dispose(self = true) {
|
||||
if (this.a === STATE_DISPOSED)
|
||||
return;
|
||||
let head = self ? this.j || this.k : this, current = this.g, next = null;
|
||||
if (this.a === STATE_DISPOSED) return;
|
||||
let head = self ? this.j || this.k : this,
|
||||
current = this.g,
|
||||
next = null;
|
||||
while (current && current.k === this) {
|
||||
current.dispose(true);
|
||||
current.n();
|
||||
@@ -78,16 +72,12 @@ var Owner = class {
|
||||
current.g = null;
|
||||
current = next;
|
||||
}
|
||||
if (self)
|
||||
this.n();
|
||||
if (current)
|
||||
current.j = !self ? this : this.j;
|
||||
if (head)
|
||||
head.g = current;
|
||||
if (self) this.n();
|
||||
if (current) current.j = !self ? this : this.j;
|
||||
if (head) head.g = current;
|
||||
}
|
||||
n() {
|
||||
if (this.j)
|
||||
this.j.g = null;
|
||||
if (this.j) this.j.g = null;
|
||||
this.k = null;
|
||||
this.j = null;
|
||||
this.h = defaultContext;
|
||||
@@ -96,8 +86,7 @@ var Owner = class {
|
||||
this.emptyDisposal();
|
||||
}
|
||||
emptyDisposal() {
|
||||
if (!this.e)
|
||||
return;
|
||||
if (!this.e) return;
|
||||
if (Array.isArray(this.e)) {
|
||||
for (let i = 0; i < this.e.length; i++) {
|
||||
const callable = this.e[i];
|
||||
@@ -109,9 +98,9 @@ var Owner = class {
|
||||
this.e = null;
|
||||
}
|
||||
handleError(error) {
|
||||
if (!this.f)
|
||||
throw error;
|
||||
let i = 0, len = this.f.length;
|
||||
if (!this.f) throw error;
|
||||
let i = 0,
|
||||
len = this.f.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
try {
|
||||
this.f[i](error);
|
||||
@@ -120,8 +109,7 @@ var Owner = class {
|
||||
error = e;
|
||||
}
|
||||
}
|
||||
if (i === len)
|
||||
throw error;
|
||||
if (i === len) throw error;
|
||||
}
|
||||
};
|
||||
function createContext(defaultValue, description) {
|
||||
@@ -131,7 +119,9 @@ function getContext(context, owner = currentOwner) {
|
||||
if (!owner) {
|
||||
throw new NoOwnerError();
|
||||
}
|
||||
const value = hasContext(context, owner) ? owner.h[context.id] : context.defaultValue;
|
||||
const value = hasContext(context, owner)
|
||||
? owner.h[context.id]
|
||||
: context.defaultValue;
|
||||
if (isUndefined(value)) {
|
||||
throw new ContextNotFoundError();
|
||||
}
|
||||
@@ -143,15 +133,14 @@ function setContext(context, value, owner = currentOwner) {
|
||||
}
|
||||
owner.h = {
|
||||
...owner.h,
|
||||
[context.id]: isUndefined(value) ? context.defaultValue : value
|
||||
[context.id]: isUndefined(value) ? context.defaultValue : value,
|
||||
};
|
||||
}
|
||||
function hasContext(context, owner = currentOwner) {
|
||||
return !isUndefined(owner?.h[context.id]);
|
||||
}
|
||||
function onCleanup(disposable) {
|
||||
if (!currentOwner)
|
||||
return;
|
||||
if (!currentOwner) return;
|
||||
const node = currentOwner;
|
||||
if (!node.e) {
|
||||
node.e = disposable;
|
||||
@@ -176,12 +165,10 @@ var Computations = [];
|
||||
var RenderEffects = [];
|
||||
var Effects = [];
|
||||
function flushSync() {
|
||||
if (!runningScheduled)
|
||||
runScheduled();
|
||||
if (!runningScheduled) runScheduled();
|
||||
}
|
||||
function flushQueue() {
|
||||
if (scheduled)
|
||||
return;
|
||||
if (scheduled) return;
|
||||
scheduled = true;
|
||||
queueMicrotask(runScheduled);
|
||||
}
|
||||
@@ -193,8 +180,7 @@ function runTop(node) {
|
||||
}
|
||||
}
|
||||
for (let i = ancestors.length - 1; i >= 0; i--) {
|
||||
if (ancestors[i].a !== STATE_DISPOSED)
|
||||
ancestors[i].l();
|
||||
if (ancestors[i].a !== STATE_DISPOSED) ancestors[i].l();
|
||||
}
|
||||
}
|
||||
function runScheduled() {
|
||||
@@ -222,8 +208,7 @@ function runScheduled() {
|
||||
}
|
||||
function runPureQueue(queue) {
|
||||
for (let i = 0; i < queue.length; i++) {
|
||||
if (queue[i].a !== STATE_CLEAN)
|
||||
runTop(queue[i]);
|
||||
if (queue[i].a !== STATE_CLEAN) runTop(queue[i]);
|
||||
}
|
||||
}
|
||||
function runEffectQueue(queue) {
|
||||
@@ -274,16 +259,12 @@ var Computation2 = class extends Owner {
|
||||
this.s = compute2;
|
||||
this.a = compute2 ? STATE_DIRTY : STATE_CLEAN;
|
||||
this.d = initialValue;
|
||||
if (options?.equals !== void 0)
|
||||
this.t = options.equals;
|
||||
if (options?.unobserved)
|
||||
this.x = options?.unobserved;
|
||||
if (options?.equals !== void 0) this.t = options.equals;
|
||||
if (options?.unobserved) this.x = options?.unobserved;
|
||||
}
|
||||
y() {
|
||||
if (this.s)
|
||||
this.l();
|
||||
if (!this.b || this.b.length)
|
||||
track(this);
|
||||
if (this.s) this.l();
|
||||
if (!this.b || this.b.length) track(this);
|
||||
newFlags |= this.i & ~currentMask;
|
||||
if (this.i & ERROR_BIT) {
|
||||
throw this.d;
|
||||
@@ -336,11 +317,14 @@ var Computation2 = class extends Owner {
|
||||
}
|
||||
/** Update the computation with a new value. */
|
||||
write(value, flags = 0, raw = false) {
|
||||
const newValue = !raw && typeof value === "function" ? value(this.d) : value;
|
||||
const valueChanged = newValue !== UNCHANGED && (!!(flags & ERROR_BIT) || this.t === false || !this.t(this.d, newValue));
|
||||
if (valueChanged)
|
||||
this.d = newValue;
|
||||
const changedFlagsMask = this.i ^ flags, changedFlags = changedFlagsMask & flags;
|
||||
const newValue =
|
||||
!raw && typeof value === "function" ? value(this.d) : value;
|
||||
const valueChanged =
|
||||
newValue !== UNCHANGED &&
|
||||
(!!(flags & ERROR_BIT) || this.t === false || !this.t(this.d, newValue));
|
||||
if (valueChanged) this.d = newValue;
|
||||
const changedFlagsMask = this.i ^ flags,
|
||||
changedFlags = changedFlagsMask & flags;
|
||||
this.i = flags;
|
||||
this.w = clock + 1;
|
||||
if (this.c) {
|
||||
@@ -358,8 +342,7 @@ var Computation2 = class extends Owner {
|
||||
* Set the current node's state, and recursively mark all of this node's observers as STATE_CHECK
|
||||
*/
|
||||
m(state) {
|
||||
if (this.a >= state)
|
||||
return;
|
||||
if (this.a >= state) return;
|
||||
this.a = state;
|
||||
if (this.c) {
|
||||
for (let i = 0; i < this.c.length; i++) {
|
||||
@@ -374,17 +357,16 @@ var Computation2 = class extends Owner {
|
||||
* @param newFlags The source's new flags, masked to just the changed ones.
|
||||
*/
|
||||
z(mask, newFlags2) {
|
||||
if (this.a >= STATE_DIRTY)
|
||||
return;
|
||||
if (this.a >= STATE_DIRTY) return;
|
||||
if (mask & this.p) {
|
||||
this.m(STATE_DIRTY);
|
||||
return;
|
||||
}
|
||||
if (this.a >= STATE_CHECK)
|
||||
return;
|
||||
if (this.a >= STATE_CHECK) return;
|
||||
const prevFlags = this.i & mask;
|
||||
const deltaFlags = prevFlags ^ newFlags2;
|
||||
if (newFlags2 === prevFlags) ; else if (deltaFlags & prevFlags & mask) {
|
||||
if (newFlags2 === prevFlags);
|
||||
else if (deltaFlags & prevFlags & mask) {
|
||||
this.m(STATE_CHECK);
|
||||
} else {
|
||||
this.i ^= deltaFlags;
|
||||
@@ -433,10 +415,8 @@ var Computation2 = class extends Owner {
|
||||
* Remove ourselves from the owner graph and the computation graph
|
||||
*/
|
||||
n() {
|
||||
if (this.a === STATE_DISPOSED)
|
||||
return;
|
||||
if (this.b)
|
||||
removeSourceObservers(this, 0);
|
||||
if (this.a === STATE_DISPOSED) return;
|
||||
if (this.b) removeSourceObservers(this, 0);
|
||||
super.n();
|
||||
}
|
||||
};
|
||||
@@ -450,7 +430,7 @@ function loadingState(node) {
|
||||
node.l();
|
||||
return !!(node.i & LOADING_BIT);
|
||||
},
|
||||
options
|
||||
options,
|
||||
);
|
||||
computation.p = ERROR_BIT | LOADING_BIT;
|
||||
setOwner(prevOwner);
|
||||
@@ -466,7 +446,7 @@ function errorState(node) {
|
||||
node.l();
|
||||
return !!(node.i & ERROR_BIT);
|
||||
},
|
||||
options
|
||||
options,
|
||||
);
|
||||
computation.p = ERROR_BIT;
|
||||
setOwner(prevOwner);
|
||||
@@ -474,10 +454,13 @@ function errorState(node) {
|
||||
}
|
||||
function track(computation) {
|
||||
if (currentObserver) {
|
||||
if (!newSources && currentObserver.b && currentObserver.b[newSourcesIndex] === computation) {
|
||||
if (
|
||||
!newSources &&
|
||||
currentObserver.b &&
|
||||
currentObserver.b[newSourcesIndex] === computation
|
||||
) {
|
||||
newSourcesIndex++;
|
||||
} else if (!newSources)
|
||||
newSources = [computation];
|
||||
} else if (!newSources) newSources = [computation];
|
||||
else if (computation !== newSources[newSources.length - 1]) {
|
||||
newSources.push(computation);
|
||||
}
|
||||
@@ -487,7 +470,9 @@ function track(computation) {
|
||||
}
|
||||
}
|
||||
function update(node) {
|
||||
const prevSources = newSources, prevSourcesIndex = newSourcesIndex, prevFlags = newFlags;
|
||||
const prevSources = newSources,
|
||||
prevSourcesIndex = newSourcesIndex,
|
||||
prevFlags = newFlags;
|
||||
newSources = null;
|
||||
newSourcesIndex = 0;
|
||||
newFlags = 0;
|
||||
@@ -504,8 +489,7 @@ function update(node) {
|
||||
}
|
||||
} finally {
|
||||
if (newSources) {
|
||||
if (node.b)
|
||||
removeSourceObservers(node, newSourcesIndex);
|
||||
if (node.b) removeSourceObservers(node, newSourcesIndex);
|
||||
if (node.b && newSourcesIndex > 0) {
|
||||
node.b.length = newSourcesIndex + newSources.length;
|
||||
for (let i = 0; i < newSources.length; i++) {
|
||||
@@ -517,10 +501,8 @@ function update(node) {
|
||||
let source;
|
||||
for (let i = newSourcesIndex; i < node.b.length; i++) {
|
||||
source = node.b[i];
|
||||
if (!source.c)
|
||||
source.c = [node];
|
||||
else
|
||||
source.c.push(node);
|
||||
if (!source.c) source.c = [node];
|
||||
else source.c.push(node);
|
||||
}
|
||||
} else if (node.b && newSourcesIndex < node.b.length) {
|
||||
removeSourceObservers(node, newSourcesIndex);
|
||||
@@ -541,8 +523,7 @@ function removeSourceObservers(node, index) {
|
||||
swap = source.c.indexOf(node);
|
||||
source.c[swap] = source.c[source.c.length - 1];
|
||||
source.c.pop();
|
||||
if (!source.c.length)
|
||||
source.x?.();
|
||||
if (!source.c.length) source.x?.();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -550,8 +531,7 @@ function isEqual(a, b) {
|
||||
return a === b;
|
||||
}
|
||||
function untrack(fn) {
|
||||
if (currentObserver === null)
|
||||
return fn();
|
||||
if (currentObserver === null) return fn();
|
||||
return compute(getOwner(), fn, null);
|
||||
}
|
||||
function hasUpdated(fn) {
|
||||
@@ -565,7 +545,9 @@ function hasUpdated(fn) {
|
||||
}
|
||||
}
|
||||
function compute(owner, compute2, observer) {
|
||||
const prevOwner = setOwner(owner), prevObserver = currentObserver, prevMask = currentMask;
|
||||
const prevOwner = setOwner(owner),
|
||||
prevObserver = currentObserver,
|
||||
prevMask = currentMask;
|
||||
currentObserver = observer;
|
||||
currentMask = observer?.p ?? DEFAULT_FLAGS;
|
||||
try {
|
||||
@@ -583,8 +565,7 @@ var EagerComputation = class extends Computation2 {
|
||||
Computations.push(this);
|
||||
}
|
||||
m(state) {
|
||||
if (this.a >= state)
|
||||
return;
|
||||
if (this.a >= state) return;
|
||||
if (this.a === STATE_CLEAN) {
|
||||
Computations.push(this);
|
||||
flushQueue();
|
||||
@@ -604,8 +585,7 @@ var BaseEffect = class extends Computation2 {
|
||||
this.o = initialValue;
|
||||
}
|
||||
write(value) {
|
||||
if (value === UNCHANGED)
|
||||
return this.d;
|
||||
if (value === UNCHANGED) return this.d;
|
||||
this.d = value;
|
||||
this.q = true;
|
||||
return value;
|
||||
@@ -626,8 +606,7 @@ var Effect = class extends BaseEffect {
|
||||
flushQueue();
|
||||
}
|
||||
m(state) {
|
||||
if (this.a >= state)
|
||||
return;
|
||||
if (this.a >= state) return;
|
||||
if (this.a === STATE_CLEAN) {
|
||||
Effects.push(this);
|
||||
flushQueue();
|
||||
@@ -642,8 +621,7 @@ var RenderEffect = class extends BaseEffect {
|
||||
RenderEffects.push(this);
|
||||
}
|
||||
m(state) {
|
||||
if (this.a >= state)
|
||||
return;
|
||||
if (this.a >= state) return;
|
||||
if (this.a === STATE_CLEAN) {
|
||||
RenderEffects.push(this);
|
||||
flushQueue();
|
||||
@@ -659,7 +637,7 @@ function createSignal(first, second, third) {
|
||||
const node2 = new Computation2(
|
||||
first(p ? untrack(p[0]) : second),
|
||||
null,
|
||||
third
|
||||
third,
|
||||
);
|
||||
return [node2.read.bind(node2), node2.write.bind(node2)];
|
||||
});
|
||||
@@ -677,7 +655,7 @@ function createAsync(fn, initial, options) {
|
||||
return {
|
||||
wait() {
|
||||
return source;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
const signal = new Computation2(initial, null, options);
|
||||
@@ -689,16 +667,15 @@ function createAsync(fn, initial, options) {
|
||||
},
|
||||
(error) => {
|
||||
signal.write(error, ERROR_BIT);
|
||||
}
|
||||
},
|
||||
);
|
||||
} else {
|
||||
let abort = false;
|
||||
onCleanup(() => abort = true);
|
||||
onCleanup(() => (abort = true));
|
||||
(async () => {
|
||||
try {
|
||||
for await (let value of source) {
|
||||
if (abort)
|
||||
return;
|
||||
if (abort) return;
|
||||
signal.write(value, 0);
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -711,43 +688,28 @@ function createAsync(fn, initial, options) {
|
||||
return () => lhs.wait().wait();
|
||||
}
|
||||
function createMemo(compute2, initialValue, options) {
|
||||
let node = new Computation2(
|
||||
initialValue,
|
||||
compute2,
|
||||
options
|
||||
);
|
||||
let node = new Computation2(initialValue, compute2, options);
|
||||
let value;
|
||||
return () => {
|
||||
if (node) {
|
||||
value = node.wait();
|
||||
if (!node.b?.length)
|
||||
node = void 0;
|
||||
if (!node.b?.length) node = void 0;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
}
|
||||
function createEffect(compute2, effect, initialValue, options) {
|
||||
void new Effect(
|
||||
initialValue,
|
||||
compute2,
|
||||
effect,
|
||||
void 0
|
||||
);
|
||||
void new Effect(initialValue, compute2, effect, void 0);
|
||||
}
|
||||
function createRenderEffect(compute2, effect, initialValue, options) {
|
||||
void new RenderEffect(
|
||||
initialValue,
|
||||
compute2,
|
||||
effect,
|
||||
void 0
|
||||
);
|
||||
void new RenderEffect(initialValue, compute2, effect, void 0);
|
||||
}
|
||||
function createRoot(init) {
|
||||
const owner = new Owner();
|
||||
return compute(
|
||||
owner,
|
||||
!init.length ? init : () => init(() => owner.dispose()),
|
||||
null
|
||||
null,
|
||||
);
|
||||
}
|
||||
function runWithOwner(owner, run) {
|
||||
@@ -768,4 +730,33 @@ function catchError(fn, handler) {
|
||||
}
|
||||
}
|
||||
|
||||
export { Computation2 as Computation, ContextNotFoundError, Effect, NoOwnerError, NotReadyError, Owner, RenderEffect, catchError, compute, createAsync, createContext, createEffect, createMemo, createRenderEffect, createRoot, createSignal, flushSync, getContext, getObserver, getOwner, hasContext, hasUpdated, isEqual, onCleanup, runWithOwner, setContext, setOwner, untrack };
|
||||
export {
|
||||
Computation2 as Computation,
|
||||
ContextNotFoundError,
|
||||
Effect,
|
||||
NoOwnerError,
|
||||
NotReadyError,
|
||||
Owner,
|
||||
RenderEffect,
|
||||
catchError,
|
||||
compute,
|
||||
createAsync,
|
||||
createContext,
|
||||
createEffect,
|
||||
createMemo,
|
||||
createRenderEffect,
|
||||
createRoot,
|
||||
createSignal,
|
||||
flushSync,
|
||||
getContext,
|
||||
getObserver,
|
||||
getOwner,
|
||||
hasContext,
|
||||
hasUpdated,
|
||||
isEqual,
|
||||
onCleanup,
|
||||
runWithOwner,
|
||||
setContext,
|
||||
setOwner,
|
||||
untrack,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user