@@ -13,18 +13,19 @@ in obscure internal Node-RED errors (typically, `TypeError` being thrown)
13
13
if one calls certain runtime or admin API method too early. This module
14
14
allows waiting deterministically for the runtime API for flows to be ready.
15
15
16
+ ## Use cases
17
+
16
18
If your primary need is to interact programmatically with the flows API of
17
19
an [ embedded Node-RED] instance once it starts up, then this module is for
18
- you. One example use case likely faced by many developers (such as myself)
20
+ you.
21
+
22
+ One use case likely faced by many developers (such as myself)
19
23
of nodes for Node-RED is running automatic testing for their node(s). Ideally
20
24
such testing can include testing discovery, loading, and running of nodes
21
- by a live embedded Node-RED instance.
22
-
23
- _ (In the wild, those who run such tests, including Node-RED itself, seem
24
- to be using mocks of Node-RED. Not only do the mocks take a lot of code to
25
- create, they are also at risk of diverging from what a live Node-RED instance
26
- does. Hence, personally I would rather test manually than use a mock, or
27
- waste my time on creating one.)_
25
+ by a live embedded Node-RED instance. Usually, one of the first steps in
26
+ getting a node to "run" (i.e., Node-RED invoking the constructor for the
27
+ node type) is to add it to a flow, whether one that exists or one that you
28
+ create from scratch programmatically.
28
29
29
30
## Installation
30
31
@@ -56,7 +57,7 @@ promise chain from RED.start():
56
57
var embeddedStart = require (' node-red-embedded-start' );
57
58
RED .start ().then (embeddedStart (RED )).then ((result ) => {
58
59
// result is whatever RED.start() resolved to
59
- // RED.node.getFlow () etc are now ready to use
60
+ // RED.node.getFlows () etc are now ready to use
60
61
}).catch ((err ) => {
61
62
if (/ ^ timed out/ .test (err .message )) {
62
63
// embeddedStart() timed out
@@ -103,6 +104,14 @@ RED.start().then(waitFunc).then(() => {
103
104
});
104
105
```
105
106
107
+ Usually, because the wait function will only be used once, one will write
108
+ the call directly into the promise chain:
109
+ ``` js
110
+ RED .start ().then (embeddedStart (RED , 5000 )).then ((result ) => {
111
+ // do whatever
112
+ });
113
+ ```
114
+
106
115
* ` RED ` is the embedded Node-RED instance.
107
116
* ` timeout ` is the time in milliseconds after which waiting for the
108
117
` nodes-started ` event should time out. Optional, default is 30 seconds.
@@ -154,11 +163,12 @@ some time and hope for the best. Although a couple of seconds will often
154
163
suffice, the time needed will depend on processor speed, storage speed,
155
164
number of flows, number of nodes in those flows, and various other factors.
156
165
A wait-and-hope-for-the-best solution seems therefore unsatisfactory, and
157
- still requires code to implement.
166
+ nevertheless also requires code to implement.
158
167
159
168
This issue has been brought to the attention of the Node-RED developers
160
- [ Node-RED issue #1168 ] , but they responded that the behaviour is intentional
161
- and will thus not be fixed.
169
+ (see Node-RED issues [ #1168 ] and [ #902 ] ), but they responded that the behaviour
170
+ is intentional and will thus not be fixed until the behavior of an embedded
171
+ application is fully defined and implemented.
162
172
163
173
This module allows waiting instead for a certain event (` nodes-started ` ) to
164
174
fire. The event fires at a point in time during the Node-RED startup when
@@ -175,13 +185,14 @@ the code driving the flow API is initialized and thus ready for interaction.
175
185
the flow API being ready does _ not_ (and _ should_ not) mean that any other
176
186
components have fully completed their initialization as well. For example,
177
187
the constructors of nodes may themselves have launched initialization tasks
178
- asynchronously, and there is no way of knowing which in which stage
179
- these are. See [ Node-RED issue 698] for discussion.
188
+ asynchronously, and there is no way of knowing in which stage
189
+ these are. See Node-RED issue [ # 698 ] for discussion.
180
190
181
191
## License
182
192
183
- MIT
193
+ Available under the [ MIT License ] ( LICENSE ) .
184
194
185
- [ Node-RED issue #1168 ] : https://github.com/node-red/node-red/issues/1168
186
- [ Node-RED issue 698 ] : https://github.com/node-red/node-red/issues/698
195
+ [ #1168 ] : https://github.com/node-red/node-red/issues/1168
196
+ [ #902 ] : https://github.com/node-red/node-red/issues/902
197
+ [ #698 ] : https://github.com/node-red/node-red/issues/698
187
198
[ embedded Node-RED ] : http://nodered.org/docs/embedding
0 commit comments