From 3dece8f40764a5876f2ea0f196d9a960d72f60ad Mon Sep 17 00:00:00 2001 From: Dmitry Vasilev Date: Mon, 28 Nov 2022 22:02:37 +0800 Subject: [PATCH] fix --- src/cmd.js | 28 +++++++++++++++++++--------- src/effects.js | 6 ------ src/eval.js | 4 ++-- src/index.js | 11 ++++++++--- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/cmd.js b/src/cmd.js index 67d8894..41f5666 100644 --- a/src/cmd.js +++ b/src/cmd.js @@ -754,7 +754,7 @@ const on_async_call = (state, call) => { } } -const load_dir = (state, dir) => { +const do_load_dir = (state, dir) => { const collect_files = dir => dir.kind == 'file' ? [dir] : dir.children.map(collect_files).flat() @@ -763,13 +763,21 @@ const load_dir = (state, dir) => { collect_files(dir).map(f => [f.path, f.contents]) ) - // Clear parse cache and rerun code - return rerun_code({ + return { ...state, - // remove cache - parse_result: null, project_dir: dir, files: {...files, ...state.files}, + } +} + +const load_dir = (state, dir) => { + // Clear parse cache and rerun code + return rerun_code({ + ...do_load_dir(state, dir), + // remove cache. We have to clear cache because imports of modules that are + // not available because project_dir is not available have errors and the + // errors are cached + parse_result: null, }) } @@ -789,12 +797,13 @@ const open_run_window = state => { const get_initial_state = state => { const with_files = state.project_dir == null ? state - : load_dir(state, state.project_dir) + : do_load_dir(state, state.project_dir) const entrypoint = with_files.entrypoint const current_module = with_files.current_module + const html_file = with_files.html_file - const s = { + return { ...with_files, // If module for entrypoint or current_module does not exist, use *scratch* entrypoint: @@ -804,9 +813,10 @@ const get_initial_state = state => { current_module: with_files.files[current_module] == null ? '' : current_module, + html_file: with_files.files[html_file] == null + ? '' + : html_file, } - - return run_code(s, 0) } export const COMMANDS = { diff --git a/src/effects.js b/src/effects.js index c9f4fae..a56e275 100644 --- a/src/effects.js +++ b/src/effects.js @@ -122,12 +122,6 @@ const render_parse_result = (ui, state) => { export const render_initial_state = (ui, state) => { ensure_session(ui, state) ui.editor.switch_session(state.current_module) - render_parse_result(ui, state) - if(state.current_calltree_node != null) { - ui.render_debugger(state) - render_coloring(ui, state) - } - load_external_imports(state) } export const render_common_side_effects = (prev, next, command, ui) => { diff --git a/src/eval.js b/src/eval.js index 44889a9..a9dd42e 100644 --- a/src/eval.js +++ b/src/eval.js @@ -58,8 +58,8 @@ type Node = ToplevelCall | Call // TODO just export const Iframe_Function? const make_function = (...args) => { - if(globalThis.run_window == null) { - // Tests are run in Node.js or user have not opened run_window + if(globalThis.process != null) { + // Tests are run in Node.js return new Function(...args) } else { // Code run in browser and user opened run_window diff --git a/src/index.js b/src/index.js index 6738dda..db46492 100644 --- a/src/index.js +++ b/src/index.js @@ -141,18 +141,23 @@ export const init = (container, _COMMANDS) => { read_modules().then(initial_state => { - open_run_iframe(initial_state) - state = COMMANDS.get_initial_state({ ...initial_state, on_async_call: (...args) => exec('on_async_call', ...args) }) + // Expose state for debugging globalThis.__state = state ui = new UI(container, state) // Expose for debugging globalThis.__ui = ui + render_initial_state(ui, state) + + open_run_iframe(state) + + // TODO exec on iframe load + exec('open_run_window') }) } @@ -186,7 +191,7 @@ export const exec = (cmd, ...args) => { } // Sanity check - if(state?.parse_result == null) { + if(state?.current_module == null) { console.error('command did not return state, returned', result) throw new Error('illegal state') }