Sharing data in Javascript
(Created page with "The Javascript language does not support concurrent execution and locking so direct data sharing between different script instances in not possible. This page describes other ...") |
m |
||
Line 11: | Line 11: | ||
If the message is enqueued the communication occurs only from sender to receiver (data / event push). | If the message is enqueued the communication occurs only from sender to receiver (data / event push). | ||
− | For a dispatched message one or more results may be returned in the message parameters (data query / pull). In this case care must be taken to avoid deadlocks caused by having two (or more) scripts waiting for data provided by the other | + | For a dispatched message one or more results may be returned in the message parameters (data query / pull). In this case care must be taken to avoid deadlocks caused by having two (or more) scripts waiting for data provided by the other in a circular manner. |
===Using shared Engine variables=== | ===Using shared Engine variables=== |
Revision as of 14:24, 24 April 2013
The Javascript language does not support concurrent execution and locking so direct data sharing between different script instances in not possible. This page describes other ways of transferring data between Javascript instances and also with other components.
Sending data by messages
A simple (but relatively slow) method of communication between script instances is the use of messages.
One instance of the script installs a message handler for a mutually agreed message. The other instance populates and then dispatches or enqueues the agreed message.
Note that both instances may install a handler for same message in which case a message parameter should be used to establish which script should answer. It is also possible that a message is handled by multiple scripts, for example to add their own ID to a list held in a message parameter.
If the message is enqueued the communication occurs only from sender to receiver (data / event push).
For a dispatched message one or more results may be returned in the message parameters (data query / pull). In this case care must be taken to avoid deadlocks caused by having two (or more) scripts waiting for data provided by the other in a circular manner.
The Engine.shared object allows access to the shared variables held by the Yate Engine. This object allows performing simple set and retrieve operations and also atomic counter increments and decrements.
See also