ritajs-v2 icon indicating copy to clipboard operation
ritajs-v2 copied to clipboard

Pass context to observable 'rs' template

Open dhowe opened this issue 3 years ago • 13 comments

Let's see if this is possible in 'rs', same way as we pass State.variables to <<rs>> in Twine. Simple test: define a js variable foo in one cell, refer to it as $foo inside an 'rs' block in a separate cell.

dhowe avatar Mar 22 '21 17:03 dhowe

https://observablehq.com/@real-john-cheung/test-rs haven't figure out how yet

but my idea is to change those might be global ones into ${sth} eval() output something like His name is $name $name is unresolved symbol, so it is very likely a global variables then we can replace it with ${name} and evaluate the string in reverse quotation marks so name call be pointed to the cell.

another direction is to replace the not assigned symbols in the input

Real-John-Cheung avatar Mar 23 '21 15:03 Real-John-Cheung

I'd rather find the global context -- it must be there if we can refer to variable from one cell to another... I can take a look if you don't have success

dhowe avatar Mar 23 '21 15:03 dhowe

hmm, after some research and experiments, I think Observable works like this: The cells created will be parsed by the parser, then evaluated and computed by the runtime, which also add the DOM elements of the cells to the page.

So there is a 'gobal context' in the runtime, but that's not accessible in the cells (hmm more accurately, the runtime of that page is not accessible in the cells on that page, we can created a new runtime (which run by the page's runtime) like this)...


edit I made a post in the forum and I just got a reply saying that there might be a hacky way to do that... gonna check it out

Real-John-Cheung avatar Mar 26 '21 16:03 Real-John-Cheung

https://observablehq.com/@real-john-cheung/test-rs It works, but in a very complex way...

Real-John-Cheung avatar Mar 27 '21 13:03 Real-John-Cheung

I don't like the look of this at all...

Two questions:

  1. can we always get access to the runtime this way?
  2. once we have a runtime, can we not use just use a function like below to create a context:
toContext = function(runtime) {
  let context = {};
  Array.from(runtime._variables)   // extract cell-defined symbols
    .filter(v => v._name !== null && v._type === 1 && thisModules.get(v._module) === 'self')
    .forEach(v => context[v._name] = v._value);
  return context;
}

image

dhowe avatar Mar 28 '21 06:03 dhowe

For question 1, I think under the current version of observable this works all the time (at least I haven't encountered failure yet... ), but for the importing part, it might need to also import the runtime and other stuffs like this 螢幕截圖 2021-03-29 下午9 20 59 螢幕截圖 2021-03-29 下午9 21 51 and also need to figure out how to load the runtime on load with import...

For question 2, the answer is yes.

Real-John-Cheung avatar Mar 29 '21 13:03 Real-John-Cheung

  1. Make sure to test when no variables have been defined (only the single cell using rs)
  2. I think this would need to be a function in the notebook that exports rs, which is then called the first time rs is used in the (client) notebook

dhowe avatar Mar 29 '21 14:03 dhowe

Only import and a cell using rs works too, but still need to figure out the unload things

Real-John-Cheung avatar Mar 30 '21 12:03 Real-John-Cheung

My progress so far (updates to context not working quite right):

  1. The client: https://observablehq.com/@dhowe/rs-client
  2. The notebook exporting "rs": https://observablehq.com/@dhowe/rsc

dhowe avatar Mar 30 '21 19:03 dhowe

@Real-John-Cheung please test if this is working correctly by importing RS from here: https://observablehq.com/@dhowe/rsc

dhowe avatar Apr 06 '21 17:04 dhowe

I think it works quite well except

  1. context doesn't automatically update when some cells are changed (need to click and run the rs cell manually, I guess the update function (like the Generators.change in my above example) needs to be imported to the client in someway too))
  2. the rs cell is not work until the run botton of it is clicked (I think this relates to the problem that the runtime is not fetch when the page is loaded, i.e the runtime is only fetch when the run button of the cell is clicked) 螢幕截圖 2021-04-07 下午1 09 14 this is how the cell looks like after the page is loaded but its run button is not clicked

Real-John-Cheung avatar Apr 07 '21 05:04 Real-John-Cheung

pls include the link for your test

I am aware of issue #1, and I think it is ok like this. Issue #2 is a real problem however - we need the behavior to be identical with or without a context.

I'm confused as to why the rs function runs on load without a context specified, and doesn't appear to run at all with a context specified that depends on the runtime. The only thing I can think is that it has to do with the topological sort, and a specific mechanism not to run cells which depend on the runtime. I'd like to ask this in the forum but it would likely require a much simpler test....

dhowe avatar Apr 07 '21 08:04 dhowe

https://observablehq.com/@real-john-cheung/import-test here is the link

Real-John-Cheung avatar Apr 07 '21 08:04 Real-John-Cheung