object-subscriptions

1.0.2 • Public • Published

object-subscriptions

Nested object with mutations and subscribers

Examples

Calculate and Subscribe

import {NestedObjectWithSubscriptions} from "object-subscriptions";

const obj = new NestedObjectWithSubscriptions();

obj.set("aa.bb", 123);
obj.set("cc.dd", 123);

obj.calculate(["aa.bb", "cc.dd"], (a, b) => {
    return a + b;
}, "ee.ff")

obj.subscribe("ee.ff", (value) => {
    console.log("VAL!", value);
});

Custom Separator and Traversal

import {NestedObjectWithSubscriptions} from "object-subscriptions";

const obj = new NestedObjectWithSubscriptions({}, {separator: '/'});

obj.set("aa/bb", 123);
obj.set("cc/dd", 123);

obj.calculate(["aa/bb", "cc/dd"], (a, b) => {
    return a + b;
}, "ee/ff")

obj.subscribe("ee/ff", (value) => {
    console.log("VAL!", value);
});


for(let [pathParts, value] of obj.entries({pathParts: true})) {
    console.log(pathParts, value);
}

Defer execution

import {NestedObjectWithSubscriptions} from "object-subscriptions";

const obj = new NestedObjectWithSubscriptions({}, {separator: '/'});

obj.set("aa/bb", 123);
obj.set("cc/dd", 123);

obj.calculate(["aa/bb", "cc/dd"], (a, b) => {
    return a + b;
}, "ee/ff", {defer: true})

obj.subscribe("ee/ff", (value) => {
    console.log("VAL!", value);
});

for(let [pathParts, value] of obj.entries({pathParts: true})) {
    console.log(pathParts, value);
}

Debounced calculation

import {NestedObjectWithSubscriptions} from "object-subscriptions";

const obj = new NestedObjectWithSubscriptions({}, {separator: '/'});

obj.set("aa/bb", 123);
obj.set("cc/dd", 123);

obj.calculate(["aa/bb", "cc/dd"], (a, b) => {
    return a + b;
}, "ee/ff", {debounce: 100, immediate: false})

obj.subscribe("ee/ff", (value) => {
    console.log("VAL!", value);
});

for(let [pathParts, value] of obj.entries({pathParts: true})) {
    console.log(pathParts, value);
}

Take a slice

import {NestedObjectWithSubscriptions} from "object-subscriptions";

const obj = new NestedObjectWithSubscriptions({}, {separator: '/'});

obj.set("aa/bb", 123);

const child = obj.child("aa");

console.log("VAL!", child.get([]));

Readme

Keywords

none

Package Sidebar

Install

npm i object-subscriptions

Weekly Downloads

13

Version

1.0.2

License

none

Unpacked Size

28.9 kB

Total Files

10

Last publish

Collaborators

  • johnvmt