mirror of
https://github.com/sr229/kuru-kuru.git
synced 2024-09-20 04:10:33 +00:00
Finally figure out how to increment this lol
Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
parent
4a01821907
commit
1a42b2ccf0
3 changed files with 10 additions and 16 deletions
|
@ -10,7 +10,7 @@ interface SharedProps {
|
||||||
|
|
||||||
export default function Counter(props: SharedProps) {
|
export default function Counter(props: SharedProps) {
|
||||||
const [count, setCount] = useState(0);
|
const [count, setCount] = useState(0);
|
||||||
const [globalCount, setGlobalCount] = useState(props.globalCount);
|
const [globalCount, setGlobalCount] = useState(props.globalCount ?? 0);
|
||||||
const [internalCount, setInternalCount] = useState(0);
|
const [internalCount, setInternalCount] = useState(0);
|
||||||
|
|
||||||
const onClick = () => {
|
const onClick = () => {
|
||||||
|
|
|
@ -15,9 +15,9 @@ export const handler: Handlers = {
|
||||||
const bc = new BroadcastChannel("global-count");
|
const bc = new BroadcastChannel("global-count");
|
||||||
const body = new ReadableStream({
|
const body = new ReadableStream({
|
||||||
start(controller) {
|
start(controller) {
|
||||||
bc.addEventListener("message", () => {
|
bc.addEventListener("message", async () => {
|
||||||
try {
|
try {
|
||||||
const data = getGlobalStatistics();
|
const data = await getGlobalStatistics();
|
||||||
const chunk = `data: ${JSON.stringify({globalCount: data})}\n\n`;
|
const chunk = `data: ${JSON.stringify({globalCount: data})}\n\n`;
|
||||||
controller.enqueue(new TextEncoder().encode(chunk));
|
controller.enqueue(new TextEncoder().encode(chunk));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -43,7 +43,7 @@ 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);
|
await setGlobalStatistics(body.data);
|
||||||
|
|
||||||
const bc = new BroadcastChannel("global-count");
|
const bc = new BroadcastChannel("global-count");
|
||||||
bc.postMessage(new TextEncoder().encode(getGlobalStatistics().toString()))
|
bc.postMessage(new TextEncoder().encode(getGlobalStatistics().toString()))
|
||||||
|
|
18
shared/db.ts
18
shared/db.ts
|
@ -1,17 +1,11 @@
|
||||||
const kv = await Deno.openKv();
|
const kv = await Deno.openKv();
|
||||||
|
|
||||||
export function getGlobalStatistics(): number {
|
export async function getGlobalStatistics() {
|
||||||
let res = 0;
|
const res = await kv.get<number>(["global-statistics"]) ?? 0;
|
||||||
kv.get<number>(["global-statistics"]).then(v => {
|
return res.value ?? 0;
|
||||||
res = v.value as number;
|
|
||||||
});
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setGlobalStatistics(value: number) {
|
export async function setGlobalStatistics(value: number) {
|
||||||
const pv = getGlobalStatistics();
|
const pv = await getGlobalStatistics();
|
||||||
kv.set(["global-statistics"], pv + value).then(() => {
|
await kv.set(["global-statistics"], pv + value);
|
||||||
return;
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue