From ff8d42da1a6e41ec430dcb835dcba3e6d9f7598d Mon Sep 17 00:00:00 2001 From: ynwd <10122431+ynwd@users.noreply.github.com> Date: Sun, 29 Sep 2024 13:38:34 +0700 Subject: [PATCH] chore: update map repo --- core/map/mod.test.ts | 16 ++++++++-------- core/map/mod.ts | 23 ++++++++++++----------- core/server/mod.ts | 2 +- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/core/map/mod.test.ts b/core/map/mod.test.ts index f0f4bfaae..5beee53e3 100644 --- a/core/map/mod.test.ts +++ b/core/map/mod.test.ts @@ -129,9 +129,9 @@ const time = d.getTime(); const token = Deno.env.get("GITHUB_TOKEN"); const store = new Store({ owner: "fastrodev", - repo: "fastro", + repo: "store", path: "modules/store/records.json", - branch: "store", + branch: "main", token, }); const i = store.sync(5000); @@ -153,7 +153,6 @@ Deno.test("Store: update value", async () => { }); Deno.test("Store: sync with github periodically", async () => { - await new Promise((resolve) => setTimeout(resolve, 10000)); const g = await store.get("key1"); assertEquals(g, 2); }); @@ -175,22 +174,23 @@ Deno.test("Store: destroy map without options", async () => { const s = new Store({ owner: "fastrodev", - repo: "fastro", + repo: "store", path: "modules/store/map.json", - branch: "store", + branch: "main", token, }); await s.set("exist", true).commit(); Deno.test("Store: sync exist file", async () => { const newStore = new Store({ owner: "fastrodev", - repo: "fastro", + repo: "store", path: "modules/store/map.json", - branch: "store", + branch: "main", token, }); + await newStore.get("exist"); const intervalId = newStore.sync(); - await new Promise((resolve) => setTimeout(resolve, 10000)); + await new Promise((resolve) => setTimeout(resolve, 15000)); const r = await newStore.get("exist"); assertEquals(r, true); clearInterval(intervalId); diff --git a/core/map/mod.ts b/core/map/mod.ts index a688d6cb7..4ef267658 100644 --- a/core/map/mod.ts +++ b/core/map/mod.ts @@ -179,24 +179,25 @@ export class Store { * @param interval * @returns intervalId */ - sync(interval: number = 5000) { + sync(interval: number = 3000) { if (this.intervalId) clearInterval(this.intervalId); - this.intervalId = setInterval(() => { - if (!this.options || (this.map.size === 0)) return; - this.saveToGitHub({ + this.intervalId = setInterval(async () => { + if (!this.options || (this.map.size === 0)) { + return; + } + this.isCommitting = true; + const r = await this.saveToGitHub({ token: this.options.token, owner: this.options.owner, repo: this.options.repo, path: this.options.path, branch: this.options.branch, - }).then((r) => { - console.log(JSON.stringify({ - sha: r.data.content?.sha, - path: r.data.content?.path, - })); - }).catch((error) => { - throw error; }); + console.log(JSON.stringify({ + sha: r.data.content?.sha, + path: r.data.content?.path, + })); + this.isCommitting = false; }, interval); return this.intervalId; } diff --git a/core/server/mod.ts b/core/server/mod.ts index 9561a84e4..e64a2a3e0 100644 --- a/core/server/mod.ts +++ b/core/server/mod.ts @@ -675,7 +675,7 @@ if (root) fetchProps(root); serverOptions: Record = {}; store = new Store({ owner: Deno.env.get("GITHUB_OWNER") || "fastrodev", - repo: Deno.env.get("GITHUB_REPO") || "fastro", + repo: Deno.env.get("GITHUB_REPO") || "store", path: Deno.env.get("GITHUB_PATH") || "modules/store/records.json", branch: Deno.env.get("GITHUB_BRANCH") || "main", token: Deno.env.get("GITHUB_TOKEN"),