observablehq / runtime

The reactive dataflow runtime that powers Observable Framework and Observable notebooks
https://observablehq.com/@observablehq/how-observable-runs
ISC License
1.01k stars 71 forks source link

Order of nodes in custom observer #345

Closed wiless closed 1 year ago

wiless commented 1 year ago

From the given example: this renders all the nodes/cells in the same order as exported from the notebook.

const runtime = new Runtime();
const main = runtime.module(define, Inspector.into(document.body));

I would like to add all the nodes in a custom observer. I see that the nodes passed on, are not in the same order as in the notebook.

const runtime = new Runtime();
const main = runtime.module(define, name => {
  return {
    pending() {
      console.log(`${name}: pending`);
    },
    fulfilled(value) {
      console.log(`${name}: fullfilled`, value);
    },
    rejected(error) {
      console.error(`${name}: rejected`, error);
    }
  };
});

Any suggestion of getting list of nodes in order ?

wiless commented 1 year ago

Seems I fixed it. I was creating nodes in the fulfilled method, which is obviously not expected to be called in order. created corresponding

/container before the defining the observer

const runtime = new Runtime();
const main = runtime.module(define, name => {

       var node = document.createElement("DIV");
        root.appendChild(node);
  return {
    pending() {
      console.log(`${name}: pending`);
    },
    fulfilled(value) {
// Use the node and customise the render..
      console.log(`${name}: fullfilled`, value);
    },
    rejected(error) {
      console.error(`${name}: rejected`, error);
    }
  };
});