-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,050 additions
and
190 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useContext, useState, useEffect } from "../imports/Preact.js" | ||
import { PlutoActionsContext } from "./PlutoContext.js" | ||
|
||
/** Request the current time from the server, compare with the local time, and return the current best estimate of our time difference. Updates regularly. | ||
* @param {{connected: boolean}} props | ||
*/ | ||
export const useMyClockIsAheadBy = ({ connected }) => { | ||
let pluto_actions = useContext(PlutoActionsContext) | ||
|
||
const [my_clock_is_ahead_by, set_my_clock_is_ahead_by] = useState(0) | ||
|
||
useEffect(() => { | ||
console.error({ connected }) | ||
if (connected) { | ||
let f = async () => { | ||
let getserver = () => pluto_actions.send("current_time").then((m) => m.message.time) | ||
let getlocal = () => Date.now() / 1000 | ||
|
||
// once to precompile and to make sure that the server is not busy with other tasks | ||
// console.log("getting server time warmup") | ||
for (let i = 0; i < 16; i++) await getserver() | ||
// console.log("getting server time warmup done") | ||
|
||
let t1 = await getlocal() | ||
let s1 = await getserver() | ||
let s2 = await getserver() | ||
let t2 = await getlocal() | ||
// console.log("getting server time done") | ||
|
||
let mytime = (t1 + t2) / 2 | ||
let servertime = (s1 + s2) / 2 | ||
|
||
let diff = mytime - servertime | ||
// console.info("My clock is ahead by ", diff, " s") | ||
if (!isNaN(diff)) set_my_clock_is_ahead_by(diff) | ||
} | ||
|
||
f() | ||
|
||
let handle = setInterval(f, 60 * 1000) | ||
|
||
return () => clearInterval(handle) | ||
} | ||
}, [connected]) | ||
|
||
return my_clock_is_ahead_by | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { cl } from "../common/ClassTable.js" | ||
import { html, useEffect, useRef, useState } from "../imports/Preact.js" | ||
|
||
export const DiscreteProgressBar = ({ total, done, busy }) => { | ||
total = Math.max(1, total) | ||
|
||
return html` | ||
<div | ||
class=${cl({ | ||
"discrete-progress-bar": true, | ||
"small": total < 8, | ||
"mid": total >= 8 && total < 48, | ||
"big": total >= 48, | ||
})} | ||
data-total=${total} | ||
> | ||
${[...Array(total)].map((_, i) => { | ||
return html`<div | ||
class=${cl({ | ||
done: i < done, | ||
busy: i >= done && i < done + busy, | ||
})} | ||
></div>` | ||
})} | ||
</div> | ||
` | ||
} | ||
|
||
export const DiscreteProgressBarTest = () => { | ||
const [done_total, set_done_total] = useState([0, 0, 0]) | ||
|
||
const done_total_ref = useRef(done_total) | ||
done_total_ref.current = done_total | ||
|
||
useEffect(() => { | ||
let handle = setInterval(() => { | ||
const [done, busy, total] = done_total_ref.current | ||
|
||
if (Math.random() < 0.3) { | ||
if (done < total) { | ||
if (Math.random() < 0.1) { | ||
set_done_total([done, 1, total + 5]) | ||
} else { | ||
set_done_total([done + 1, 1, total]) | ||
} | ||
} else { | ||
set_done_total([0, 1, Math.ceil(Math.random() * Math.random() * 100)]) | ||
} | ||
} | ||
}, 100) | ||
return () => clearInterval(handle) | ||
}, []) | ||
|
||
return html`<${DiscreteProgressBar} total=${done_total[2]} busy=${done_total[1]} done=${done_total[0]} />` | ||
} |
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
Oops, something went wrong.