-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(go): routes * refactor(ui): App * feat(ui): counter * feat(ui): better websocket reconnect. ping * refactor(ui): rename * fix(ui): ws mining log * refactor(ui): ticker * fix(ui): page mining clear all logs in destory hook * chore(ui): clean code * feat(go): ws ping * refactor(ui): use/websocket private vars * chore(go): clean println * fix(ui): typo * fix(ui): stop ticker
- Loading branch information
Showing
7 changed files
with
140 additions
and
34 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
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,36 +1,49 @@ | ||
import { log } from '../util/log' | ||
|
||
/** @type {WebSocket} */ | ||
let ws | ||
|
||
export function useWebsocket(url, protocol) { | ||
/** @type {WebSocket} */ | ||
let ws | ||
|
||
const onMessage = [] | ||
|
||
ws = new WebSocket(url, protocol) | ||
connect(url, protocol) | ||
|
||
ws.onmessage = (event) => { | ||
invokeAll(onMessage, event.data) | ||
} | ||
ws.onopen = () => { | ||
log('ws', 'opened.') | ||
function invokeAll(fns, data) { | ||
if (!ws) { | ||
log('ws', 'ws is not available') | ||
return | ||
} | ||
|
||
fns.forEach((fn) => fn(data)) | ||
} | ||
ws.onclose = () => { | ||
log('ws', 'closed.') | ||
ws = null | ||
|
||
function connect(url, protocol) { | ||
ws = new WebSocket(url, protocol) | ||
|
||
ws.onmessage = (event) => { | ||
invokeAll(onMessage, event.data) | ||
} | ||
ws.onopen = () => { | ||
log('ws', 'opened.') | ||
} | ||
ws.onclose = () => { | ||
log('ws', 'closed.') | ||
ws = null | ||
} | ||
} | ||
|
||
return { | ||
message(fn) { | ||
onMessage.push(fn) | ||
}, | ||
} | ||
} | ||
|
||
function invokeAll(fns, data) { | ||
if (!ws) { | ||
log('ws', 'ws is not available') | ||
return | ||
ping: () => { | ||
if (!ws) return | ||
ws.send('ping') | ||
}, | ||
reconnect: () => { | ||
if (ws) return | ||
connect(url, protocol) | ||
}, | ||
} | ||
|
||
fns.forEach((fn) => fn(data)) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export function ticker(second, cb) { | ||
let ticker | ||
let count = 0 | ||
const resetCount = () => { | ||
count = 0 | ||
} | ||
|
||
startTicker() | ||
|
||
function startTicker() { | ||
if (ticker) return | ||
|
||
ticker = setInterval(() => { | ||
if (count >= second) { | ||
cb() | ||
resetCount() | ||
} | ||
|
||
count++ | ||
}, 1000) | ||
} | ||
|
||
return { | ||
resetCount, | ||
startTicker, | ||
stopTicker: () => { | ||
ticker = clearInterval(ticker) | ||
}, | ||
} | ||
} |