-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Respo/app
trying with new app definition
- Loading branch information
Showing
4 changed files
with
57 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,42 @@ | ||
let app_store_key : String = "mbt-workflow" | ||
|
||
struct App { | ||
store : Ref[Store] | ||
mount_target : @dom_ffi.Node | ||
} | ||
|
||
fn App::get_store(self : App) -> Ref[Store] { | ||
self.store | ||
} | ||
|
||
fn App::get_mount_target(self : App) -> @dom_ffi.Node { | ||
self.mount_target | ||
} | ||
|
||
fn App::dispatch(self : App, op : ActionOp) -> Unit { | ||
// TODO intent | ||
@dom_ffi.log("Action: " + op.to_string()) | ||
self.store.val.update(op) | ||
} | ||
|
||
fn App::view( | ||
self : App | ||
fn view( | ||
store : Store | ||
) -> @respo_node.RespoNode[ActionOp]!@respo_node.RespoCommonError { | ||
if false { | ||
raise @respo_node.RespoCommonError("TODO") | ||
} | ||
let store = self.store.val | ||
// @dom_ffi.log("Store to render: " + store.to_json().stringify(indent=2)) | ||
let states = store.get_states() | ||
@respo_node.div( | ||
div( | ||
class_name=@respo.ui_global, | ||
style=@respo_node.RespoStyle::new(padding=Px(12)), | ||
style=respo_style(padding=Px(12)), | ||
[comp_counter(states.pick("counter"), store.counted)], | ||
) | ||
} | ||
|
||
fn pick_storage_key() -> String { | ||
app_store_key | ||
} | ||
|
||
fn try_load_storage(self : App) -> Unit!@respo_node.RespoCommonError { | ||
let window = @dom_ffi.window() | ||
let storage = window.local_storage() | ||
let key = pick_storage_key() | ||
match storage.get_item(key) { | ||
Some(s) => { | ||
let store = self.get_store() | ||
store.val = try_from_string!(s) | ||
} | ||
None => raise @respo_node.RespoCommonError("No storage") | ||
} | ||
} | ||
|
||
/// backup store to local storage before unload | ||
|
||
fn backup_model_beforeunload(self : App) -> Unit { | ||
let window = @dom_ffi.window() | ||
let storage = window.local_storage() | ||
let p = pick_storage_key() | ||
let store = self.get_store().val | ||
let beforeunload = fn(_e : @dom_ffi.BeforeUnloadEvent) { | ||
let content = store.to_json().stringify(indent=2) | ||
// util::log!("before unload {} {}", p, content); | ||
storage.set_item(p, content) | ||
} | ||
window.set_onbeforeunload(beforeunload) | ||
} | ||
|
||
fn main { | ||
let app : App = { | ||
mount_target: @dom_ffi.window() | ||
let window = @dom_ffi.window() | ||
let mount_target = window | ||
.document() | ||
.query_selector(".app") | ||
.reinterpret_as_node(), | ||
store: Ref::new(Store::default()), | ||
} | ||
let ret = app.try_load_storage?() | ||
match ret { | ||
Ok(_) => () | ||
Err(e) => @dom_ffi.error_log(e.to_string()) | ||
.reinterpret_as_node() | ||
let app : @respo.RespoApp[Store] = { | ||
store: Ref::new(@respo.try_load_storage(app_store_key)), | ||
mount_target, | ||
storage_key: app_store_key, | ||
} | ||
app.backup_model_beforeunload() | ||
@dom_ffi.log("store: " + app.store.val.to_json().stringify(indent=2)) | ||
app.render_loop() | ||
} | ||
|
||
fn App::render_loop(self : App) -> Unit { | ||
let mount_target = self.mount_target | ||
// let store_to_action = global_store.to_owned() | ||
|
||
let dispatch_action = fn(op : ActionOp) -> Unit!@respo_node.RespoCommonError { | ||
// @dom_ffi.log("Action: \{op.to_string()}") | ||
// @dom_ffi.log("Global store: \{global_store.val.to_json()}") | ||
|
||
// Self::dispatch(store_to_action.to_owned(), op)?; | ||
self.store.val.update(op) | ||
match op { | ||
Noop => raise @respo_node.RespoCommonError("TODO") | ||
_ => () | ||
} | ||
} | ||
let renderer = fn() -> @respo_node.RespoNode[_]!@respo_node.RespoCommonError { | ||
self.view!() | ||
} | ||
let ret = @respo.render_node?( | ||
mount_target, | ||
fn() { self.store }, | ||
renderer, | ||
dispatch_action, | ||
Some(100), | ||
// @dom_ffi.log("store: " + app.store.val.to_json().stringify(indent=2)) | ||
app.render_loop( | ||
fn() { view!(app.store.val) }, | ||
fn(op) { | ||
@dom_ffi.log("Action: " + op.to_string()) | ||
app.store.val.update(op) | ||
}, | ||
) | ||
match ret { | ||
Ok(_) => () | ||
Err(e) => @dom_ffi.error_log(e.to_string()) | ||
} | ||
let dev_mode = @dom_ffi.get_url_search_params( | ||
window.get_location().get_search(), | ||
).get("mode") | ||
@dom_ffi.log("dev mode: " + dev_mode.to_string()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
{ | ||
"is-main": true, | ||
"import": [ | ||
{ "path": "tiye/respo/lib", "alias": "respo" }, | ||
{ "path": "tiye/respo/lib/node", "alias": "respo_node" }, | ||
{ | ||
"path": "tiye/respo/lib", | ||
"alias": "respo", | ||
"value": ["ui_global", "ui_button"] | ||
}, | ||
{ | ||
"path": "tiye/respo/lib/node", | ||
"alias": "respo_node", | ||
"value": [ | ||
"div", | ||
"button", | ||
"span", | ||
"respo_attrs", | ||
"respo_style", | ||
"declare_static_style" | ||
] | ||
}, | ||
{ "path": "tiye/dom-ffi/lib", "alias": "dom_ffi" } | ||
] | ||
} |