mirror of
https://github.com/sr229/kuru-kuru.git
synced 2024-09-20 04:10:33 +00:00
Implement POSTing
Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
parent
237c12fb63
commit
77781faca9
3 changed files with 25 additions and 9 deletions
|
@ -35,7 +35,8 @@
|
|||
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.2.3",
|
||||
"twind": "https://esm.sh/twind@0.16.19",
|
||||
"twind/": "https://esm.sh/twind@0.16.19/",
|
||||
"$std/": "https://deno.land/std@0.193.0/"
|
||||
"$std/": "https://deno.land/std@0.193.0/",
|
||||
"axios-web": "https://esm.sh/axios@1.3.2?target=es2022"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import type { Signal } from "@preact/signals";
|
||||
import { Button } from "../components/Button.tsx";
|
||||
import { useState, useEffect } from "preact/hooks";
|
||||
import axios from 'axios-web';
|
||||
|
||||
interface SharedProps {
|
||||
hasClicked: Signal<boolean>;
|
||||
|
@ -8,20 +9,26 @@ interface SharedProps {
|
|||
}
|
||||
|
||||
export default function Counter(props: SharedProps) {
|
||||
const count = useState(0);
|
||||
const [count, setCount] = useState(0);
|
||||
const onClick = () => {
|
||||
let internalCount = 0
|
||||
let timer: number;
|
||||
|
||||
// set a timer to update the global count, resetting
|
||||
// whenever a user activity is detected
|
||||
let timer: number;
|
||||
timer = setTimeout(async () => {
|
||||
// TODO: add the upload code here
|
||||
internalCount += 1;
|
||||
setCount(count + 1);
|
||||
|
||||
timer = setTimeout(() => {
|
||||
axios.post(window.location.href, JSON.stringify({data: internalCount}));
|
||||
internalCount = 0;
|
||||
}, 5000);
|
||||
|
||||
window.onclick = () => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(async () => {
|
||||
// Upload code goes here
|
||||
timer = setTimeout(() => {
|
||||
axios.post(window.location.href, JSON.stringify({data: internalCount}));
|
||||
internalCount = 0;
|
||||
}, 5000);
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +41,8 @@ export default function Counter(props: SharedProps) {
|
|||
})
|
||||
|
||||
es.addEventListener("message", (e) => {
|
||||
console.log(`[${new Date()}] Received global count: ${e.data}`);
|
||||
props.globalCount = e.data;
|
||||
});
|
||||
|
||||
// TODO: Reconnect backoff logic could be improved
|
||||
|
|
|
@ -13,8 +13,7 @@ export const handler: Handlers = {
|
|||
start(controller) {
|
||||
bc.addEventListener("message", () => {
|
||||
try {
|
||||
const data = getGlobalStatistics();
|
||||
controller.enqueue(`${data}`);
|
||||
controller.enqueue(getGlobalStatistics.toString());
|
||||
} catch (e) {
|
||||
console.error(`[${new Date()}] Error while getting global statistics: ${e}`);
|
||||
}
|
||||
|
@ -38,6 +37,13 @@ export const handler: Handlers = {
|
|||
},
|
||||
POST: async (req, ctx) => {
|
||||
const body = await req.json();
|
||||
setGlobalStatistics(body.data);
|
||||
|
||||
// broadcast new value to everyone
|
||||
const bc = new BroadcastChannel("global-count");
|
||||
bc.postMessage(getGlobalStatistics().toString());
|
||||
|
||||
return Response.json({ success: true })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue