Skip to content

Commit

Permalink
chore: update map repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ynwd committed Sep 29, 2024
1 parent 2b5de15 commit ff8d42d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
16 changes: 8 additions & 8 deletions core/map/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ const time = d.getTime();
const token = Deno.env.get("GITHUB_TOKEN");
const store = new Store<string, number>({
owner: "fastrodev",
repo: "fastro",
repo: "store",
path: "modules/store/records.json",
branch: "store",
branch: "main",
token,
});
const i = store.sync(5000);
Expand All @@ -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);
});
Expand All @@ -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);
Expand Down
23 changes: 12 additions & 11 deletions core/map/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,25 @@ export class Store<K extends string | number | symbol, V> {
* @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;
}
Expand Down
2 changes: 1 addition & 1 deletion core/server/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ if (root) fetchProps(root);
serverOptions: Record<string, any> = {};
store = new Store<string | number | symbol, any>({
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"),
Expand Down

0 comments on commit ff8d42d

Please sign in to comment.