npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reactive-box

v0.9.0

Published

Minimalistic, fast, and highly efficient reactivity

Downloads

786

Readme

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 :sweat_smile:

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 :+1:

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 :blush:

How to install

npm i reactive-box

Thanks for your time!