mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2025-12-12 12:42:30 +04:00
Documentation: new doc on Storage module and JS cleanup (#4161)
- Add new doc on Storage module - Improve formatting in the JS section for better readability
This commit is contained in:
@@ -1,27 +1,28 @@
|
||||
# Event Loop module {#js_event_loop}
|
||||
|
||||
```js
|
||||
let eventLoop = require("event_loop");
|
||||
```
|
||||
|
||||
The event loop is central to event-based programming in many frameworks, and our
|
||||
JS subsystem is no exception. It is a good idea to familiarize yourself with the
|
||||
event loop first before using any of the advanced modules (e.g. GPIO and GUI).
|
||||
|
||||
```js
|
||||
let eventLoop = require("event_loop");
|
||||
```
|
||||
|
||||
## Conceptualizing the event loop
|
||||
If you ever wrote JavaScript before, you have definitely seen callbacks. It's
|
||||
when a function accepts another function (usually an anonymous one) as one of
|
||||
the arguments, which it will call later on, e.g. when an event happens or when
|
||||
If you've ever written JavaScript code before, you've definitely seen callbacks. It's
|
||||
when a function takes another function (usually an anonymous one) as one of
|
||||
the arguments, which it will call later, e.g. when an event happens or when
|
||||
data becomes ready:
|
||||
```js
|
||||
setTimeout(function() { console.log("Hello, World!") }, 1000);
|
||||
```
|
||||
|
||||
Many JavaScript engines employ a queue that the runtime fetches events from as
|
||||
Many JavaScript engines employ a queue from which the runtime fetches events as
|
||||
they occur, subsequently calling the corresponding callbacks. This is done in a
|
||||
long-running loop, hence the name "event loop". Here's the pseudocode for a
|
||||
typical event loop:
|
||||
```js
|
||||
|
||||
\code{.js}
|
||||
while(loop_is_running()) {
|
||||
if(event_available_in_queue()) {
|
||||
let event = fetch_event_from_queue();
|
||||
@@ -33,12 +34,14 @@ while(loop_is_running()) {
|
||||
sleep_until_any_event_becomes_available();
|
||||
}
|
||||
}
|
||||
```
|
||||
\endcode
|
||||
|
||||
Most JS runtimes enclose the event loop within themselves, so that most JS
|
||||
programmers does not even need to be aware of its existence. This is not the
|
||||
programmers don't even need to be aware of its existence. This is not the
|
||||
case with our JS subsystem.
|
||||
|
||||
---
|
||||
|
||||
# Example
|
||||
This is how one would write something similar to the `setTimeout` example above:
|
||||
```js
|
||||
@@ -87,14 +90,18 @@ The first two arguments that get passed to our callback are:
|
||||
- The event item, used for events that have extra data. Timer events do not,
|
||||
they just produce `undefined`.
|
||||
|
||||
---
|
||||
|
||||
# API reference
|
||||
## `run`
|
||||
## run()
|
||||
Runs the event loop until it is stopped with `stop`.
|
||||
|
||||
## `subscribe`
|
||||
<br>
|
||||
|
||||
## subscribe()
|
||||
Subscribes a function to an event.
|
||||
|
||||
### Parameters
|
||||
**Parameters**
|
||||
- `contract`: an event source identifier
|
||||
- `callback`: the function to call when the event happens
|
||||
- extra arguments: will be passed as extra arguments to the callback
|
||||
@@ -107,35 +114,45 @@ to `undefined`. The callback may return an array of the same length as the count
|
||||
of the extra arguments to modify them for the next time that the event handler
|
||||
is called. Any other returns values are discarded.
|
||||
|
||||
### Returns
|
||||
**Returns**
|
||||
|
||||
A `SubscriptionManager` object:
|
||||
- `SubscriptionManager.cancel()`: unsubscribes the callback from the event
|
||||
|
||||
### Warning
|
||||
**Warning**
|
||||
|
||||
Each event source may only have one callback associated with it.
|
||||
|
||||
## `stop`
|
||||
<br>
|
||||
|
||||
## stop()
|
||||
Stops the event loop.
|
||||
|
||||
## `timer`
|
||||
<br>
|
||||
|
||||
## timer()
|
||||
Produces an event source that fires with a constant interval either once or
|
||||
indefinitely.
|
||||
|
||||
### Parameters
|
||||
**Parameters**
|
||||
- `mode`: either `"oneshot"` or `"periodic"`
|
||||
- `interval`: the timeout (for `"oneshot"`) timers or the period (for
|
||||
`"periodic"` timers)
|
||||
|
||||
### Returns
|
||||
**Returns**
|
||||
|
||||
A `Contract` object, as expected by `subscribe`'s first parameter.
|
||||
|
||||
## `queue`
|
||||
<br>
|
||||
|
||||
## queue()
|
||||
Produces a queue that can be used to exchange messages.
|
||||
|
||||
### Parameters
|
||||
**Parameters**
|
||||
- `length`: the maximum number of items that the queue may contain
|
||||
|
||||
### Returns
|
||||
**Returns**
|
||||
|
||||
A `Queue` object:
|
||||
- `Queue.send(message)`:
|
||||
- `message`: a value of any type that will be placed at the end of the queue
|
||||
|
||||
Reference in New Issue
Block a user