All Projects → betula → reactive-box

betula / reactive-box

Licence: MIT license
1 kB effective reactive core

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to reactive-box

EventEmitter
Simple EventEmitter with multiple listeners
Stars: ✭ 19 (+0%)
Mutual labels:  observer, observable
state inspector
State change & method call logger. A debugging tool for instance variables and method calls.
Stars: ✭ 24 (+26.32%)
Mutual labels:  observer, observable
ng-observe
Angular reactivity streamlined...
Stars: ✭ 65 (+242.11%)
Mutual labels:  observer, observable
mutation-observer
A library for idiomatic use of MutationObserver with Angular
Stars: ✭ 32 (+68.42%)
Mutual labels:  observer, observable
Ease
It's magic.
Stars: ✭ 1,213 (+6284.21%)
Mutual labels:  observer, observable
mobx-router5
Router5 integration with mobx
Stars: ✭ 22 (+15.79%)
Mutual labels:  observer, observable
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (+205.26%)
Mutual labels:  observer, observable
Packer Ubuntu 1404
DEPRECATED - Packer Example - Ubuntu 14.04 Vagrant Box using Ansible provisioner
Stars: ✭ 81 (+326.32%)
Mutual labels:  minimal, box
Dob
Light and fast 🚀 state management tool using proxy.
Stars: ✭ 713 (+3652.63%)
Mutual labels:  observer, observable
Observable
The easiest way to observe values in Swift.
Stars: ✭ 346 (+1721.05%)
Mutual labels:  observer, observable
React Reactive Form
Angular like reactive forms in React.
Stars: ✭ 259 (+1263.16%)
Mutual labels:  observer, observable
Lightweightobservable
📬 A lightweight implementation of an observable sequence that you can subscribe to.
Stars: ✭ 114 (+500%)
Mutual labels:  observer, observable
Oba
Observe any object's any change
Stars: ✭ 101 (+431.58%)
Mutual labels:  observer, observable
Receiver
Swift µframework implementing the Observer pattern 📡
Stars: ✭ 238 (+1152.63%)
Mutual labels:  observer, observable
reputation-bot
Reputation bot which collects reputation based on chat messages in discord
Stars: ✭ 23 (+21.05%)
Mutual labels:  reaction
oh-my-design-patterns
🎨 Record the articles and code I wrote while learning design patterns
Stars: ✭ 33 (+73.68%)
Mutual labels:  observer
reaction-cli
A command line tool for working with Reaction Commerce.
Stars: ✭ 33 (+73.68%)
Mutual labels:  reaction
reaction-light
Easy to use reaction role Discord bot written in Python.
Stars: ✭ 108 (+468.42%)
Mutual labels:  reaction
minimalist
Observable Property and Signal for building data-driven UI without Rx
Stars: ✭ 88 (+363.16%)
Mutual labels:  observer
pidi-os
A minimalistic operating system
Stars: ✭ 35 (+84.21%)
Mutual labels:  minimal

npm version bundle size code coverage typescript supported

Minimalistic, fast, and highly efficient reactivity.

Hi friends! Today I will tell you how I came to this.

Redux has so many different functions, Mobx has mutable objects by default, Angular so heavy, Vue so strange, and other them so young 😅

These funny thoughts served as fuel for writing the minimal reaction core. So that everyone can make their own syntax for managing the state of the application in less than 100 lines of code 👍

It only three functions:

  • box - is the container for an immutable value.
  • sel - is the cached selector (or computed value) who will mark for recalculating If some of read inside boxes or selectors changed.
  • expr - is the expression who detects all boxes and selectors read inside and reacted If some of them changed.
import { box, sel, expr } from "reactive-box";

const [get, set] = box(0);

const [next] = sel(() => get() + 1);

const [run, stop] = expr(
    () => `Counter: ${get()} (next value: ${next()})`,
    () => console.log(run())
);
console.log(run()); // console: "Counter 0 (next value: 1)"

set(get() + 1);     // console: "Counter 1 (next value: 2)"

Try It on RunKit!

It is a basis for full feature reactive mathematic! For example that possible syntax to transcript previous javascript code:

  a` = 0                // create reactive value
  next` = a` + 1        // create new reactive value, dependent on the previous one
  expr = { "Counter: ${a`} (next value: ${next`})" }  // create reactive expression

  // subscribe to expression dependencies were change and run It again
  expr: () => console.log(expr())

  // run the expression
  console.log(expr())                         // message to console "Counter: 0 (next value: 1)"

  a = a` + 1   // here will be fired log to console again with new "Counter: 1 (next value: 2)" message, because a` was changed.
  1. We create reactive a
  2. We create reactive operation a + 1
  3. We create reactive expression "Counter: ${a} (next value: ${next})"
  4. We subscribe to change of a and next reactive dependencies
  5. We run reactive expression
  6. We are increasing the value of reactive a for demonstration subscriber reaction

Atomic

These are three basic elements necessary for creating data flow any difficulty.

The first element is a reactive container for an immutable value. All reactions beginning from container change value reaction.

The second one is the middle element. It uses all reactive containers as a source of values and returns the result of the expression. It's a transformer set of reactive values to a single one. The selector can be used as a reactive container in other selectors and expressions. It subscribes to change in any of the dependencies. And will recalculate the value if some of the dependency changed, but will propagate changes only if the return value changed.

And the last one is a reaction subscriber. It provides the possibility to subscribe to change any set of reactive containers. It can be run again after the listener was called.

Deep inside

  • It runs calculations synchronously.

  • glitch free - your reactions will only be called when there is a consistent state for them to run on.

  • Possibility for modification everywhere: in expressions and selectors!

In the real world

Below we will talk about more high level abstraction, to the world of React and integration reactive-box into, for best possibilities together!

Basic usage examples:

It is minimal core for a big family of state managers' syntax. You can use the different syntax of your data flow on one big project, but the single core of your reactions provides the possibility for easy synchronization between them.

Mobx like syntax example (57 lines of reactive core):

import React from "react";
import { computed, immutable, observe, shared } from "./core";

class Counter {
  @immutable value = 0;

  @computed get next() {
    return this.value + 1;
  }

  increment = () => this.value += 1;
  decrement = () => this.value -= 1;
}

const App = observe(() => {
  const { value, next, increment, decrement } = shared(Counter);

  return (
    <p>
      Counter: {value} (next value: {next})
      <br />
      <button onClick={decrement}>Prev</button>
      <button onClick={increment}>Next</button>
    </p>
  );
});

Try It on CodeSandbox

Effector like syntax example (76 lines of reactive core):

import React from "react";
import { action, store, selector, useState } from "./core";

const increment = action();
const decrement = action();

const counter = store(0)
  .on(increment, (state) => state + 1)
  .on(decrement, (state) => state - 1);

const next = selector(() => counter.get() + 1);

const App = () => {
  const value = useState(counter);
  const nextValue = useState(next);

  return (
    <p>
      Counter: {value} (next value: {nextValue})
      <br />
      <button onClick={decrement}>Prev</button>
      <button onClick={increment}>Next</button>
    </p>
  );
}

Try It on CodeSandbox

More examples

Articles about

You can easily make your own state manager system or another observable and reactive data flow. It's so funny 😊

How to install

npm i reactive-box

Thanks for your time!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].