Subscriptions
It is often important to be made aware of when something changes in Openexus; notably, properties on a node as they change state. This is acheived through the SDK subscription system. Whenever something changes, everything that is subscribed to listen for changes gets notified.
Single Subscription
To listen to a property change, we simply use the context.subscribe()
function.
context.subscribe(<property name>, <subscribe function>)
Whenever <property name>
changes, <subscribe function>
is executed. The <subscribe function>
receives the new values, the old value, and the property name as arguments: mySubscribeFunction(newValue, oldValue, propertyName)
.
const sub = context.subscribe('count', (value) => {
// Set ((double)) when ((count)) changes
context.set('double', value * 2);
});
One can also To stop listening to a property change, simply call unsubscribe()
with the same arguments you called subscribe. Here's an example again:
const sub = context.subscribe('count', (value) => {
// Set ((double)) when ((count)) changes
context.set('double', value * 2);
});
setTimeout(() => {
// Stop listening to ((count)) changes after 10s
sub.unsubscribe('count', subFunc);
}, 30000);
const sub = context.subscribe('count', (value) => {
// Set ((double)) when ((count)) changes
context.set('double', value * 2);
});
setTimeout(() => {
// Stop listening to ((count)) changes after 30s
sub.unsubscribe();
}, 30000);
`}
Multiple Subscriptions
If you need to subscribe to mutliple properties at once, you can pass in an array of property names, instead of a single property name.
context.subscribe(<array of property names>, <subscribe function>)
If any property of the array changes, the <subscriber function>
will be called for that property. If multiple properties change, the <subscribed function>
will be called one time for each property changed.
const sub = context.subscribe(['count', 'count2'], (value) => {
context.set('total', context.get('count') + context.get('count2'));
});
Subscribe to all properties
Sometimes it is useful to subscribe to all property for a given node at once. This can be accomplished using context.subscribeAll()
.
context.subscribeAll(<subscriber function>)
Whenever any property of the node changes, the subscription will be called. If multiple properties of the node change, a call for each changed properly will be run.
const sub = context.subscribeAll((value) => {
context.set('total', context.get('count1') + context.get('count2') + context.get('count3'));
});
Unsubscribing
As mentioned above one can unsubscribe from a property by passing the same arguments to context.unsubscribe()
that were used to make the subscription in the first place. However, it is worth nothing that when you pass an array of property names to context.subscribe()
you may either unsubscribe to all of them by passing the same array of property names to context.unsubscribe()
, or you may unsubscribe from a single one of the property names in the array at a time by calling context.unsubscribe()
with a single property name.