1.0.2 • Published 8 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-observerObjectObserverFactory
A factory that is used to create an IObjectObserver.
newInstance<T>(observed : T, config? : IFactoryConfig) : IObjectObserverA factory method that returns anIObjectObserver. IfProxyis 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? : booleanIf the factory should use fallback mode whenProxyis unavailable. Default istrue.fallbackUpdateFrequency? : numberHow 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.observedis the object that will be observed. Note: Changes ofobservedare not detected by theObjectObserver. Make changes to the object returned bygetObserved().getObserved() : TReturns the observed object. Changing this object will notify the observers.on(on(type : EventType | EventType[], callback : (data : any) => void | IObserver) : { cancel : Function })Adds the observercallbackto the eventtype. The returned object has a functioncancelthat removes the observer. Note: Only the'change'event can be used in fallback mode.off(observer : IObserver) : booleanRemoves the observer. Only works if the bound observer is of typeIObserver. Returnstrueif the removal was successful, otherwise false.countObservers() : numberReturns the number of observers.clearObservers() : voidRemoves 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) : voidCalled 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