All Projects → nx-js → Observer Util

nx-js / Observer Util

Licence: mit
Transparent reactivity with 100% language coverage. Made with ❤️ and ES6 Proxies.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Observer Util

Observable Slim
Observable Slim is a singleton that utilizes ES6 Proxies to observe changes made to an object and any nested children of that object. It is intended to assist with state management and one-way data binding.
Stars: ✭ 178 (-80.33%)
Mutual labels:  observable, proxy, data-binding
ObservableComputations
Cross-platform .NET library for computations whose arguments and results are objects that implement INotifyPropertyChanged and INotifyCollectionChanged (ObservableCollection) interfaces.
Stars: ✭ 94 (-89.61%)
Mutual labels:  observable, reactive-programming
ng-observe
Angular reactivity streamlined...
Stars: ✭ 65 (-92.82%)
Mutual labels:  observable, reactive-programming
observe-component
A library for accessing React component events as reactive observables
Stars: ✭ 36 (-96.02%)
Mutual labels:  observable, reactive-programming
Od Virtualscroll
🚀 Observable-based virtual scroll implementation in Angular
Stars: ✭ 133 (-85.3%)
Mutual labels:  reactive-programming, observable
Redux Most
Most.js based middleware for Redux. Handle async actions with monadic streams & reactive programming.
Stars: ✭ 137 (-84.86%)
Mutual labels:  reactive-programming, observable
rxjs-proxify
Turns a Stream of Objects into an Object of Streams
Stars: ✭ 34 (-96.24%)
Mutual labels:  observable, reactive-programming
Evolui
A tiny reactive user interface library, built on top of RxJs.
Stars: ✭ 43 (-95.25%)
Mutual labels:  reactive-programming, observable
observable-playground
Know your Observables before deploying to production
Stars: ✭ 96 (-89.39%)
Mutual labels:  observable, reactive-programming
Swiftrex
Swift + Redux + (Combine|RxSwift|ReactiveSwift) -> SwiftRex
Stars: ✭ 267 (-70.5%)
Mutual labels:  reactive-programming, observable
Recycle
Convert functional/reactive object description using RxJS into React component
Stars: ✭ 374 (-58.67%)
Mutual labels:  reactive-programming, observable
Lightweightobservable
📬 A lightweight implementation of an observable sequence that you can subscribe to.
Stars: ✭ 114 (-87.4%)
Mutual labels:  reactive-programming, observable
Nestedtypes
BackboneJS compatibility layer for Type-R data framework.
Stars: ✭ 94 (-89.61%)
Mutual labels:  reactive-programming, observable
Pharmacist
Builds observables from events.
Stars: ✭ 221 (-75.58%)
Mutual labels:  observable, reactive-programming
Rocket.jl
Functional reactive programming extensions library for Julia
Stars: ✭ 69 (-92.38%)
Mutual labels:  reactive-programming, observable
realar
5 kB Advanced state manager for React
Stars: ✭ 41 (-95.47%)
Mutual labels:  observable, reactive-programming
Utility
Assign/Partial/ReadOnly/Proxy
Stars: ✭ 31 (-96.57%)
Mutual labels:  observable, proxy
Icaro
Smart and efficient javascript object observer, ideal for batching DOM updates (~1kb)
Stars: ✭ 568 (-37.24%)
Mutual labels:  observable, proxy
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (-93.59%)
Mutual labels:  observable, reactive-programming
Doux
🦄 Immutable reactivity system, made with ES6 Proxy.
Stars: ✭ 460 (-49.17%)
Mutual labels:  reactive-programming, proxy

The Observer Utility

Transparent reactivity with 100% language coverage. Made with ❤️ and ES6 Proxies.

Build Coverage Status JavaScript Style Guide Package size Version dependencies Status License

Table of Contents

Motivation

Popular frontend frameworks - like Angular, React and Vue - use a reactivity system to automatically update the view when the state changes. This is necessary for creating modern web apps and staying sane at the same time.

The Observer Utililty is a similar reactivity system, with a modern twist. It uses ES6 Proxies to achieve true transparency and a 100% language coverage. Ideally you would like to manage your state with plain JS code and expect the view to update where needed. In practice some reactivity systems require extra syntax - like React's setState. Others have limits on the language features, which they can react on - like dynamic properties or the delete keyword. These are small nuisances, but they lead to long hours lost among special docs and related issues.

The Observer Utility aims to eradicate these edge cases. It comes with a tiny learning curve and with a promise that you won't have to dig up hidden docs and issues later. Give it a try, things will just work.

Bindings

This is a framework independent library, which powers the reactivity system behind other state management solutions. These are the currently available bindings.

  • React Easy State is a state management solution for React with a minimal learning curve.
  • preact-ns-observer provides a simple @observable decorator that makes Preact components reactive.

Installation

