0.0.6 • Published 5 years ago

@zalelion/immutable-object v0.0.6

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

immutable-object

Install

yarn add @zalelion/immutable-object

Example

import { sync } from "../src/index";

const obj = {
    name: "hello",
    change(name) {
        this.name = name;
    },
    set: new Set,
    map: new Map,
    sub: { arr: ["1", "2", "333", "44444", "555555"] }
}

const [proxy, unlisten] = sync<typeof obj>(obj, newobj => console.log(newobj));
proxy.map.set("a", "2");

Output:

{ ...
  map: Map { 'a' => '2' }
}

And:

proxy.change("lion");

Output:

{ name: 'lion',
  ...
}

API

import { sync } from "../src/index";

const [obj_proxy , unsubscribe] = sync(obj, cb);
  • obj is a object.
  • cb(newImmutableObj) when obj changed , then callback return a immutable object.
  • obj_proxy is a Proxy , call it to cb(newImmutableObj)
  • unsubscribe is a function, stop subscribe obj change event.