nobounds v0.0.54
nobounds
Rewrite your module bindings into reactive orchestration.
Installation
$ npm i nobounds --save
How it works?
The nobounds
library provides a compiler
and the reactor
factory, e.g.
import { reactor, compiler } from 'nobounds';
async function main() {
const data = {
b: undefined,
c: undefined,
};
const result = await reactor(null, data, ctx => {
with (ctx) {
const a = 42;
$set(() => { b = 2; });
$set(() => { c = a * b; });
}
}, () => {
console.log('DONE');
});
console.log(result.data);
// { b: 2, c: 84 }
console.log(Object.keys(result.change));
// ['b', 'c']
}
main();
As it sounds, when you reassign b = 4
then the value for c
is recalculated.
The result
value will be the mutated values from the callback, the original data
is not touched.
Compiling stuff
Now lets see how compilation would help to simplify how this code is being authored, e.g.
// some symbols are re-exported
export util from 'util';
export const value = 42;
// bindings are wrapped
export let other = 2;
export let out = value / 2;
The generated output from the compiler would be:
async $$ => {with ($$) {
// some symbols are re-exported
const util = require("util"); $def(_, { util });
const value = 42; $def(_, { value });
// bindings are wrapped
await $set(async () => { other = 2; });
await $set(async () => { out = value / 2; });
}}
Make sure you're wrapping this code into something useful, e.g.
import vm from 'vm';
const data = { x: 42 };
const sample = compiler.transform('export const def = x / 2;');
const result = await vm.runInNewContext(`reactor(null, data, ${sample.code})`, { reactor, data });
console.log(result.data.def);
// 21
reactor(ctx, data, source[, callback])
ctx
— First argument will be passed toValue
instances when reading.data
— Second argument is the initialprops
whose keys will be computed.source
— Third argument is the reactor code to be executed.callback
— Last argument is a function that runs after actual code is executed.
Value instances
nobounds also exports a Value
class as a placeholder for computed variables.
import { Value } from 'nobounds';
const value = new Value(0);
value.set(42);
console.log(value.valueOf());
// 42
Those variables inside
reactor()
calls are bound aswell, e.g.value = 42
can be used instead.
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago