Skip to content

Keep Key Command

0xMaxLab edited this page Mar 16, 2026 · 6 revisions

By default, RichJSON resolves expressions only once to maximize performance. When a command is resolved, the original command string is overwritten by its result. While efficient, this presents a challenge for objects intended to be reused or re-resolved multiple times (e.g., templates used with $clone or $invoke).

The Keep Key Command system provides a compromise: it allows Key Commands to be preserved and resolved multiple times, while standard Member Commands remain one-time resolutions.


🛠 How it Works

When a command is marked to be "kept," RichJSON ensures the command definition is stored in a way that survives the first resolution phase. This is achieved by isolating the command metadata within the object.

keepKeyCommands(jsonObject)

This function makes the key commands "constant" for a given JSON object by cloning the internal command member. This prevents the second resolution from mutating the original command state of the first resolution.

If you are writing a custom module and want your command to support multiple resolutions, you must invoke the preservation logic within your command function.

Example

import {keepKeyCommands} from "@rjson/parser"

function(root, current, command, member, address, name) {
    // If 'member' is an object representing a key command, 
    // we preserve it so it can be resolved again later.
    keepKeyCommands(member); 

    let result = /* your logic */;
    return result;
}

Key Differences

Feature Member Command Key Command (with Keep)
Resolution Resolved once; string is overwritten. Resolved every time the object is applied.
Performance High (no overhead). Moderate (requires cloning/re-parsing).
Use Case Static data, one-off calculations. Dynamic templates, $clone, $invoke.

Author’s Recommendation: next read Configuration

Clone this wiki locally