publisherproxy

2.6.4 • Public • Published

PublisherProxy

Association between a Javascript Proxy and a kind of pubsub pattern

Installation

yarn add publisherproxy

Usage

Let's say you have an object representing a state. We are going to create a publisher associated with this state

const Publisher = require("publisherproxy");
const state = {
    title: "News",
    items: [
        { title: "1th", text: "First news" },
        { title: "2nd", text: "Second news" }
    ]
}
const publisher = new Publisher(state);

Be notified of any state change.

This is used for example to trigger a save of the state for any change, or an api call if a filter has changed.

function save(){
	//do something to save the state
}
publisher.onInternalMutation(save);
publisher.items[1].text = "Second news Modified";

Dynamic filling

Dynamically fill an object with each modification.

const fillable = {};
publisher.startDynamicFilling(fillable);
publisher.items[1].text = "Second news Modified";
publisher.stopDynamicFilling(fillable);
publisher.items[1].text = "Second news Modified Again";
console.log(fillable);

The object is a real-time copy of the state.

Template filling

Identical to the dynamic filling. However, only existing attributes are updated.

const fillableTemplate = { title: "A title to be replaced"};
publisher.startTemplateFilling(fillableTemplate);
state.title = "Good morning";
publisher.stopTemplateFilling(fillableTemplate);
state.title = "Oops";
console.log(fillableTemplate);

This is interesting to use with webComponents with getters setters for example.

Assignment Listener

You can listen to changes of a particular property or sub-property event if a parent object was replaced.

publisher.items[1].text.onAssign(console.log);
publisher.items[1].text = "Second news Modified";
publisher.items = [
    { title: "Ah Ah", text: "Welcome" },
    { title: "Hello", text: "World" }
];

Examples

Please look at the examples for a more in depth view.

Package Sidebar

Install

npm i publisherproxy

Weekly Downloads

0

Version

2.6.4

License

MIT

Unpacked Size

41.2 kB

Total Files

11

Last publish

Collaborators

  • ladigitale