Giter Site home page Giter Site logo

maksims / mr-observer Goto Github PK

View Code? Open in Web Editor NEW
23.0 3.0 1.0 160 KB

An observer is a wrapper over JSON data, that provides an interface to know when data is changed, with a focus on performance and memory efficiency.

License: MIT License

Handlebars 10.01% JavaScript 89.99%
observer object-proxying data-proxy json-proxy data-centric

mr-observer's Introduction

mr-Observer

Observer is a wrapper over JSON data, that provides an interface to know when data is changed, with a focus on performance and memory efficiency.

For data-centric design, this provides a modular and robust pattern for applications with various data-dependent parts: ui, real-time render, history undo/redo, real-time collaboration, and more. This pattern has been used for years in PlayCanvas Editor providing fast iteration and ease of development in a complex environment.

This library it has two build targets: ES5 (ECMA2009) and ES8+ (modern JS), to maximize browser support without sacraficing benefits of modern JS for majority of users. And can be used with ECMA Modules in Node.js.

GitHub license

๐Ÿš€ Install

Node.js

Install:

npm install mr-observer

In Node.js:

import Observer from 'mr-observer';

Browser

<script type='module' src='mr-observer.min.js'></script>
<script nomodule src='mr-observer.es5.min.js'></script>

Use built files from dist directory for browser. It will load ES8+ version if it is supported (~94%), otherwise it will load ES5 (ECMA2009) version that supports pretty much every platform.

CDN (jsDelivr)

You can use a public CDN for the library:

ES8+ module: https://cdn.jsdelivr.net/npm/[email protected]/dist/mr-observer.min.js
ES5 version: https://cdn.jsdelivr.net/npm/[email protected]/dist/mr-observer.es5.min.js

Example

let earth = new Observer({ age: 4.543, population: 7.594 });

earth.on('population:set', function (path, value) {
    element.textContent = value;
});

earth.set('population', 7.595);
// fires "population:set" event

Usage

Creating:

Creating Observer (object):

let obj = new Observer({ position: { x: 4, y: 2 } });

Creating Observer (array):

let planets = new Observer([ 'mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune' ]);

Get:

Data provided to Observer should not be modified by application logic, it is not cloned either as it is a choice for a developer. Accessing data is possible through .data property or get method. Where get method is more safe approach, as it will handle undefined paths well:

let obj = new Observer({ position: { x: 4, y: 2 } });
console.log(obj.get('position.x')); // 4
console.log(obj.get('position.z')); // undefined
console.log(obj.get('hello.world')); // undefined
console.log(planets.get(8)); // undefined, sorry Pluto

Returned values should NOT be modified by an application logic:

let obj = new Observer({ position: { x: 4, y: 2 } });
let position = obj.get('position');
position.z = 10; // not good
obj.data.position.z = 10; // not good
obj.set('position.z', 10); // good

Set:

Set is used to change data, which can fire "set" and "unset" events.

By specific path:

obj.set('position.y', 64);

Set root of the data:

obj.set({ hello: 'world' });

If root is an array, index can be used:

planets.set(2, 'Earth');

Set will not create missing objects along the path:

let obj = new Observer({ });
obj.set('hello.world', true);
// `hello` is undefined, so this will not change data

Set can fire "unset" events:

let obj = new Observer({ position: { x: 4, y: 2 } });

// this will trigger twice: `position.x` and `position.y`
obj.on('unset', function (path) {
    console.log(`"${path.join('.')}" has been unset`);
});

obj.set('position', { z: 64 });

Patch:

Patch is used to partially update data, it will not unset values if they are missing in provided data. But can still trigger "unset" event when object is replaced with non-object value. If patching an array, insert event can be triggered when new values added.

It can be used same way as set method.

let obj = new Observer({ position: { x: 4, y: 2 } });
obj.patch('position', { z: 64 });
// data now is: { position: { x: 4, y: 2, z: 64 } };

Case where "unset" event can still occur:

let obj = new Observer({ position: { x: 4, y: 2 } });

// this will trigger twice: `position.x` and `position.y`
obj.on('unset', function (path) {
    console.log(`"${path.join('.')}" has been unset`);
});

obj.patch('position', 64);
// data now is: { position: 64 };

Unset:

By specific path:

obj.unset('position.y');

Reset root to an empty object:

obj.unset();

It is possible to clear (sets to an empty object) an observer without firing "unset" events, this will also unsubscribe all events:

obj.clear();

Insert:

When working with arrays, insert, move and remove methods to be used for better control over data.

Insert to an array:

const planets = new Observer({
    earth: {
        satellites: [ ]
    }
});
planets.insert('earth.satellites', 'moon');

Observer as an array:

const primes = new Observer([ 2, 5, 7 ]);
primes.insert('', 11); // inserts to the end

Insert at a specific index:

