From 289d4491b31763087de0788555836f9285588c3d Mon Sep 17 00:00:00 2001 From: Steve Phillips Date: Thu, 15 Apr 2021 23:32:03 -0700 Subject: [PATCH 1/6] index.html: Typo fix --- index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 964bf62..fe77d4a 100644 --- a/index.html +++ b/index.html @@ -16,9 +16,9 @@ // const init = JSON.stringify({event: 'Init'}) webSocket.send('init'); }; - webSocket.onmessage = function (resopnse) { + webSocket.onmessage = function (response) { const node = document.getElementById('app') - node.innerHTML = resopnse.data + node.innerHTML = response.data node.querySelectorAll('[v-click]').forEach(clickable => { clickable.addEventListener("click", click => { const event = click.target.attributes['v-click'].value From 14f1ee6979d22f6ac2bf2774758d14291e07bfe5 Mon Sep 17 00:00:00 2001 From: Steve Phillips Date: Thu, 15 Apr 2021 23:32:57 -0700 Subject: [PATCH 2/6] live.v: Convert functions to methods (start_server, view, update) --- live.v | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/live.v b/live.v index 32f3431..34a719c 100644 --- a/live.v +++ b/live.v @@ -24,7 +24,7 @@ pub fn (mut app App) init() { } pub fn (mut app App) init_once() { - go start_server(mut app) + go app.start_server() } struct Model { @@ -32,7 +32,7 @@ mut: count int } -fn start_server(mut app App) ? { +fn (mut app App) start_server() ? { mut s := websocket.new_server(30000, '') s.ping_interval = 100 s.on_connect(fn (mut s websocket.ServerClient) ?bool { @@ -46,9 +46,9 @@ fn start_server(mut app App) ? { s.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, mut model Model) ? { event := msg.payload.bytestr() - update(event, mut model) + model.update(event) - rendered := view(model) + rendered := model.view() ws.write(rendered.bytes(), msg.opcode) or { panic(err) } }, app.model) @@ -60,7 +60,7 @@ fn start_server(mut app App) ? { s.listen() or {} } -fn update(event string, mut model Model) { +fn (mut model Model) update(event string) { match event { 'init' { model.count = 0 @@ -75,7 +75,7 @@ fn update(event string, mut model Model) { } } -fn view(model Model) string { +fn (model Model) view() string { return '
$model.count
From 685c54e24058f4377c0889b68e2358648de890d2 Mon Sep 17 00:00:00 2001 From: Steve Phillips Date: Thu, 15 Apr 2021 23:33:28 -0700 Subject: [PATCH 3/6] live.v: Use ++/-- to increment/decrement --- live.v | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/live.v b/live.v index 34a719c..f039718 100644 --- a/live.v +++ b/live.v @@ -66,10 +66,10 @@ fn (mut model Model) update(event string) { model.count = 0 } 'inc' { - model.count = model.count + 1 + model.count++ } 'dec' { - model.count = model.count - 1 + model.count-- } else {} } From c599ebb72f7740e6c9deed068f3da366ad32cf47 Mon Sep 17 00:00:00 2001 From: Steve Phillips Date: Thu, 15 Apr 2021 23:42:58 -0700 Subject: [PATCH 4/6] live.v: Simpler way to initialize Model.count = 3 --- live.v | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/live.v b/live.v index f039718..7470436 100644 --- a/live.v +++ b/live.v @@ -17,19 +17,13 @@ pub fn (mut app App) index() vweb.Result { return $vweb.html() } -pub fn (mut app App) init() { - app.model = &Model{ - count: 3 - } -} - pub fn (mut app App) init_once() { go app.start_server() } struct Model { mut: - count int + count int = 3 } fn (mut app App) start_server() ? { From b7aaafec9e67077f283c9ad7397515ec4f69e234 Mon Sep 17 00:00:00 2001 From: Steve Phillips Date: Thu, 15 Apr 2021 23:45:01 -0700 Subject: [PATCH 5/6] live.v: Another way to essentially initialize Model.count = 3 --- live.v | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/live.v b/live.v index 7470436..f6d9831 100644 --- a/live.v +++ b/live.v @@ -23,7 +23,7 @@ pub fn (mut app App) init_once() { struct Model { mut: - count int = 3 + count int } fn (mut app App) start_server() ? { @@ -57,7 +57,7 @@ fn (mut app App) start_server() ? { fn (mut model Model) update(event string) { match event { 'init' { - model.count = 0 + model.count = 3 } 'inc' { model.count++ From d7c4157adb72db061ec290df32c4026066ff7610 Mon Sep 17 00:00:00 2001 From: Steve Phillips Date: Thu, 15 Apr 2021 23:48:11 -0700 Subject: [PATCH 6/6] live.v: Added reset that sets Model.count = 0 --- live.v | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/live.v b/live.v index f6d9831..1ac301c 100644 --- a/live.v +++ b/live.v @@ -59,6 +59,9 @@ fn (mut model Model) update(event string) { 'init' { model.count = 3 } + 'reset' { + model.count = 0 + } 'inc' { model.count++ } @@ -74,5 +77,6 @@ fn (model Model) view() string {
$model.count
+ ' }