Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyraspopov committed Oct 17, 2023
1 parent de72c9f commit 99c6815
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions inertial.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function ObservableScope(schedule = (cb) => cb()) {
return (value) => {
if (typeof value === "undefined") {
// reading
if (tracking != null) vertice(vs, key, tracking);
if (tracking != null) union(vs, key, tracking);
return current;
} else {
// writing
Expand Down Expand Up @@ -59,7 +59,7 @@ export function ObservableScope(schedule = (cb) => cb()) {
return (value) => {
if (typeof value === "undefined") {
// reading
if (tracking != null) vertice(vs, inputKey, tracking);
if (tracking != null) union(vs, inputKey, tracking);
return current;
} else {
// writing
Expand Down Expand Up @@ -89,7 +89,7 @@ export function ObservableScope(schedule = (cb) => cb()) {
if (action === "dispose") clear();
});
return () => {
if (tracking != null) vertice(vs, key, tracking);
if (tracking != null) union(vs, key, tracking);
return current;
};
}
Expand All @@ -116,10 +116,15 @@ export function ObservableScope(schedule = (cb) => cb()) {

function digest() {
while (queue.size > 0) {
let temp = (wip = queue);
wip = queue;
queue = new Set();
for (let cursor = 0, cons = new Map(cbs), key, fn; cursor < vs.length; cursor += 2) {
if (temp.has(vs[cursor])) {
for (
let cursor = 0, cons = new Map(cbs), q = wip, key, fn, p;
cursor < vs.length;
cursor += 2
) {
if (vs[cursor] === p || q.has(vs[cursor])) {
p = vs[cursor];
key = vs[cursor + 1];
fn = cons.get(key);
if (fn != null) {
Expand All @@ -135,17 +140,15 @@ export function ObservableScope(schedule = (cb) => cb()) {
return { signal, watch, derive, observe, peek, batch, dispose };
}

function vertice(vs, pi, ci) {
vs.splice(bisect2(vs, pi), 0, pi, ci);
}

function bisect2(values, x, lo = 0, hi = values.length) {
let mid;
function union(vs, pk, ck) {
let mid,
lo = 0,
hi = vs.length;
while (lo < hi) {
mid = (lo + hi) >>> 1;
mid -= mid % 2;
if (values[mid] <= x) lo = mid + 2;
if (vs[mid] <= pk) lo = mid + 2;
else hi = mid;
}
return lo;
vs.splice(lo, 0, pk, ck);
}

0 comments on commit 99c6815

Please sign in to comment.