Open dhowe opened 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
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
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
https://observablehq.com/@real-john-cheung/test-rs It works, but in a very complex way...
I don't like the look of this at all...
Two questions:
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;
}
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
and also need to figure out how to load the runtime on load with import...
For question 2, the answer is yes.
rs
)rs
, which is then called the first time rs
is used in the (client) notebookOnly import and a cell using rs
works too, but still need to figure out the unload things
My progress so far (updates to context not working quite right):
@Real-John-Cheung please test if this is working correctly by importing RS from here: https://observablehq.com/@dhowe/rsc
I think it works quite well except
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....
https://observablehq.com/@real-john-cheung/import-test here is the link
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 variablefoo
in one cell, refer to it as$foo
inside an 'rs' block in a separate cell.