Implement POSTing

Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
Ayase Minori 2023-09-11 03:55:06 +00:00 committed by GitHub
parent 237c12fb63
commit 77781faca9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 9 deletions

View file

@ -35,7 +35,8 @@
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.2.3", "@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",
"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": { "compilerOptions": {
"jsx": "react-jsx", "jsx": "react-jsx",

View file

@ -1,6 +1,7 @@
import type { Signal } from "@preact/signals"; import type { Signal } from "@preact/signals";
import { Button } from "../components/Button.tsx"; import { Button } from "../components/Button.tsx";
import { useState, useEffect } from "preact/hooks"; import { useState, useEffect } from "preact/hooks";
import axios from 'axios-web';
interface SharedProps { interface SharedProps {
hasClicked: Signal<boolean>; hasClicked: Signal<boolean>;
@ -8,20 +9,26 @@ interface SharedProps {
} }
export default function Counter(props: SharedProps) { export default function Counter(props: SharedProps) {
const count = useState(0); const [count, setCount] = useState(0);
const onClick = () => { const onClick = () => {
let internalCount = 0
let timer: number;
// set a timer to update the global count, resetting // set a timer to update the global count, resetting
// whenever a user activity is detected // whenever a user activity is detected
let timer: number; internalCount += 1;
timer = setTimeout(async () => { setCount(count + 1);
// TODO: add the upload code here
timer = setTimeout(() => {
axios.post(window.location.href, JSON.stringify({data: internalCount}));
internalCount = 0;
}, 5000); }, 5000);
window.onclick = () => { window.onclick = () => {
clearTimeout(timer); clearTimeout(timer);
timer = setTimeout(async () => { timer = setTimeout(() => {
// Upload code goes here axios.post(window.location.href, JSON.stringify({data: internalCount}));
internalCount = 0;
}, 5000); }, 5000);
} }
} }
@ -34,6 +41,8 @@ export default function Counter(props: SharedProps) {
}) })
es.addEventListener("message", (e) => { es.addEventListener("message", (e) => {
console.log(`[${new Date()}] Received global count: ${e.data}`);
props.globalCount = e.data;
}); });
// TODO: Reconnect backoff logic could be improved // TODO: Reconnect backoff logic could be improved

View file

@ -13,8 +13,7 @@ export const handler: Handlers = {
start(controller) { start(controller) {
bc.addEventListener("message", () => { bc.addEventListener("message", () => {
try { try {
const data = getGlobalStatistics(); controller.enqueue(getGlobalStatistics.toString());
controller.enqueue(`${data}`);
} catch (e) { } catch (e) {
console.error(`[${new Date()}] Error while getting global statistics: ${e}`); console.error(`[${new Date()}] Error while getting global statistics: ${e}`);
} }
@ -38,6 +37,13 @@ export const handler: Handlers = {
}, },
POST: async (req, ctx) => { POST: async (req, ctx) => {
const body = await req.json(); 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 })
} }
} }