1.0.2 • Published 7 years ago
typescript-object-observer v1.0.2
typescript-object-observer
Class used to observe objects in Typescript.
The ObjectObserver
uses a Proxy
to intercept calls. When Proxy
is unavailable fallack mode is used.
Installation
With npm
npm install --save typescript-proxy-observer
ObjectObserverFactory
A factory that is used to create an IObjectObserver
.
newInstance<T>(observed : T, config? : IFactoryConfig) : IObjectObserver
A factory method that returns anIObjectObserver
. IfProxy
is unavailable in the environment a fallback mode is used. Note: Only the'change'
event can be used in fallback mode.
IFactoryConfig
Config object for the factory.
enableFallback? : boolean
If the factory should use fallback mode whenProxy
is unavailable. Default istrue
.fallbackUpdateFrequency? : number
How long time in milliseconds that should pass between checks in fallback mode. Default is100
.
IObjectObserver
T - type of the observed object
The ObjectObserver
intercepts changes and notifies its observers.
ObjectObserver(observed : T)
Constructor for theObjectObserver
.observed
is the object that will be observed. Note: Changes ofobserved
are not detected by theObjectObserver
. Make changes to the object returned bygetObserved()
.getObserved() : T
Returns the observed object. Changing this object will notify the observers.on(on(type : EventType | EventType[], callback : (data : any) => void | IObserver) : { cancel : Function })
Adds the observercallback
to the eventtype
. The returned object has a functioncancel
that removes the observer. Note: Only the'change'
event can be used in fallback mode.off(observer : IObserver) : boolean
Removes the observer. Only works if the bound observer is of typeIObserver
. Returnstrue
if the removal was successful, otherwise false.countObservers() : number
Returns the number of observers.clearObservers() : void
Removes all observers from the object.
EventType
A string corresponding to an event. The EventType
can take the following values:
'change'
Called when the object changes. Available in fallback mode.'set'
Called when a property of the object is set or changed. Not available in fallback mode.'delete'
Called when a property is deleted. Not available in fallback mode.
IObserver
An interface that defines an observer.
update(data : any) : void
Called when the observers are notified.
Examples
An example with an array.
import {ObjectObserverFactory} from 'typescript-object-observer';
let objectObserver = ObjectObserverFactory.newInstance<string[]>([], {
enableFallback: false
}),
observedArray = objectObserver.getObserved();
let changeObserver = objectObserver.on('change', data => {
console.log('ChangeEvent');
});
objectObserver.on('set', data => {
console.log('SetEvent');
});
objectObserver.on('delete', data => {
console.log('DeleteEvent');
});
/*
* Prints:
* ChangeEvent
* SetEvent
*/
observedArray.push('foo');
/*
* Prints:
* ChangeEvent
* DeleteEvent
*/
observedArray.splice(0, 1);
// Remove the change observer
changeObserver.cancel();
A short example with an object.
import {ObjectObserverFactory} from 'typescript-object-observer'
type StringDict = {[index : string] : string};
let objectObserver = ObjectObserverFactory.newInstance<StringDict>({}, {
enableFallback: false
}),
observedArray = objectObserver.getObserved();
objectObserver.on('change', data => {
console.log('ChangeEvent');
});
/*
* Prints:
* Object changed
*/
observedArray['foo'] = 'bar';
Contribute
Make sure to run the tests
npm test