Finally figure out how to increment this lol

Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
Ayase Minori 2023-09-12 06:38:05 +00:00 committed by GitHub
parent 4a01821907
commit 1a42b2ccf0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 16 deletions

View file

@ -10,7 +10,7 @@ interface SharedProps {
export default function Counter(props: SharedProps) {
const [count, setCount] = useState(0);
const [globalCount, setGlobalCount] = useState(props.globalCount);
const [globalCount, setGlobalCount] = useState(props.globalCount ?? 0);
const [internalCount, setInternalCount] = useState(0);
const onClick = () => {

View file

@ -15,9 +15,9 @@ export const handler: Handlers = {
const bc = new BroadcastChannel("global-count");
const body = new ReadableStream({
start(controller) {
bc.addEventListener("message", () => {
bc.addEventListener("message", async () => {
try {
const data = getGlobalStatistics();
const data = await getGlobalStatistics();
const chunk = `data: ${JSON.stringify({globalCount: data})}\n\n`;
controller.enqueue(new TextEncoder().encode(chunk));
} catch (e) {
@ -43,7 +43,7 @@ export const handler: Handlers = {
},
POST: async (req, ctx) => {
const body = await req.json();
setGlobalStatistics(body.data);
await setGlobalStatistics(body.data);
const bc = new BroadcastChannel("global-count");
bc.postMessage(new TextEncoder().encode(getGlobalStatistics().toString()))

View file

@ -1,17 +1,11 @@
const kv = await Deno.openKv();
export function getGlobalStatistics(): number {
let res = 0;
kv.get<number>(["global-statistics"]).then(v => {
res = v.value as number;
});
return res;
export async function getGlobalStatistics() {
const res = await kv.get<number>(["global-statistics"]) ?? 0;
return res.value ?? 0;
}
export function setGlobalStatistics(value: number) {
const pv = getGlobalStatistics();
kv.set(["global-statistics"], pv + value).then(() => {
return;
})
export async function setGlobalStatistics(value: number) {
const pv = await getGlobalStatistics();
await kv.set(["global-statistics"], pv + value);
}