oculusx
Easy javascript object observing for browser and node
51 |
52 | Oculus is a latin word for eye or sight
53 |
54 | See Demo
55 | Watching
56 | import { watch } from 'oculusx';
57 |
58 | const appData = {
59 | // empty state at first, but it can be a fully populated object
60 | };
61 |
62 | watch(appData)
63 | ('user.name', (value) => {
64 | // do something when the name changes
65 | })
66 | ('user', (value) => {
67 | // do something when the whole user object changes
68 | // subscription to name remains even if the user is a whole new object and will be invoked
69 | });
70 |
71 | appData.user = {
72 | name: 'John Doe' // both wathcers are inboked
73 | };
74 |
75 | appData.user.name = 'Jane Doe'; // user.name watcher is invoked
76 |
77 | appData.user = undefined; // both watchers are invoked
78 |
79 | appData.user = {
80 | name: 'Jordan Doe' // user.name is still intact and both watchers will be invoked.
81 | };
Unwatching a specific callback
import { watch, unwatch } from 'oculusx'
82 |
83 | const handler_1 = (value) => {
84 | // do something
85 | };
86 |
87 | const handler_2 = (value) => {
88 | // do something
89 | };
90 |
91 | watch(appData)
92 | ('user.name', handler_1)
93 | ('user.name', handler_2);
94 |
95 | unwatch(appData)('user.name', handler_1);
96 |
97 | appData.user = {
98 | name: 'Jaquelin Doe' // only handler_2 is invoked
99 | };
Unwatching all callbacks from a specific path
import { watch, unwatch } from 'oculusx';
100 |
101 | watch(appData)
102 | ('user', () => { /* do something useful */ }) // callback #1
103 | ('user', () => { /* do something useful */ }) // callback #2
104 | ('user.name', () => { /* do something useful */ }); // watch user.name
105 |
106 | unwatch(appData)('user'); // removes all user object watchers
107 |
108 | appData.user.name = 'James Doe'; // user.name watcher is invoked
109 | appData.user = {
110 | name: 'Jimmy Doe' // Only user.name watcher is invoked
111 | };
112 |
Immediate invocation
113 | watch(target)(path, callback, true);
114 |
Alternative syntax
For those who dislike currying syntax:
115 | watch(target)
116 | (path, callback)
117 | (path2, callback2);
118 |
119 | // equals
120 | watch(target, path, callback);
121 | watch(target, path2, callback2);
122 |
123 | // or
124 | watch(target, path, callback)
125 | (path2, callback2);
It also works the same for unwatch.
126 | DOM Binding - browser only feature
Demo
127 | Interface:
type NodeBindingOptions = {
128 | method?: 'attribute'|'text', // defaults to 'text'
129 | attribute?: string, // attribute to be changed applicable only when method is 'attribute',
130 | compute?: Function // observed value can be computed before injected to the element
131 | };
132 |
133 | bindToElement (model: Object, target: Element, path: string, options?:NodeBindingOptions) => Function
134 | // returns unsubscribe function to stop binding
Example:
import 'oculusx/bind-to-element';
135 | const someElement = document.querySelector('#myElement');
136 | const model = {}
137 | const unsubscribe = oculusx.bindToElement(model, someElement, 'path.to.data');
138 | model.path.to = {
139 | data: 'Hello, world'
140 | }; // element's text changed to 'Hello, world'
141 | unsubscribe(); // element is no longer bound to model
Using options:
const someElement = document.querySelector('#myElement');
142 | const unsubscribe = oculusx.bindToElement(model, someElement, 'path.to.data', {
143 | method: 'attribute',
144 | attribute: 'my-attr-name',
145 | compute: x => x.toLowerCase()
146 | });
Future releases
147 | - Unwatching all nested pathes:
unwatch(target)('some.path.*');
148 | - Unwatching all pathes from an object:
unwatch(target, '*');
149 | - Watching arrays. Currently partially working only when shift/unshift are invoked or the whole array is replaced
150 | - Documentation
151 |
152 | Installation
npm i oculusx
153 | yarn add oculusx
154 |