proxy-observable
ES6 Proxy observable implementation. Supports Arrays and Objects
Attention!
Please keep in mind that the implementation is based on ES6 Proxy feature which is a very slow thingy
Install
npm install proxy-observable --save
Usage
Just call observable()
if you want an object or an array to be observable
; const soldier = name: "Titus Pullo" age: 36 inventory: friends: ; console;
Object
const onCoinsChanged = soldierinventory;soldierinventorycoins = 100; // onCoinsChanged will be calledsoldierinventory;
Simply speaking soldier.inventory
is still just a simple object, but it has additionally a few things:
- method
on
Attach a handler to an event for the elements - method
once
Attach a handler to an event for the elements. The handler is executed at most once per element per event type - method
off
Remove an event handler - you can call just
soldier.inventory.newProp = value
to define a new prop, instead ofsoldier.inventory["newProp"] = value
Array
soldierfriends;soldierfriends;soldierfriends; soldierfriends0 = "Lucius Vorenus"soldierfriends;
soldier.friends
is still just a simple array with on
, once
and off
methods and predefined events
Array events
change
- Fires when an item in an array is changed:
arr0 = "new value";// orarr;
pop
- Fires when pop
method is called:
arr;
shift
- Fires when shift
method is called:
arr;
any other Array's methods
any
event
Do you want to track all the events? Just use any
like this:
// objectsoldierinventory;soldierinventorycoins = 1000;// This way you can track when a new property is added to an objectsoldierinventoryshield = "Gold Shield"; // arraysoldierfriends;soldierfriends0 = "Mark Antony";soldierfriends;
Browser Usage
<!-- ES6 not minified version --><!-- ES5 minified version --><!--<script src="../node_modules/proxy-observable/bin/proxy.observable.min.js"></script>-->
One more example
const Frodo = ; const Samwise = name: "Samwise Gamgee" friends: Frodo; Frodofriends; Frodobag; Frodofriends;Frodobag;Frodobag;Frodobag;Frodofriends;
API
ctx
observable(ctx) => Makes an object or array observable
Returns: object|array
- Observable object or array
Param | Type | Description |
---|---|---|
ctx | object|array |
Input Object or Array |
callback
observable.on(e, callback) => Subscribes on event (property changed)
Returns: function
- Input callback for unsubscribing
Param | Type | Description |
---|---|---|
e | string |
Event name or property name |
callback | function |
Callback |
Callback signature
(value, prev, e) => {}
Param | Type | Description |
---|---|---|
value | any |
Current value |
prev | any |
Previous value |
e | string |
Event name or property name |
callback
observable.once(e, callback) => Subscribes once. The handler is executed at most once per element per event type
Returns: function
- Input callback for unsubscribing
Param | Type | Description |
---|---|---|
e | string |
Event name or property name |
callback | function |
Callback |
boolean
observable.off(callback) => Removes event handler
Returns: boolean
- True if unsubscribed
Param | Type | Description |
---|---|---|
callback | function |
Callback |
ES6 JavaScript Proxy MDN documentation
godban
Browsers support made byIE / Edge |
Firefox |
Chrome |
Safari |
Opera |
iOS Safari |
Chrome for Android |
---|---|---|---|---|---|---|
Edge | last 3 versions | last 10 versions | last 5 versions | last 4 versions | last 2 versions | last 3 versions |
License
MIT