$ npm install @nx-js/observer-util

Usage

The two building blocks of reactivity are observables and reactions. Observable objects represent the state and reactions are functions, that react to state changes. In case of transparent reactivity, these reactions are called automatically on relevant state changes.

Observables

Observables are transparent Proxies, which can be created with the observable function. From the outside they behave exactly like plain JS objects.

import { observable } from '@nx-js/observer-util';

const counter = observable({ num: 0 });

// observables behave like plain JS objects
counter.num = 12;

Reactions

Reactions are functions, which use observables. They can be created with the observe function and they are automatically executed whenever the observables - used by them - change.

Vanilla JavaScript

import { observable, observe } from '@nx-js/observer-util';

const counter = observable({ num: 0 });
const countLogger = observe(() => console.log(counter.num));

// this calls countLogger and logs 1
counter.num++;

React Component

import { store, view } from 'react-easy-state';

// this is an observable store
const counter = store({
  num: 0,
  up() {
    this.num++;
  }
});

// this is a reactive component, which re-renders whenever counter.num changes
const UserComp = view(() => <div onClick={counter.up}>{counter.num}</div>);

Preact Component

import { observer } from "preact-nx-observer";

let store = observable({ title: "This is foo's data"});

@observer // Component will now re-render whenever store.title changes.
class Foo extends Component {
  render() {
    return <h1>{store.title}</h1>
  }
}

More examples

Dynamic properties
import { observable, observe } from '@nx-js/observer-util';

const profile = observable();
observe(() => console.log(profile.name));

// logs 'Bob'
profile.name = 'Bob';
Nested properties
import { observable, observe } from '@nx-js/observer-util';

const person = observable({
  name: {
    first: 'John',
    last: 'Smith'
  },
  age: 22
});

observe(() => console.log(`${person.name.first} ${person.name.last}`));

// logs 'Bob Smith'
person.name.first = 'Bob';
Getter properties
import { observable, observe } from '@nx-js/observer-util';

const person = observable({
  firstName: 'Bob',
  lastName: 'Smith',
  get name() {
    return `${this.firstName} ${this.lastName}`;
  }
});

observe(() => console.log(person.name));

// logs 'Ann Smith'
person.firstName = 'Ann';
Conditionals
import { observable, observe } from '@nx-js/observer-util';

const person = observable({
  gender: 'male',
  name: 'Potato'
});

observe(() => {
  if (person.gender === 'male') {
    console.log(`Mr. ${person.name}`);
  } else {
    console.log(`Ms. ${person.name}`);
  }
});

// logs 'Ms. Potato'
person.gender = 'female';
Arrays
import { observable, observe } from '@nx-js/observer-util';

const users = observable([]);

observe(() => console.log(users.join(', ')));

// logs 'Bob'
users.push('Bob');

// logs 'Bob, John'
users.push('John');

// logs 'Bob'
users.pop();
ES6 collections
import { observable, observe } from '@nx-js/observer-util';

const people = observable(new Map());

observe(() => {
  for (let [name, age] of people) {
    console.log(`${name}, ${age}`);
  }
});

// logs 'Bob, 22'
people.set('Bob', 22);

// logs 'Bob, 22' and 'John, 35'
people.set('John', 35);
Inherited properties
import { observable, observe } from '@nx-js/observer-util';

const defaultUser = observable({
  name: 'Unknown',
  job: 'developer'
});
const user = observable(Object.create(defaultUser));

// logs 'Unknown is a developer'
observe(() => console.log(`${user.name} is a ${user.job}`));

// logs 'Bob is a developer'
user.name = 'Bob';

// logs 'Bob is a stylist'
user.job = 'stylist';

// logs 'Unknown is a stylist'
delete user.name;

Reaction scheduling

Reactions are scheduled to run whenever the relevant observable state changes. The default scheduler runs the reactions synchronously, but custom schedulers can be passed to change this behavior. Schedulers are usually functions which receive the scheduled reaction as argument.

import { observable, observe } from '@nx-js/observer-util';

// this scheduler delays reactions by 1 second
const scheduler = reaction => setTimeout(reaction, 1000);

const person = observable({ name: 'Josh' });
observe(() => console.log(person.name), { scheduler });

// this logs 'Barbie' after a one second delay
person.name = 'Barbie';

Alternatively schedulers can be objects with an add and delete method. Check out the below examples for more.

More examples

React Scheduler

The React scheduler simply calls setState on relevant observable changes. This delegates the render scheduling to React Fiber. It works roughly like this.

import { observe } from '@nx-js/observer-util';

class ReactiveComp extends BaseComp {
  constructor() {
    // ...
    this.render = observe(this.render, {
      scheduler: () => this.setState()
    });
  }
}
Batched updates with ES6 Sets

