dhowe / ritajs-v2

RiTa: generative language tools
https://rednoise.org/rita
GNU General Public License v3.0
103 stars 11 forks source link

Pass context to observable 'rs' template #143

Open dhowe opened 3 years ago

dhowe commented 3 years ago

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.

Real-John-Cheung commented 3 years ago

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

dhowe commented 3 years ago

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

Real-John-Cheung commented 3 years ago

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 commented 3 years ago

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

dhowe commented 3 years ago

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

Real-John-Cheung commented 3 years ago

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.

dhowe commented 3 years ago
  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
Real-John-Cheung commented 3 years ago

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

dhowe commented 3 years ago

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 commented 3 years ago

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

Real-John-Cheung commented 3 years ago

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
dhowe commented 3 years ago

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....

Real-John-Cheung commented 3 years ago

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