Reference
Everything a program can use. require('nbrjs').globals() makes the operators globals, require('nbrjs/lib').globals() the library; the playground does both for you.
Operators
| Operator | What it does |
nbr(v) | The field of v on the neighbours. |
rep(init, (prev) => ...) | State across rounds, local: sends nothing. |
share(init, (n) => ...) | Returns and sends the same value; n is the field of what the neighbours sent. |
xc(init, (n) => ...) | The primitive: the result is both kept and sent, and may be a field, a value per neighbour. |
retsend(ret, send) | In xc, keep ret and send send. |
branch(c, () => a, () => b) | Runs one side; devices on different sides do not see each other. b is optional. |
align(key, () => ...) | Aligns by key instead of position: for loops and dynamic structures. |
aggregate(name, fn) | Aligns calls of fn by name: different functions never share messages. No cost in bytes. |
id() | The device id (a number on Bluetooth). |
sense(name) | A local sensor, from sensors() in the config. |
nbrSense(name) | A field measured per neighbour: 'range' (distance), 'rssi' on Bluetooth. |
The initial value fixes the shape of objects: {dist: Infinity, value: v} travels as [dist, value], and a field that is not in it is not sent. Aggregate code under if, ?:, && or || needs branch (chapter 5).
Fields
| Member | What it does |
.self | The device's own value. |
.at(id) | The value for one neighbour. |
.map((v, id) => ...) | A new field, value by value. |
.zip(other, (a, b) => ...) | Combines two fields (or a field and a value). |
.reduce((acc, v) => ..., init) | Folds the neighbours' values, the device itself excluded. Math.min and Math.max work as reducers. |
.min(), .max(), .sum() | Shorthands; an optional argument is the result with no neighbours. |
Library
| Function | What it does |
hopsTo(source, max) | Hops to the nearest source. Beyond max (optional), Infinity: that stops the climb when a source goes away. |
distanceTo(source, metric, max) | Distance along the network; metric defaults to nbrSense('range'). |
broadcast(source, value, metric) | The value of the nearest source, everywhere. |
collect(distance, value, accumulate, none) | Accumulates towards the source along distance: collect(d, v, Math.max, -Infinity) is the largest value. none must leave a value unchanged. |
collectSum(distance, value) | The sum, at the source: collectSum(hopsTo(s), 1) counts the devices. |
isLeader(radius, metric) | Spread-out leaders: never two neighbouring ones, most about radius apart. |
networkMax(v), networkMin(v) | The largest / smallest value in the network (a past maximum stays). |
neighbourCount(), rounds(), countdown(n) | Neighbours, rounds so far, a countdown that stops at 0. |
minId(field, below) | The id of the neighbour with the smallest value, or null. |
Running: start(program, config)
Every target takes target, period (ms between rounds), sensors(id) (the values for sense) and onResult(value, id).
| Target | Options |
'ble' Puck.js, ESP32 | ttl (s a silent neighbour is kept, 3 periods), scan (ms of listening per period, 400), minRssi, txPower (RSSI at 1 m, −60), chunks (adverts a message over 23 bytes may take, 4), group, hashSeed (another hash for operator names, when two collide), hashBytes (1–3 bytes per operator name, 2: more make collisions much rarer). Every board must share chunks, hashSeed and hashBytes. |
'udp' ESP32 on WiFi, Node | wifi: {ssid, password}, position: () => [x, y] and range for a spatial neighbourhood, port (4210), address, id, ttl, group. |
'sim' browser, Node | nodes, layout ('random', 'grid', 'line'), range, seed, loss, skip, ttl (rounds); sensors(id, x, y, round). |
'host' a board and a PC | The PC runs the rounds and simulates the neighbours: see the Puck.js demo. |
In the playground's config.js, sources and obstacles set the initial sources and obstacles, and target: 'ble' simulates with Bluetooth messages (chapter 9).
Under the hood
| Function | What it does |
device(id, hashed) | A device: .round(program, inbox, sensors, nbrSensors) runs one round and returns {value, message}. With hashed, message keys are 2-byte hashes. |
encode(msg), decode(str) | Messages as JSON, keeping Infinity and NaN. |
sim.network(pos, range, opts) | A simulated network: .step(program, sensors). opts: codec, loss, skip, ttl. .collisions lists operators that share a hashed name, .freeSeed() a hashSeed that avoids it. |
sim.layout(kind, n, seed) | Positions for n nodes. |
sim.bleCodec({chunks, hashSeed, hashBytes}) | The Bluetooth format, for opts.codec. |
Tools
- Alignment check (
src/check.js): the playground and the bundler warn about aggregate code under if/?:, in loops or .map(), and about functions picked at run time without aggregate.
- Bundler:
npx nbrjs bundle app.js > app.bundle.js puts the modules and the program in one file, for the Espruino IDE or CLI.
- Types: TypeScript declarations in
types/, for editors; /// <reference types="nbrjs/types/globals" /> for the globals.