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) {
|
||||
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 = () => {
|
||||
|
|
|
@ -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()))
|
||||
|
|
18
shared/db.ts
18
shared/db.ts
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue