Creating Operators
An operator is what a person places on the canvas: "Filter", "Arc Layer". Each one is an OpDef registered with the planner (@noodles.gl/planner, in packages/planner): its ports, its parameters, and how it compiles. This page is a summary of dev-docs/adding-operators.md, which has the full test layout.
Pick the kind
- Planner operator, the default, for anything over rows: a source, a filter or join, a GIS step, an attribute, a layer. It compiles to SQL, a CPU pass or a GPU kernel. Add it to
OPERATORSinpackages/planner/src/operators.ts, or togis.tsfor a GIS step. - Deck operator for a deck.gl object built from parameters rather than rows: a view, a widget, a layer extension, a tile source, a map style. These live in
noodles-editor/src/runtime/deck-ops.ts; the number curves (Bezier Curve, Ramp) are invalue-ops.ts. - Host operator only when the work cannot run in the engine: it calls an API with the user's key, reads the page, or returns JavaScript objects rather than rows.
A planner operator
{
type: 'above', label: 'Above', category: 'rows',
description: 'Keep rows where a column is above a value.',
inputs: [table()], outputs: [out()],
params: [
{ name: 'column', label: 'Column', kind: 'column', default: '', of: 'in' },
{ name: 'value', label: 'Value', kind: 'float', default: 0, step: 0.1, changeRate: 4, port: true },
],
lower(ctx) {
const col = ctx.str('column');
if (!col) ctx.error('Above needs a column');
return {
nodes: [{ id: ctx.id, type: 'filter', input: ctx.input('in'), predicate: `${col} > ${ctx.bind('value')}` }],
outputs: { out: ctx.id },
};
},
},
typeis theopin a project file.labelanddescriptionare what the Add Node menu and the parameter pane show.categoryplaces it in the Add Node menu. There is no second registry.inputsandoutputsare ports.table()andout()are helpers inoperators.tsfor a table port namedinand a table output namedout.lower(ctx)returns IR nodes (sql,filter,join,aggregate,attribute,wrangle,js, layers; seetypes.ts) and which IR node each output port is.ctx.input('in')is the IR node wired into a port, andctx.errorreports a problem on the node.
Key principles
- Bind values, read structure.
ctx.str,ctx.numandctx.boolread a parameter structurally, so changing it recompiles.ctx.bindreturns a placeholder ({{key}}) for SQL or an expression, so a change is routed instead: draggingvalueabove requeries or updates a uniform and never recompiles.float,intandtextparameters are bindable by default, other kinds are structural, andbindoverrides either way (bind: 'prop'makes a parameter a deck prop). - Per-row values are columns. Channels and attributes are expressions over columns, never JavaScript callbacks per row.
changeRate(expected changes per second while dragged) helps the optimizer pick a route.port: truegives a parameter apar:<name>handle that a number output can drive.whenshows a parameter only for some values of another;foldergroups parameters into tabs.- Scalars. An operator that computes a number from its parameters alone has
scalar(p, clock)instead oflower, and joins the scalar program evaluated every frame (Number, Time, Math).
Never Biome-format packages/planner: it keeps its own style.
Parameter kinds
| Kind | For |
|---|---|
float, int | Numbers, with min, max and step |
toggle | A boolean |
menu | One of options |
string, text, url | Text |
column | A column of the input named by of |
expr | A row expression |
code | SQL, JavaScript or another language |
color | [r, g, b] from 0 to 255 |
field | A widget the editor supplies (see Field System) |
A deck operator
Add a DeckOp to DECK_OPS in noodles-editor/src/runtime/deck-ops.ts. Its def is an OpDef whose outputs are value ports, and its build(params, id, env) returns the description deck takes, such as { type: 'CompassWidget', id, placement }. noodles names the older Noodles operator it replaces. registerDeckOperators() registers them all with the planner.
A build that reads an API key with getKeysStore().getKey() lists the keys in usesKeys, so it runs again when the user changes one.
A host operator
A Noodles operator class in noodles-editor/src/noodles/operators.ts, listed in opTypes and planned as host(reason) in src/runtime/migrate/op-catalog.ts:
export class GeocoderOp extends Operator<GeocoderOp> {
static displayName = 'Geocoder'
static description = 'Get the location of a query string'
static usesKeys = ['mapbox'] as const
createInputs() {
return { query: new StringField() }
}
createOutputs() {
return { location: new Point2DField(), results: new DataField() }
}
async execute({ query }: ExtractProps<typeof this.inputs>) {
const apiKey = getKeysStore().getKey('mapbox')
const [result] = await geocodeWithMapbox(query, apiKey)
// ...
}
}
HostRunner runs execute() on the main thread, and rows it returns reach planner nodes through DuckDB. Keep host operators small: each one is work the engine cannot plan.
Code without a new operator
Often a new operator isn't needed:
- An Attribute Create node or a layer channel takes a row expression, such as
distance([lng1, lat1], [lng2, lat2]). See Expressions. - A
sqlnode takes any SELECT.{{input0}}to{{input2}}are the tables wired intoin0toin2, andch('threshold/value')binds a parameter, so dragging it reruns the query without recompiling. - A
codenode runs JavaScript in the engine worker and returns rows, withd3,turfandutilsin scope. - A
js-functionnode defines a function expressions can call per row. DuckDB runs it about 20 times slower than SQL.
Tests and generated files
- Planner behavior:
packages/planner/src/*.test.ts(npm test -w @noodles.gl/planner), usually by lowering a smallEditorDocand checking the IR. - Real SQL: a
*.node.test.tsinnoodles-editor/src/runtime/__tests__/usingopenNodeDuck()fromduckdb-node.ts. Assert on rows, and on what a change cost (compiles, requeries). src/runtime/migrate/examples.node.test.tsruns every example; keep it passing.- A new or changed operator changes the registry snapshot and its reference page. From
noodles-editor/, runnpm run generate:registry, thennpm run generate:op-docs(it writesdocs/reference/operators/<type>.md). Write anything the generated tables can't say under the page's## Remarks; the generator keeps it.
An operator that replaces one from older projects also needs a conversion: see Extending the Framework.