Skip to content

Commit

Permalink
implement a more efficient resumable framework
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowdHailer committed Oct 6, 2023
1 parent 8c9da37 commit 819ef22
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 52 deletions.
14 changes: 7 additions & 7 deletions eyg/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_bitwise", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "6064699EFBABB1CA392DCB193D0E8B402FB042B4B46857B01E6875E643B57F54" },
{ name = "gleam_fetch", version = "0.2.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_javascript"], otp_app = "gleam_fetch", source = "hex", outer_checksum = "D0C9E9CAE8D6EFCCC3A9FF817DCA9ED327097222086D91DE4F6CA8FBAB02D79F" },
{ name = "gleam_bitwise", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "E2A46EE42E5E9110DAD67E0F71E7358CBE54D5EC22C526DD48CBBA3223025792" },
{ name = "gleam_fetch", version = "0.2.1", build_tools = ["gleam"], requirements = ["gleam_javascript", "gleam_http", "gleam_stdlib"], otp_app = "gleam_fetch", source = "hex", outer_checksum = "F64E93C754D948B2D37ABC4ADD5482FE0FAED4B99C79E66012DDE96BEDC40544" },
{ name = "gleam_http", version = "3.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "FAE9AE3EB1CA90C2194615D20FFFD1E28B630E84DACA670B28D959B37BCBB02C" },
{ name = "gleam_javascript", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "EC5C683E144B407B3DC948197FD22D2D8E8D92D32E57332F8041A178E2D73710" },
{ name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["thoas", "gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" },
{ name = "gleam_stdlib", version = "0.30.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "704258528887F95075FFED7AAE1CCF836A9B88E3AADA2F69F9DA15815F94A4F9" },
{ name = "gleam_javascript", version = "0.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "BFEBB63ABE4A1694E07DEFD19B160C2980304B5D775A89D4B02E7DE7C9D8008B" },
{ name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" },
{ name = "gleam_stdlib", version = "0.31.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6D1BC5B4D4179B9FEE866B1E69FE180AC2CE485AD90047C0B32B2CA984052736" },
{ name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" },
{ name = "lustre", version = "3.0.3", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "lustre", source = "hex", outer_checksum = "3FE126D993DC4B5E779E75AA1D8C80A9F5EC5C5E5A4F859D5B3BAAD61CAE5557" },
{ name = "plinth", version = "0.1.3", build_tools = ["gleam"], requirements = ["gleam_javascript", "gleam_stdlib"], source = "local", path = "/Users/petersaxton/src/github.com/crowdhailer/plinth" },
{ name = "lustre", version = "3.0.6", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "lustre", source = "hex", outer_checksum = "2D2BEF77B5966773467574C2DC23A27FAB7C720DEF428E72C610DA1547E7E171" },
{ name = "plinth", version = "0.1.3", build_tools = ["gleam"], requirements = ["gleam_javascript", "gleam_stdlib"], source = "local", path = "/opt/plinth" },
{ name = "simplifile", version = "0.1.14", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "10EA0207796F20488A3A166C50A189C9385333F3C9FAC187729DE7B9CE4ADDBC" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
]
Expand Down
2 changes: 1 addition & 1 deletion eyg/saved/saved.json

Large diffs are not rendered by default.

173 changes: 129 additions & 44 deletions eyg/src/platforms/resume.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import gleam/map
import gleam/io
import gleam/list
import gleam/javascript/map
import gleam/option.{None, Some}
import gleam/result
import gleam/string
import gleam/javascript
import gleam/javascript/array
import plinth/browser/document
import plinth/browser/element
import plinth/browser/event
Expand All @@ -9,51 +14,131 @@ import eygir/decode
import eyg/runtime/interpreter as r
import harness/stdlib

pub fn run() {
// console.log(document.document())
case document.query_selector("script[type=\"application/eygir.json\"]") {
Ok(script) -> {
// console.log(script)
let assert Ok(source) =
decode.from_json(string.replace(element.inner_text(script), "\\/", "/"))
// console.log(source)
// env needs builtins
let env = stdlib.env()
let rev = []
let assert #(c, env) = r.resumable(source, env, None)
console.log(#("c", c))
document.add_event_listener(
"click",
fn(event) {
case element.closest(event.target(event), "*[on\\:click]") {
Ok(target) ->
case element.closest(target, "[r\\:container]") {
Ok(container) -> {
let k = Some(r.Kont(r.CallWith(r.Binary("0"), [], env), None))
let #(answer, _) = r.loop_till(r.V(c), rev, env, k)
// console.log(answer)
let assert r.Value(term) = answer
// console.log(r.to_string(term))
case term {
r.Tagged("Ok", r.Binary(content)) ->
element.set_inner_html(container, content)
_ -> {
console.log("bad stuff")
Nil
}
}
Nil
}

Error(Nil) -> Nil
}
Error(Nil) -> Nil
}
},
)
fn handle_click(event, states) {
use target <- result.then(element.closest(
event.target(event),
"*[on\\:click]",
))
use container <- result.then(element.closest(target, "[r\\:container]"))
use key <- result.then(element.get_attribute(target, "on:click"))
use #(action, env) <- result.then(map.get(states, container))
// TODO get attribute and multiple sources
let k = Some(r.Kont(r.CallWith(r.Binary(key), [], env), None))
let rev = []
let #(answer, env) = r.loop_till(r.V(action), rev, env, k)
// console.log(answer)
let assert r.Value(term) = answer
// console.log(r.to_string(term))
case term {
r.Tagged("Ok", return) -> {
// console.log(r.to_string(return))
let assert Ok(r.Binary(content)) = r.field(return, "content")
let assert Ok(action) = r.field(return, "action")
element.set_inner_html(container, content)
// Need native map because js objects are deep equal true
Ok(map.set(states, container, #(r.Value(action), env)))
}
_ -> {
console.log("bad stuff")
Ok(states)
}
Error(Nil) -> Nil
}
}

pub fn run() {
let scripts =
document.query_selector_all("script[type=\"application/eygir.json\"]")
|> array.to_list()
let states =
list.filter_map(
scripts,
fn(script) {
use container <- result.then(element.closest(script, "[r\\:container]"))
use source <- result.then(
decode.from_json(string.replace(
element.inner_text(script),
"\\/",
"/",
))
|> result.map_error(fn(_) { Nil }),
)
let env = stdlib.env()
let rev = []
let #(action, env) = r.resumable(source, env, None)
Ok(#(container, #(action, env)))
},
)
|> list.fold(
map.new(),
fn(map, item) {
let #(key, value) = item
map.set(map, key, value)
},
)
states
|> console.log

let ref = javascript.make_reference(states)
document.add_event_listener(
"click",
fn(event) {
let states = javascript.dereference(ref)
let assert Ok(states) = handle_click(event, states)
javascript.set_reference(ref, states)
Nil
},
)
// case {
// Ok(script) -> {
// // console.log(script)
// let assert Ok(source) =
// decode.from_json(string.replace(element.inner_text(script), "\\/", "/"))
// // console.log(source)
// // env needs builtins
// let env = stdlib.env()
// let rev = []
// let assert #(action, env) = r.resumable(source, env, None)
// let ref = javascript.make_reference(action)

// document.add_event_listener(
// "click",
// fn(event) {
// case element.closest(event.target(event), "*[on\\:click]") {
// Ok(target) ->
// case element.closest(target, "[r\\:container]") {
// Ok(container) -> {
// let k = Some(r.Kont(r.CallWith(r.Binary("0"), [], env), None))
// let c = javascript.dereference(ref)
// let #(answer, _) = r.loop_till(r.V(c), rev, env, k)
// // console.log(answer)
// let assert r.Value(term) = answer
// // console.log(r.to_string(term))
// case term {
// r.Tagged("Ok", return) -> {
// // console.log(r.to_string(return))
// let assert Ok(r.Binary(content)) =
// r.field(return, "content")
// let assert Ok(action) = r.field(return, "action")
// javascript.set_reference(ref, r.Value(action))
// element.set_inner_html(container, content)
// }
// _ -> {
// console.log("bad stuff")
// Nil
// }
// }
// Nil
// }

// Error(Nil) -> Nil
// }
// Error(Nil) -> Nil
// }
// },
// )
// }
// Error(Nil) -> Nil
// }
// document.add_event_listener(
// "click",
// fn(event) {
Expand Down
1 change: 1 addition & 0 deletions tmp.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let stop (serve 5000 (multi_tenent [{key "localhost:5000" value projects.laura}]


let stop (serve 5000 ctrl.serve)
let stop (serve_page 5000 projects.counters)

## DB AST

Expand Down

0 comments on commit 819ef22

Please sign in to comment.