Skip to content

Commit

Permalink
feat: flow removed
Browse files Browse the repository at this point in the history
  • Loading branch information
betula committed Jun 7, 2022
1 parent 8e20a2c commit 9fc537b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 94 deletions.
85 changes: 4 additions & 81 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ let level_current;
let batch_nodes;

const reactions_loop_limit = 1000000;
const flow_stop = Symbol();

// node: sel or expr node
// type: 0 - rels, 1 - deps
Expand Down Expand Up @@ -98,17 +97,16 @@ const write = (box_node, is_array) => {

const stack_node_h = stack_nodes.get(node);
stack_node_h[0] = 1;
const len = node.length;

if (stack_node_h[2]) nodes.delete(node);
else
do {
stack_node_h[1] = 0;
let expr, sel;

if (len === 3) expr = 1;
if (node.length === 3) expr = 1;
else {
if (node[0].size || len === 4) sel = 1 /* sel or flow */;
if (node[0].size) sel = 1;
else node[3] = 0;
}

Expand All @@ -117,7 +115,7 @@ const write = (box_node, is_array) => {

if (expr) node[0]();
if (sel) {
if (node[len - 1]()) {
if (node[4]()) {
node_expand(node);
free(node, 0);
}
Expand Down Expand Up @@ -282,79 +280,4 @@ const expr = (body, sync) => {
];
};

const flow = (fn, empty_value, is_equals = Object.is) => {
let value = empty_value;

const resolve = (next_value) => {
if (!is_equals(value, next_value)) {
value = next_value;
write(flow_node);
}
};
const body_run = () => fn(resolve, value);

const digest_run = () => {
const stack_context_node = context_node;
const stack_untrack = context_untrack;
context_untrack = 0;

context_node = flow_node;
context_node[2] = 0; // clear level

let result;
let h = stack_nodes.get(flow_node);
let is_entry;
// [<now_in_execution>, <marked_for_recalc>, <is_stopped>]
if (!h) {
stack_nodes.set(flow_node, (is_entry = h = [1, 0, 0]));
} else if (h[2]) h[2] = 0;

try {
if (is_entry) {
let limit = reactions_loop_limit;
do {
flow_node[1].size && free(flow_node, 1);
h[1] = 0;
result = body_run();
if (!--limit) throw_infinity_reactions();
} while (h[1] && !h[2]);

stack_nodes.delete(flow_node);
h[2] && (free(flow_node, 1), free(flow_node, 0));
} else {
result = body_run();
}
} finally {
context_node = stack_context_node;
context_untrack = stack_untrack;
}
return result;
};
const run = () => {
let next = digest_run();
return next === flow_stop || is_equals(value, next)
? false
: ((value = next), true);
};

// rels, deps, level, fn
const flow_node = [new Set(), new Set(), 0, run];

return [
run,
() => {
read(flow_node);
calculate_level(flow_node);
return value;
},
() => {
free(flow_node, 1);
free(flow_node, 0);
value = empty_value;
stack_nodes.has(flow_node) && (stack_nodes.get(flow_node)[2] = 1);
},
];
};
flow.stop = flow_stop;

module.exports = { box, sel, expr, flow, batch, untrack };
module.exports = { box, sel, expr, batch, untrack };
14 changes: 1 addition & 13 deletions tests/lib.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { box, expr, sel, flow, batch, untrack } = require("..");
const { box, expr, sel, batch, untrack } = require("..");

module.exports.batch = batch;
module.exports.untrack = untrack;
Expand Down Expand Up @@ -38,16 +38,4 @@ module.exports.comp = (body, comparer) => {
return obj;
};

module.exports.flow = flow;
module.exports.flowstop = flow.stop;
module.exports.compflow = (body, empty, comparer) => {
const f = flow(body, empty, comparer);
f[0]();
const obj = { stop: f[2] };
Object.defineProperty(obj, "val", {
get: f[1],
});
return obj;
};

module.exports.delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

0 comments on commit 9fc537b

Please sign in to comment.