Skip to content

Commit b06e9db

Browse files
committed
Add context docs ahead of 0.19
1 parent 10b49a6 commit b06e9db

22 files changed

Lines changed: 472 additions & 81 deletions

_includes/api-toc.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
<li {% if page.url contains "/docs/api/storage/methods" %}class="active"{% endif %}><a href="/docs/api/storage/methods">Methods</a>
2929
</ul>
3030
</li>
31+
<li class="tocheader">
32+
<ul>
33+
<li class="toctitle{% if page.url == "/docs/api/context/" %} active{% endif %}"><a href="/docs/api/context/">Context Store API</a>
34+
<li {% if page.url contains "/docs/api/context/methods" %}class="active"{% endif %}><a href="/docs/api/context/methods">Methods</a>
35+
</ul>
36+
</li>
3137
<li class="tocheader">
3238
<ul>
3339
<li class="toctitle{% if page.url == "/docs/api/ui/" %} active{% endif %}"><a href="/docs/api/ui/">Editor Widgets</a>

_includes/user-guide-toc.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<li {% if page.url == "/docs/user-guide/nodes" %}class="active"{% endif %}><a href="/docs/user-guide/nodes">The core nodes</a></li>
99
<li {% if page.url == "/docs/writing-functions" %}class="active"{% endif %}><a href="/docs/writing-functions">Writing Functions</a></li>
1010
<li {% if page.url == "/docs/user-guide/messages" %}class="active"{% endif %}><a href="/docs/user-guide/messages">Working with messages</a></li>
11+
<li {% if page.url == "/docs/user-guide/context" %}class="active"{% endif %}><a href="/docs/user-guide/context">Working with context</a></li>
1112
</ul>
1213
</li>
1314
<li class="tocheader">

docs/api/context/index.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
layout: docs
3+
toc: api-toc.html
4+
title: Context Store API
5+
---
6+
7+
**New in 0.19**
8+
9+
The Context Store API provides a pluggable way to configure where context data is
10+
stored.
11+
12+
By default, Node-RED uses a memory-based implementation of this API. It also provides
13+
a file-based implementation.
14+
15+
The API functions are documented [here](methods/).
16+
17+
### Configuration
18+
19+
The `contextStorage` property in settings.js can be used to configure context storage.
20+
21+
It is an object with one or more named context store configurations.
22+
23+
{% highlight javascript %}
24+
contextStorage: {
25+
default: {
26+
module:"memory",
27+
config: {
28+
customOption: 'value'
29+
}
30+
}
31+
}
32+
{% endhighlight %}
33+
34+
Each context store configuration consists of two parts; a `module` property and a `config`
35+
property.
36+
37+
The `module` property identifies the context store plugin to use. It can either be
38+
the name of built-in module (currently either `memory` or `file`), or it should be
39+
a module that has been loaded using `require`.
40+
41+
{% highlight javascript %}
42+
contextStorage: {
43+
default: {
44+
module:"memory",
45+
},
46+
custom: {
47+
module:require("my-custom-store")
48+
}
49+
}
50+
{% endhighlight %}
51+
52+
The `config` property is an object that is passed to the module to provide an
53+
custom options.
54+
55+
### Store Module API
56+
57+
A custom plugin's module must export a single constructor function. This function is called when a
58+
new instance of the plugin is required. The function is passed the value of the `config`
59+
property for the given instance. This allows the runtime to have multiple instances
60+
of the same store plugin, each with its own configuration.
61+
62+
{% highlight javascript %}
63+
64+
var ContextStore = function(config) {
65+
this.config = config;
66+
}
67+
68+
ContextStore.prototype.open = function() { ... }
69+
70+
71+
module.exports = function(config){
72+
return new ContextStore(config);
73+
};
74+
75+
{% endhighlight %}
76+
77+
78+
The object returned by the constructor function must implement all of the functions
79+
detailed [here](methods/).

