ObjectEvent
The Event module provides an event object for collaborative shared data operations. It allows synchronized operations between multiple fibers (coroutines) to achieve cooperative multitasking. The event object has three methods: wait, pulse and clear. The wait method will block the current fiber until the event is triggered, the pulse method will wake up all fibers waiting for the event, and the clear method will reset the event flag to false. by usingcoroutine.EventModule, developers can control the execution sequence and data sharing between fibers to implement complex business logic.
For example, suppose we need to share data between two fibers, but the order of their execution is uncertain. You can control the execution order of fibers through an event object to ensure that one fiber is triggered before the other is executed. Fiber events.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19const coroutine = require('coroutine');
var evt = new coroutine.Event();
coroutine.start(function() {
console.log('[1] wait for event');
evt.wait();
console.log('[1] receive event');
});
coroutine.start(function() {
loop: for (var i = 0; i < 10; i++) {
console.log('[2] do some work');
if (i === 5) {
evt.pulse();
}
coroutine.sleep(1000);
}
});
In the above example, we created an event object evt, used the wait method in fiber 1 to wait for the event to be triggered, and used the pulse method to trigger the event in fiber 2. When i equals 5, fiber 2 triggers the event. , Fiber 1 is awakened through event listening and continues execution. In this process, no locks or other synchronization tools are used between the two fibers, but they ensure data synchronization at the fiber level.
inheritance relationship
Constructor
Event
event object constructor
1new Event(Boolean value = false);
Call parameters:
- value: Boolean, specifies whether to wait, wait when true, default is false
member function
isSet
Determine whether the event object is true
1Boolean Event.isSet();
Return results:
- Boolean, if the event is true, return true
set
Activate the event (change the event status to true) and call pulse()
1Event.set();
pulse
Activate all fibers waiting for this event
1Event.pulse();
clear
Reset event (change event status to false)
1Event.clear();
wait
wait for an event
1Event.wait();
acquire
Get ownership of the lock
1Boolean Event.acquire(Boolean blocking = true);
Call parameters:
- blocking: Boolean, specifies whether to wait, wait when true, default is true
Return results:
- Boolean, returns whether the lock was successfully acquired, true indicates successful acquisition.
The acquire method is used to obtain ownership of the lock. When the lock is in the acquireable state, this method returns true immediately.
When the lock cannot be acquired and blocking is true, the current fiber goes to sleep. When other fibers release the lock, this method returns true.
When the lock cannot be acquired and blocking is false, the method returns false.
release
Release ownership of the lock
1Event.release();
This method will release ownership of the lock or throw an error if the current fiber does not own the lock.
count
Query the current number of waiting tasks
1Integer Event.count();
Return results:
- Integer, returns the number of tasks
toString
Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.
1String Event.toString();
Return results:
- String, returns the string representation of the object
toJSON
Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.
1Value Event.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable