From cc1cf3a647862d100685907c71f3ac16802d3708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Rainone?= <476650+arl@users.noreply.github.com> Date: Sun, 20 Feb 2022 13:58:26 +0100 Subject: [PATCH] Switch to es6 (#67) * internal/static: js cosmetics * internal/static: convert to ES6 modules * _example: ignore dev directory * _example: minor test log improvements * internal/static: in ui.js switch to const where possible * Update CHANGELOG.md --- CHANGELOG.md | 21 +- _example/examples_test.go | 6 +- internal/static/app.js | 105 ++++--- internal/static/buffer.js | 83 +++-- internal/static/index.html | 24 +- internal/static/stats.js | 286 +++++++++-------- internal/static/ui.js | 627 +++++++++++++++++-------------------- 7 files changed, 547 insertions(+), 605 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 833c5e99..b45a5669 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,21 +1,22 @@ Unreleased yet ============== - * Build and test all examples - * Polishing (README, small UI improvements) - * Assets are `go:embed`ed, so the minimum go version is now go1.16 + * Switch javascript code to ES6 (#65) + * Build and test all examples (#63) + * Assets are `go:embed`ed, so the minimum go version is now go1.16 (#55) + * Polishing (README, small UI improvements) (#54) * Small ui improvements: link to go.dev rather than golang.org v0.4.0 / 2021-05-08 ================== * Auto-reconnect to new server from GUI after closed websocket connection (#49) - * Reorganize examples - * Make `IndexAtRoot` returns an `http.HandlerFunc` instead of `http.Handler` + * Reorganize examples (#51) + * Make `IndexAtRoot` returns an `http.HandlerFunc` instead of `http.Handler` (#52) v0.3.0 / 2021-02-14 ================== - * Enable 'save as png' button on plots + * Enable 'save as png' button on plots (#44) v0.2.2 / 2020-12-13 ================== @@ -23,14 +24,14 @@ v0.2.2 / 2020-12-13 * Use Go Modules for 'github.com/gorilla/websocket' (#39) * Support custom frequency (#37) * Added fixed go-chi example (#38) - * _example: add echo (#22) - * _example: add example gin (#34) + * `_example`: add echo (#22) + * `_example`: add gin example (#34) * ci: track coverage * RegisterDefault returns an error now * Ensure send frequency is a strictly positive integer * Don't log if we can't upgrade to websocket - * _example: add chi router (#38) - * _example: change structure to have one example per directory + * `_example`_example: add chi router (#38) + * `_example`_example: change structure to have one example per directory v0.2.1 / 2020-10-29 =================== diff --git a/_example/examples_test.go b/_example/examples_test.go index 6ea62131..02082b21 100644 --- a/_example/examples_test.go +++ b/_example/examples_test.go @@ -51,7 +51,7 @@ func TestExamples(t *testing.T) { }, } for _, ent := range ents { - if !ent.IsDir() { + if !ent.IsDir() || ent.Name() == "dev" { continue } t.Run(ent.Name(), func(t *testing.T) { @@ -150,12 +150,12 @@ func gorun() (stop func() error, err error) { if outb.Len() > 0 { out = outb.String() } - fmt.Printf("go run, output:\n%s\n", out) + fmt.Printf("command output:\n%s\n", out) } }() if err := <-errc; err != nil { - return nil, fmt.Errorf("go run: %v", err) + return nil, fmt.Errorf("command error: %v", err) } return cmd.Process.Kill, nil diff --git a/internal/static/app.js b/internal/static/app.js index 4cebcf60..9ecb55e2 100644 --- a/internal/static/app.js +++ b/internal/static/app.js @@ -1,66 +1,67 @@ -(function() { - function $(id) { - return document.getElementById(id); - } - - function buildWebsocketURI() { - var loc = window.location, - ws_prot = "ws:"; - if (loc.protocol === "https:") { - ws_prot = "wss:"; - } - return ws_prot + "//" + loc.host + loc.pathname + "ws" - } +import * as stats from './stats.js'; +import * as ui from './ui.js'; - const dataRetentionSeconds = 60; - var timeout = 250; +const $ = id => { + return document.getElementById(id); +} - function clamp(val, min, max) { - if (val < min) return min; - if (val > max) return max; - return val; +const buildWebsocketURI = () => { + var loc = window.location, + ws_prot = "ws:"; + if (loc.protocol === "https:") { + ws_prot = "wss:"; } + return ws_prot + "//" + loc.host + loc.pathname + "ws" +} - /* WebSocket connection handling */ +const dataRetentionSeconds = 60; +var timeout = 250; - function connect() { - let ws = new WebSocket(buildWebsocketURI()); - console.log("Attempting websocket connection to statsviz server..."); +const clamp = (val, min, max) => { + if (val < min) return min; + if (val > max) return max; + return val; +} - ws.onopen = () => { - console.log("Successfully connected"); - timeout = 250; // reset connection timeout for next time - }; +/* WebSocket connection handling */ - ws.onclose = event => { - console.log("Closed websocket connection: ", event); - setTimeout(connect, clamp(timeout += timeout, 250, 5000)); - }; +const connect = () => { + let ws = new WebSocket(buildWebsocketURI()); + console.log("Attempting websocket connection to statsviz server..."); - ws.onerror = error => { - console.log("Websocket error: ", error); - ws.close(); - }; + ws.onopen = () => { + console.log("Successfully connected"); + timeout = 250; // reset connection timeout for next time + }; - var initDone = false; - ws.onmessage = event => { - let allStats = JSON.parse(event.data) - if (!initDone) { - stats.init(dataRetentionSeconds, allStats); - stats.pushData(new Date(), allStats); - initDone = true; - let data = stats.slice(dataRetentionSeconds); - ui.createPlots(data); - return; - } + ws.onclose = event => { + console.log("Closed websocket connection: ", event); + setTimeout(connect, clamp(timeout += timeout, 250, 5000)); + }; + ws.onerror = error => { + console.log("Websocket error: ", error); + ws.close(); + }; + + var initDone = false; + ws.onmessage = event => { + let allStats = JSON.parse(event.data) + if (!initDone) { + stats.init(dataRetentionSeconds, allStats); stats.pushData(new Date(), allStats); - if (ui.isPaused()) { - return - } + initDone = true; let data = stats.slice(dataRetentionSeconds); - ui.updatePlots(data); + ui.createPlots(data); + return; + } + + stats.pushData(new Date(), allStats); + if (ui.isPaused()) { + return } + let data = stats.slice(dataRetentionSeconds); + ui.updatePlots(data); } - connect(); -}()); \ No newline at end of file +} +connect(); \ No newline at end of file diff --git a/internal/static/buffer.js b/internal/static/buffer.js index c4fe7ec7..90c5b788 100644 --- a/internal/static/buffer.js +++ b/internal/static/buffer.js @@ -1,53 +1,46 @@ // Buffer declares the Buffer class. -var Buffer = (function() { - class Buffer { - constructor(len, cap) { - if (cap - len < 0) { - console.Error("cap - len must be positive"); - } - - // TODO(arl): value using TypedArray rather than Array here - this._buf = new Array(cap); - this._pos = 0; - this._len = len; - this._cap = cap; +export default class Buffer { + constructor(len, cap) { + if (cap - len < 0) { + console.Error("cap - len must be positive"); } - last() { - if (this.length() == 0) { - throw 'Cannot call last() on an empty Buffer'; - } - return this._buf[this._pos]; + // TODO(arl): value using TypedArray rather than Array here + this._buf = new Array(cap); + this._pos = 0; + this._len = len; + this._cap = cap; + } + last() { + if (this.length() == 0) { + throw 'Cannot call last() on an empty Buffer'; } - push(pt) { - if (this._pos >= this._cap) { - // move data to the beggining of the buffer, effectively discarding - // the cap-len oldest elements - this._buf.copyWithin(0, this._cap - this._len); - this._pos = this._len; - } - - this._buf[this._pos] = pt; - this._pos++; + return this._buf[this._pos]; + } + push(pt) { + if (this._pos >= this._cap) { + // move data to the beggining of the buffer, effectively discarding + // the cap-len oldest elements + this._buf.copyWithin(0, this._cap - this._len); + this._pos = this._len; } - length() { - if (this._pos > this._len) { - return this._len; - } - - return this._pos; - } - // slice returns a slice of the len latest datapoints present in the buffer. - slice(len) { - // Cap the dimension of the returned slice to the data available - if (len > this.length()) { - len = this.length(); - } - - let start = this._pos - len; - return this._buf.slice(start, start + len); + this._buf[this._pos] = pt; + this._pos++; + } + length() { + if (this._pos > this._len) { + return this._len; } + return this._pos; } + // slice returns a slice of the len latest datapoints present in the buffer. + slice(len) { + // Cap the dimension of the returned slice to the data available + if (len > this.length()) { + len = this.length(); + } - return Buffer; -}()); \ No newline at end of file + let start = this._pos - len; + return this._buf.slice(start, start + len); + } +}; \ No newline at end of file diff --git a/internal/static/index.html b/internal/static/index.html index 6172b517..a520c8ef 100644 --- a/internal/static/index.html +++ b/internal/static/index.html @@ -33,22 +33,19 @@