const primes = new Observer([ 2, 5, 7 ]);
// we forgot prime number 3 which should be second in a list
primes.insert('', 3, 1);

Using negative index to insert from the end:

let planets = new Observer([ 'mercury', 'venus', 'earth', 'mars', 'jupiter', 'uranus', 'neptune' ]);
// we forgot saturn!
// insert to the third index from the end
planets.insert('', 'saturn', -3);

Move:

Moving items within array. It will trigger move event for affected items first, and then for the moved one.

let planets = new Observer([ 'earth', 'mercury', 'venus', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune' ]);
// earth should be third!
// move it from index 0 to index 2
planets.move('', 0, 2);

Move from the end to the beginning:

items.move('', -1, 0);

Remove:

Removing items can trigger move event for affected items, and then for the removed one will trigger remove event.

Remove from the end:

let planets = new Observer([ 'mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune', 'pluto' ]);
// Sorry pluto!
planets.remove('', -1);

Remove at a specific index:

const primes = new Observer([ 2, 3, 4, 5, 7, 11 ]);
// 4 is not a prime!
primes.remove('', 2);

Subscribing to changes:

This library is using mr-EventEmitter for events management.

Set event by a specific path

let obj = new Observer({ });

obj.on('hello:set', function (path, value) {
    console.log(`"hello" has been changed to "${value}"`);
});

// this will fire "hello:set" event
obj.set('hello', 'world');

Set event without path (matches any set):

let obj = new Observer({ });

// this will be executed 3 times
// with these paths: 'position.x', 'position.y', 'position'
obj.on('set', function (path, value) {
    console.log(`"${path.join('.')}" has been changed to "${value}"`);
});

obj.set('position', { x: 4, y: 2 });

Set event using wildcard notation:

let planets = new Observer({ });

// it will match with any first level property name, where second level property name is `name`
planets.on('*.name:set', function (path, value) {
    console.log(`Planet "${path[0]}" name has been changed to "${value}"`);
});

planets.set('earth', { name: 'Earth' });
planets.set('mars', { name: 'Mars' });

Second example:

let obj = new Observer({
    position: { x: 0, y: 0, z: 0 }
});

// it will match path with `position` as first level property and any second level property name
obj.on('position.*:set', function (path, value) {
    console.log(`Axis "${path[1]}" has been changed to "${value}"`);
});

obj.patch('position', { x: 0, y: 4, z: 8 });
obj.set('position.x', 32);

Single trigger of change:

let obj = new Observer({ });

// will trigger only once
obj.once('hello:set', function (path, value) {
    console.log(`"hello" has been changed to "${value}"`);
});

// both will fire "hello:set" event
obj.set('hello', 'world');
obj.set('hello', 'space');

Unset event is similar to Set event, with path or without:

let obj = new Observer({ hello: 'world' });

obj.on('hello:unset', function (path, valueOld) {
    console.log(`"hello" has been unset`);
});

// this will fire "hello:unset" event
obj.unset('hello');

Array events: insert, move and remove do not include index in a path:

let obj = new Observer({
    planets: [ 'mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune', 'pluto' ]
});

obj.on('planets:remove', function(path, value, index) {
    // path is [ 'planets' ]
    console.log(`"${value}" has been removed from ${path.join('.')} at index ${index}`);
});

// Sorry pluto!
obj.remove('planets', -1);

Subscribe to insert events on array:

let obj = new Observer({
    planets: [ 'mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune' ]
});

obj.on('planets:insert', function(path, value, index) {
    console.log(`"${value}" has been added to ${path.join('.')} at index ${index}`);
});

obj.insert('planets', 'planet nine');

Values and objects provided in events are actual for the duration of the event, and should not be stored or modified (if storing is desired then it should be cloned):

let obj = new Observer({ });

let position = null;
obj.on('position:set', function (path, value) {
    value.z = 64; // not good
    position = value; // not good
});

obj.set('position', { x: 4, y: 2 });

Removing events:

Remove event by EventHandler:

let obj = new Observer({ hello: 'world' });

let evt = obj.on('hello:set', function (path, value) {
    console.log(`hello ${value}`);
});

obj.set('hello', 'earth');
obj.set('hello', 'space');
evt.off(); // remove event
obj.set('hello', 'another dimension'); // this will not be logged

It is possible to remove all events, by specific path or using regular expression. For more details refer to EventEmitter API

Building

Builds single file into two ES5 and ES8+ versions using Babel and Terser.
Source file: src/index.js
Built versions ES5 (dist/mr-observer.es5.min.js) and ES8+ (dist/mr-observer.min.js):

npm install
npm run build

mr-observer's People

Contributors

maksims avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jagamypriera

mr-observer's Issues

CDN Build

Maybe have a folder for browser build (dist) and put it to jsDelivr, CDN URL makes easier to prepare single-page applications.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.