Schedulers can be objects with an add and delete method, which schedule and unschedule reactions. ES6 Sets can be used as a scheduler, that automatically removes duplicate reactions.

import { observable, observe } from '@nx-js/observer-util';

const reactions = new Set();
const person = observable({ name: 'Josh' });
observe(() => console.log(person), { scheduler: reactions });

// this throttles reactions to run with a minimal 1 second interval
setInterval(() => {
  reactions.forEach(reaction => reaction());
}, 1000);

// these will cause { name: 'Barbie', age: 30 } to be logged once
person.name = 'Barbie';
person.age = 87;
Batched updates with queues

Queues from the Queue Util can be used to implement complex scheduling patterns by combining automatic priority based and manual execution.

import { observable, observe } from '@nx-js/observer-util';
import { Queue, priorities } from '@nx-js/queue-util';

const scheduler = new Queue(priorities.LOW);
const person = observable({ name: 'Josh' });
observe(() => console.log(person), { scheduler });

// these will cause { name: 'Barbie', age: 30 } to be logged once
// when everything is idle and there is free time to do it
person.name = 'Barbie';
person.age = 87;

Queues are automatically scheduling reactions - based on their priority - but they can also be stopped, started and cleared manually at any time. Learn more about them here.

API

Proxy = observable(object)

Creates and returns a proxied observable object, which behaves just like the originally passed object. The original object is not modified.

  • If no argument is provided, it returns an empty observable object.
  • If an object is passed as argument, it wraps the passed object in an observable.
  • If an observable object is passed, it returns the passed observable object.

boolean = isObservable(object)

Returns true if the passed object is an observable, returns false otherwise.

reaction = observe(function, config)

Wraps the passed function with a reaction, which behaves just like the original function. The reaction is automatically scheduled to run whenever an observable - used by it - changes. The original function is not modified.

observe also accepts an optional config object with the following options.

  • lazy: A boolean, which controls if the reaction is executed when it is created or not. If it is true, the reaction has to be called once manually to trigger the reactivity process. Defaults to false.

  • scheduler: A function, which is called with the reaction when it is scheduled to run. It can also be an object with an add and delete method - which schedule and unschedule reactions. The default scheduler runs the reaction synchronously on observable mutations. You can learn more about reaction scheduling in the related docs section.

  • debugger: An optional function. It is called with contextual metadata object on basic operations - like set, get, delete, etc. The metadata object can be used to determine why the operation wired or scheduled the reaction and it always has enough data to reverse the operation. The debugger is always called before the scheduler.

unobserve(reaction)

Unobserves the passed reaction. Unobserved reactions won't be automatically run anymore.

import { observable, observe, unobserve } from '@nx-js/observer-util';

const counter = observable({ num: 0 });
const logger = observe(() => console.log(counter.num));

// after this the logger won't be automatically called on counter.num changes
unobserve(logger);

obj = raw(observable)

Original objects are never modified, but transparently wrapped by observable proxies. raw can access the original non-reactive object. Modifying and accessing properties on the raw object doesn't trigger reactions.

Using raw at property access

import { observable, observe, raw } from '@nx-js/observer-util';

const person = observable();
const logger = observe(() => console.log(person.name));

// this logs 'Bob'
person.name = 'Bob';

// `name` is used from the raw non-reactive object, this won't log anything
raw(person).name = 'John';

Using raw at property mutation

import { observable, observe, raw } from '@nx-js/observer-util';

const person = observable({ age: 20 });
observe(() => console.log(`${person.name}: ${raw(person).age}`));

// this logs 'Bob: 20'
person.name = 'Bob';

// `age` is used from the raw non-reactive object, this won't log anything
person.age = 33;

Platform support

  • Node: 6.5 and above
  • Chrome: 49 and above
  • Firefox: 38 and above
  • Safari: 10 and above
  • Edge: 12 and above
  • Opera: 36 and above
  • IE is not supported

Alternative builds

This library detects if you use ES6 or commonJS modules and serve the right format to you. The exposed bundles are transpiled to ES5 to support common tools - like UglifyJS minifying. If you would like a finer control over the provided build, you can specify them in your imports.

  • @nx-js/observer-util/dist/es.es6.js exposes an ES6 build with ES6 modules.
  • @nx-js/observer-util/dist/es.es5.js exposes an ES5 build with ES6 modules.
  • @nx-js/observer-util/dist/cjs.es6.js exposes an ES6 build with commonJS modules.
  • @nx-js/observer-util/dist/cjs.es5.js exposes an ES5 build with commonJS modules.

If you use a bundler, set up an alias for @nx-js/observer-util to point to your desired build. You can learn how to do it with webpack here and with rollup here.

Contributing

Contributions are always welcomed! Just send a PR for fixes and doc updates and open issues for new features beforehand. Make sure that the tests and the linter pass and that the coverage remains high. Thanks!

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].