0.0.54 • Published 3 years ago

nobounds v0.0.54

Weekly downloads
505
License
-
Repository
-
Last release
3 years ago

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 to Value instances when reading.
  • data Second argument is the initial props 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.

0.0.52

3 years ago

0.0.53

3 years ago

0.0.54

3 years ago

0.0.51

3 years ago

0.0.50

3 years ago

0.0.44

3 years ago

0.0.45

3 years ago

0.0.46

3 years ago

0.0.47

3 years ago

0.0.48

3 years ago

0.0.49

3 years ago

0.0.43

3 years ago

0.0.42

3 years ago

0.0.41

3 years ago

0.0.40

3 years ago

0.0.39

3 years ago

0.0.38

3 years ago

0.0.37

3 years ago

0.0.35

3 years ago

0.0.36

3 years ago

0.0.33

3 years ago

0.0.34

3 years ago

0.0.32

3 years ago

0.0.30

3 years ago

0.0.31

3 years ago

0.0.26

3 years ago

0.0.27

3 years ago

0.0.28

3 years ago

0.0.29

3 years ago

0.0.25

3 years ago

0.0.23

3 years ago

0.0.24

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago