v1.2.1

Lifecycle Hooks

Last updated


This page explains how Lifecycle (src/model/Lifecycle.js) manages the six hooks, the update debounce, and one hook that is not currently wired up anywhere in the source.

The Six Hooks


Lifecycle receives a when config object at construction, mapping to six callbacks:
HookFiresCan return false to cancel?
beforeRenderBefore the initial renderYes
renderedAfter the initial render completesNo (always runs)
beforeUpdateBefore a data update (after the 300ms debounce)Yes
updatedAfter a data update completesNo (always runs)
beforeDestroyBefore destroyYes
destroyedAfter destroy completesNo (always runs)

Only the three before* hooks can cancel the following action by returning false (#beforeAction, Lifecycle.js:22-26); rendered / updated / destroyed always run once their phase completes — there's no cancellation for them.
  1. const editor = new JSONEditor({
  2. id: "editor",
  3. when: {
  4. beforeRender: () => {
  5. // return false to cancel this render
  6. return true;
  7. },
  8. rendered: () => console.log("Render complete"),
  9. beforeUpdate: () => true, // return false to cancel this update
  10. updated: () => console.log("Data updated:", editor.json),
  11. },
  12. });

render(): the Initial Render


After JSONEditor finishes construction and mounts the DOM, it calls lifecycle.render(callback) exactly once. The sequence:
  1. Records #startAt = Date.now()
  2. Runs beforeRenderCallback; if it returns false, the method returns immediately — callback never runs, and rendered never fires
  3. await callback() (the actual node tree render)
  4. Logs Rendered in {ms}ms. to the console
  5. If renderedCallback is set, runs it

update(): the 300ms Debounce


Every node data change (editing a value, switching a type) calls lifecycle.update(callback). It debounces via setTimeout paired with clearTimeout:
  1. async update(cb) {
  2. clearTimeout(this.#updateTimer);
  3. this.#updateTimer = setTimeout(async () => {
  4. // beforeUpdate → callback → updated
  5. }, 300);
  6. }

As a result, continuous input (e.g. typing character-by-character in a textarea) only fires beforeUpdate / updated once, 300ms after input stops — every intermediate call to update() resets the timer and cancels the previously scheduled run. If beforeUpdate returns false, that scheduled update is skipped and updated never runs; the next data change still reschedules normally.

destroy(): Not Currently Invoked Automatically


Lifecycle.destroy(cb) is fully implemented (running beforeDestroy → cb() → destroyed in sequence), but nothing in src/model/JSONEditor.js currently calls lifecycle.destroy(). This means setting when.beforeDestroy / when.destroyed will not fire from any of the existing public API operations (import / reset / enable / disable). Unless your application obtains the Lifecycle instance itself and calls it manually — and there is currently no public accessor, since Lifecycle is stored in JSONEditor's private field #lifecycle — these two hooks will not run in the current version.

Example


Each hook prints its name; type continuously into a value and beforeUpdate / updated fire only once, 300ms after you stop:
  1. <section id="editor"></section>
  2. <script>
  3. const editor = new JSONEditor({
  4. id: "editor",
  5. fill: false,
  6. json: { name: "NanoJSON" },
  7. when: {
  8. beforeRender: () => console.log("beforeRender"),
  9. rendered: () => console.log("rendered"),
  10. beforeUpdate: () => console.log("beforeUpdate"),
  11. updated: () => console.log("updated", editor.json),
  12. },
  13. });
  14. </script>
Live preview · @pardnchiu/nanojson@1.2.2

Further Reading


中文