Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
observable.js
Advanced tools
Object.observe is great, but it's hard to create a polyfill for the method.
MaxArt2501 has started a polyfill but, actually I don't like dirty checking. So I create this repo to provide you another way to capture any change on an object.
npm install observable.js
As MaxArt2501 has explained here, we have no way to emulate this method other than dirty checking. Instead of modifying object directly, we use some methods as proxy functions(we call them modifiers
) to make changes and invoke listeners.
If you are not annoyed with Pub/Sub
model, welcome to the conversation :)
import { Observable } from 'observable.js';
// or require('observable.js').Observable
let obs = new Observable(object, listener, accepts);
The above code will create an observable object obs
, here's the explanation of three parameters:
object
: Object, optional. Initial object, all the properties will be assigned to the observable instancelistener
: Function, optional
. Observer listener for the changesaccepts
: Array, optional
. Accepts list of changes, refer to Object.observelistener function is invoked with an array of changes(same as native Object.observe
):
changes
: An array of objects each representing a change. The properties of these change objects are:
name
: The name of the property which was changed.object
: The changed object after the change was made.type
: A string indicating the type of change taking place. One of "add", "update", or "delete".oldValue
: Only for "update" and "delete" types. The value before the change.eg:
let obs = new Observable({ a:1 }, (changes) => {
console.log(changes);
});
obs.set('a', 2);
// changes: [{
// name: 'a',
// object: { a: 2 },
// type: 'update',
// oldValue: 1
// }]
Add new property to the observable object, eg:
let obs = new Observable(); // {}
obs.add('a', 1); // { a: 1 }
Update existed property value, eg:
let obs = new Observable({ a: 1 }); // { a: 1 }
obs.update('a', 2); // { a: 2 }
Delete property from object, eg:
let obs = new Observable({ a: 1 }); // { a: 1 }
obs.delete('a'); // {}
Syntax suger for obs#add
obs#update
, this method will choose which one to use automatically, eg:
let obs = new Observable(); // {}
obs.set('a', 1); // add: { a: 1 }
obs.set('a', 2); // update: { a: 2 }
Register listener to observable object.
Remove listener from all listeners on this observable object.
We also provide you a way to listen changes on single property by import UniqueObsever
:
import { UniqueObsever } from 'observable.js';
// or require('observable.js').UniqueObsever
Unique observer is a sub class of Observable
and accessing property through getter
and setter
. Now you can modify the property directly like obj.a = 1
.
NOTICE: when apply unique observer onto specific property, only changes of type:update will be captured!
Register unique listener to specific property, you can add as many listeners as you want.
eg:
let unq = new UniqueObsever();
unq.unique('a', (changes) => {
console.log(changes);
});
unq.a = 2;
// changes: [{
// name: 'a',
// object: { a: 2 },
// type: 'update',
// oldValue: undefined
// }]
NOTICE: when apply unique listener to a property, any change on the property WILL NOT BE CAPTURED BY LISTENERS REGIESTERED ON Observable
!
Remove unique listener on specific property.
An universal method of registering unique listener to any object.
eg:
let obj = {
a: 1
};
UniqueObsever.watch(obj, 'a', (changes) => {
console.log(changes);
});
obj.a = 2;
// changes: [{
// name: 'a',
// object: { a: 2 },
// type: 'update',
// oldValue: 1
// }]
An universal method of removing unique listener on any object.
Only changes made through obs#add
, obs#update
, obs#set
, obs#delete
will be captured
Don't support change types of "reconfigure", "setPrototype" or "preventExtensions"
MIT.
FAQs
A library provides you a way to listen changes on an object.
The npm package observable.js receives a total of 1 weekly downloads. As such, observable.js popularity was classified as not popular.
We found that observable.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.