docs/api/context/methods/index.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
layout: docs
3+
toc: api-toc.html
4+
title: Context Store API
5+
---
6+
7+
**New in 0.19**
8+
9+
A Context Storage plugin is a node.js module that exposes a function on its `module.exports`
10+
that can be used to create new instances of the plugin. The object returned by the
11+
function must have the following functions:
12+
13+
Function | Description
14+
---------------------------------------------------------------|-------------------------
15+
[ContextStore.open()](#contextstoreopen) | Open the storage ready for use
16+
[ContextStore.close()](#contextstoreclose) | Close the storage
17+
[ContextStore.get(scope, key, callback)](#contextstoregetscope-key-callback) | Get values from the store
18+
[ContextStore.set(scope, key, value, callback)](#contextstoresetscope-key-value-callback) | Set values in the store
19+
[ContextStore.keys(scope, calback)](#contextstorekeysscope-callback) | Get a list of all keys in the store
20+
[ContextStore.delete(scope)](#contextstoredeletescope) | Delete all keys for a given scope
21+
[ContextStore.clean(activeNodes)](#contextstorecleanactivenodes) | Clean the context store
22+
23+
### ContextStore.open()
24+
25+
Open the storage ready for use. This is called before any store values are accessed.
26+
27+
Returns a `Promise` that resolves when the store is ready for access.
28+
29+
### ContextStore.close()
30+
31+
Called when the runtime is stopped so no further key values will be accessed.
32+
33+
Returns a `Promise` that resolves with the store is closed.
34+
35+
### ContextStore.get(scope, key, [callback])
36+
37+
Argument | Description
38+
---------|------------------------------
39+
scope | the scope of the key
40+
key | the key, or array of keys, to return the value(s) for.
41+
callback | *optional* a callback function to invoke with the key value
42+
43+
The `key` argument can be either a String identifying a single key, or an Array
44+
of Strings identifying multiple keys to return the values for.
45+
46+
47+
If the optional `callback` argument is provided, it must be a function that takes
48+
two or more arguments:
49+
50+
```
51+
function callback(error, value1, value2, ... ) {
52+
53+
}
54+
```
55+
56+
If no callback is provided, and the store supports synchronous access, the
57+
`get` function should return the individual value, or array of values for the keys.
58+
If the store does not support synchronous access it should throw an error.
59+
60+
### ContextStore.set(scope, key, value, [callback])
61+
62+
Argument | Description
63+
---------|------------------------------
64+
scope | the scope of the key
65+
key | the key, or array of keys, to set the value(s) for.
66+
value | the value, or array of values
67+
callback | *optional* a callback function to invoke when the value is set
68+
69+
The `key` argument can be either a String identifying a single key, or an Array
70+
of Strings identifying multiple keys to set.
71+
72+
`key` | `value` | Action
73+
-------------|----------------|----------------
74+
String | Any | Stores `value` under `key`
75+
Array | Array | Stores each element of the `value` array under the corresponding `key` value. If `value` has fewer elements than `key`, it sets the missing values to `null`.
76+
Array | not-Array | Stores `value` as the value of the first key - uses `null` for any remaining keys.
77+
78+
79+
If the optional `callback` argument is provided, it will be called when the value
80+
has been stored. It takes a single argument, `error`, to indicate any errors hit
81+
whilst storing the values.
82+
83+
```
84+
function callback(error) {
85+
86+
}
87+
```
88+
89+
If no callback is provided, and the store supports synchronous access, the
90+
`set` function should return once the value is stored. If the store does not support
91+
synchronous access it should throw an error.
92+
93+
### ContextStore.keys(scope, [callback])
94+
95+
Argument | Description
96+
------------|------------------------
97+
scope | the scope of the keys to return
98+
callback | *optional* a callback function to invoke with the list of keys
99+
100+
Gets a list of all keys under the given scope.
101+
102+
If the optional `callback` argument is provided, it must be a function that takes
103+
two or more arguments:
104+
105+
```
106+
function callback(error, keys) {
107+
108+
}
109+
```
110+
111+
If no callback is provided, and the store supports synchronous access, the
112+
`keys` function should return the array of keys. If the store does not support
113+
synchronous access it should throw an error.
114+
115+
116+
### ContextStore.delete(scope)
117+
118+
Argument | Description
119+
------------|------------------------
120+
scope | the scope to delete
121+
122+
123+
### ContextStore.clean(activeNodes)
124+
125+
Argument | Description
126+
------------|------------------------
127+
activeNodes | a list of all node/flow ids that are still active
128+
129+
Returns a promise that resolves when store has removed any context scopes that
130+
are no longer required. The `activeNodes` list can be used to identify what nodes
131+
and flows are still considered active.

docs/api/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ This API can be used when embedding Node-RED into another application.
1818
This API provides a pluggable way to configure where the Node-RED runtime stores
1919
data.
2020

21+
### [Context Store API](context)
22+
23+
**New in 0.19** : This API provides a pluggable way to store context data outside of the runtime.
24+
2125
### [Editor UI Widgets](ui)
2226

2327
A set of jQuery widgets that can be used within a node's edit template. _Since 0.14_

docs/configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ user-provided settings file. It can also be used as a starting point for creatin
1818
your own settings file. It can be seen on GitHub [here](https://github.com/node-red/node-red/blob/master/settings.js).
1919

2020
<div class="doc-callout">
21-
<em>Note</em>: The <code>settings.js</code> file exports a <em>JavaScript object</em>. To configure Node-RED you should understand how to modify a JavaScript object by adding new or modifying existing key/value pairs.
21+
<em>Note</em> : The <code>settings.js</code> file exports a <em>JavaScript object</em>. To configure Node-RED you should understand how to modify a JavaScript object by adding new or modifying existing key/value pairs.
2222
</div>
2323

2424
When [embedded](embedding), they are passed in the call to `RED.init()`.
@@ -197,7 +197,7 @@ functionGlobalContext
197197

198198
var myos = global.get('osModule');
199199

200-
<div class="doc-callout"><em>Note</em>: Prior to Node-RED v0.13, the documented
200+
<div class="doc-callout"><em>Note</em> : Prior to Node-RED v0.13, the documented
201201
way to use global context was to access it as a sub-property of <code>context</code>:
202202
<pre>context.global.foo = "bar";
203203
var osModule = context.global.osModule;</pre>

docs/creating-nodes/context.md

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,28 @@ toc: creating-nodes-toc.html
44
title: Node context
55
---
66

7-
A node can store data within its context object. This context object is reset
8-
whenever the node is redeployed and when Node-RED is restarted.
7+
A node can store data within its context object.
98

10-
{% highlight javascript %}
11-
// Access the node's context object
12-
var context = this.context();
13-
14-
var count = context.get('count') || 0;
15-
count += 1;
16-
context.set('count',count);
9+
For more information about context, read the [Working with Context guide](../user-guide/context).
1710

18-
{% endhighlight %}
11+
There are three scopes of context available to a node:
1912

20-
##### Flow context
13+
- Node - only visible to the node that set the value
14+
- Flow - visible to all nodes on the same flow (or tab in the editor)
15+
- Global - visible to all nodes
2116

22-
The flow-level context is shared by all nodes on a given tab.
17+
Unlike the Function node which provides predefined variables to
18+
access each of these contexts, a custom node must access these
19+
contexts for itself:
2320

2421
{% highlight javascript %}
25-
var flowContext = this.context().flow;
26-
var count = flowContext.get('count')||0;
27-
{% endhighlight %}
28-
29-
##### Global context
22+
// Access the node's context object
23+
var nodeContext = this.context();
3024

31-
The global context is shared by, and accessible to all nodes. For example to
32-
make the variable `foo` available globally:
25+
var flowContext = this.context().flow;
3326

34-
{% highlight javascript %}
3527
var globalContext = this.context().global;
36-
globalContext.set("foo","bar"); // this is now available to other nodes
3728
{% endhighlight %}
3829

39-
##### Accessing context in a Function node
40-
41-
In the Function node, the `flow` and `global` context objects are made available
42-
as top-level objects. See [this section](/docs/writing-functions#storing-data)
43-
for more information.
30+
Each of these context objects has the same `get`/`set` functions described
31+
in the [Writing Functions guide](/docs/writing-functions#storing-data).

docs/creating-nodes/first-node.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ On Windows with older versions of npm, use `mklink` instead:
179179
mklink /D node_modules\node-red-contrib-example-lower-case C:\Users\my_name\Documents\GitHub\node-red-contrib-example-lower-case
180180

181181
<div class="doc-callout">
182-
<em>Note</em>: npm 5 will add your module as a dependency in the <code>package.json</code> file located in your user directory. If you want to prevent this, use the npm <code>--no-save</code> option.
182+
<em>Note</em> : npm 5 will add your module as a dependency in the <code>package.json</code> file located in your user directory. If you want to prevent this, use the npm <code>--no-save</code> option.
183183
</div>
184184

185185
### Unit Testing

docs/creating-nodes/packaging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ To help make the nodes discoverable within the npm repository, the file should
5050
include `node-red` in its `keywords` property. This will ensure the package
5151
appears when [searching by keyword](https://www.npmjs.org/browse/keyword/node-red).
5252

53-
<div class="doc-callout"><em>Note</em>: Please do NOT add the `node-red` keyword until
53+
<div class="doc-callout"><em>Note</em> : Please do NOT add the `node-red` keyword until
5454
you are happy that the node is stable and working correctly, and documented sufficiently
5555
for others to be able to use it.</div>
5656

docs/getting-started/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Once cloned, the core pre-requisite modules must be installed :
6464
npm install
6565

6666
<div class="doc-callout">
67-
<em>Note</em>: when running from a clone of the git repository, it is necessary
67+
<em>Note</em> : when running from a clone of the git repository, it is necessary
6868
to install all dependencies, not just the production level ones. This is why the
6969
<code>--production</code> option should not be used.
7070
</div>

0 commit comments

Comments
 (0)