Files
leporello-js/src/index.js

222 lines
5.8 KiB
JavaScript
Raw Normal View History

2022-09-10 02:48:13 +08:00
import {UI} from './editor/ui.js'
import {EFFECTS, render_initial_state, render_common_side_effects} from './effects.js'
import {load_dir} from './filesystem.js'
const EXAMPLE = `const fib = n =>
n == 0 || n == 1
? n
: fib(n - 1) + fib(n - 2)
fib(6)`
2022-11-28 23:12:55 +08:00
// Fake directory, http requests to this directory intercepted by service_worker
export const FILES_ROOT = '__leporello_files'
const set_error_handler = w => {
// TODO err.message
w.onerror = (msg, src, lineNum, colNum, err) => {
ui.set_status(msg)
}
w.addEventListener('unhandledrejection', (event) => {
ui.set_status(event.reason)
})
}
2022-10-26 13:11:51 +08:00
2022-11-28 20:53:35 +08:00
const get_html_url = state => {
2022-11-28 23:12:55 +08:00
const base = window.location.origin + '/' + FILES_ROOT + '/'
2022-11-28 20:53:35 +08:00
return state.html_file == ''
2022-11-28 23:12:55 +08:00
? base + '__leporello_blank.html'
: base + state.html_file + '?leporello'
2022-11-28 20:53:35 +08:00
}
2022-10-26 13:11:51 +08:00
// By default run code in hidden iframe, until user explicitly opens visible
// window
2022-11-28 20:53:35 +08:00
const open_run_iframe = state => {
2022-10-26 13:11:51 +08:00
const iframe = document.createElement('iframe')
2022-11-28 20:53:35 +08:00
iframe.src = get_html_url(state)
2022-10-26 13:11:51 +08:00
iframe.setAttribute('hidden', '')
document.body.appendChild(iframe)
set_error_handler(iframe.contentWindow)
2022-11-28 20:53:35 +08:00
globalThis.run_window = iframe.contentWindow
}
2022-10-26 13:11:51 +08:00
2022-11-26 02:56:32 +08:00
// Open another browser window so user can interact with application
// TODO test in another browsers
2022-11-28 20:53:35 +08:00
export const open_run_window = state => {
2022-11-26 02:56:32 +08:00
globalThis.run_window.close()
2022-11-28 20:53:35 +08:00
const next_window = globalThis.run_window = open(get_html_url(state))
2022-11-26 02:56:32 +08:00
const is_loaded = () => {
const nav = next_window.performance.getEntriesByType("navigation")[0]
return nav != null && nav.loadEventEnd > 0
}
// Wait until `load` event before executing code, because service worker that
// is responsible for loading external modules seems not working until `load`
// event fired. TODO: better register SW explicitly and don't rely on
// already registered SW?
const onload = () => {
exec('open_run_window')
2022-10-26 01:05:52 +08:00
}
2022-11-26 02:56:32 +08:00
const add_load_handler = () => {
/*
Wait until 'load event', then set unload handler. The page after
window.open seems to go through these steps:
- about:blank gets opened
- Real URL get opened
- 'unload' event for about:blank page
- 'load event for real URL
if we set unload handler right now, then it will be fired for unload
event for about:blank page
*/
if(is_loaded()) {
// Already loaded
add_unload_handler()
onload()
} else {
next_window.addEventListener('load', () => {
add_unload_handler()
onload()
})
}
}
const add_unload_handler = () => {
next_window.addEventListener('unload', (e) => {
// Set timeout to 100ms because it takes some time for page to get closed
// after triggering 'unload' event
setTimeout(() => {
if(next_window.closed) {
// If by that time next_window.closed was set to true, then page was
// closed
// TODO get back to iframe?
} else {
add_load_handler()
}
}, 100)
})
}
add_load_handler()
2022-10-26 01:05:52 +08:00
}
2022-11-28 20:53:35 +08:00
export const reload_run_window = state => {
// TODO after window location reload, open_run_window command will be fired.
// Maybe we should have separate commands for open_run_window and
// reload_run_window?
globalThis.run_window.location = get_html_url(state)
}
2022-09-10 02:48:13 +08:00
const read_modules = async () => {
const default_module = {'': localStorage.code || EXAMPLE}
const current = {
// TODO fix when there are no such modules anymore
current_module: localStorage.current_module ?? '',
entrypoint: localStorage.entrypoint ?? '',
2022-11-28 20:53:35 +08:00
html_file: localStorage.html_file ?? '',
2022-09-10 02:48:13 +08:00
}
const project_dir = await load_dir(false)
if(project_dir == null) {
// Single anonymous module
return {
...current,
files: default_module,
}
} else {
return {
...current,
project_dir,
files: default_module,
}
}
}
2022-11-26 01:25:31 +08:00
let COMMANDS
2022-09-10 02:48:13 +08:00
let ui
let state
2022-11-26 01:25:31 +08:00
export const init = (container, _COMMANDS) => {
COMMANDS = _COMMANDS
set_error_handler(window)
2022-09-10 02:48:13 +08:00
read_modules().then(initial_state => {
2022-11-28 20:53:35 +08:00
2022-11-26 02:56:32 +08:00
state = COMMANDS.get_initial_state({
2022-10-25 02:29:59 +08:00
...initial_state,
on_async_call: (...args) => exec('on_async_call', ...args)
})
2022-11-28 22:02:37 +08:00
2022-09-10 02:48:13 +08:00
// Expose state for debugging
globalThis.__state = state
ui = new UI(container, state)
// Expose for debugging
globalThis.__ui = ui
2022-11-28 22:02:37 +08:00
2022-09-10 02:48:13 +08:00
render_initial_state(ui, state)
2022-11-28 22:02:37 +08:00
open_run_iframe(state)
// TODO exec on iframe load
exec('open_run_window')
2022-09-10 02:48:13 +08:00
})
}
export const get_state = () => state
export const exec = (cmd, ...args) => {
if(cmd == 'input' || cmd == 'write') {
// Do not print file to console
console.log('exec', cmd)
} else {
console.log('exec', cmd, ...args)
}
const comm = cmd.split('.').reduce(
(comm, segment) => comm?.[segment],
COMMANDS
)
if(comm == null) {
throw new Error('command ' + cmd + ' + not found')
}
const result = comm(state, ...args)
console.log('nextstate', result)
let nextstate, effects
if(result.state != null) {
({state: nextstate, effects} = result)
} else {
nextstate = result
effects = null
}
// Sanity check
2022-11-28 22:02:37 +08:00
if(state?.current_module == null) {
2022-09-10 02:48:13 +08:00
console.error('command did not return state, returned', result)
throw new Error('illegal state')
}
render_common_side_effects(state, nextstate, cmd, ui);
if(effects != null) {
(Array.isArray(effects) ? effects : [effects]).forEach(e => {
if(e.type == 'write' || e.type == 'save_to_localstorage') {
// do not spam to console
console.log('apply effect', e.type)
} else {
console.log('apply effect', e.type, ...(e.args ?? []))
}
EFFECTS[e.type](nextstate, e.args, ui)
})
}
// Expose for debugging
globalThis.__prev_state = state
globalThis.__state = nextstate
state = nextstate
}