Skip to content

Commit 035ce3d

Browse files
committed
抽出成 aNow()
1 parent 79ad4e2 commit 035ce3d

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/app/service/service_worker/value.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { type Value, ValueDAO } from "@App/app/repo/value";
55
import type { IGetSender, Group } from "@Packages/message/server";
66
import { type RuntimeService } from "./runtime";
77
import { type PopupService } from "./popup";
8-
import { getStorageName } from "@App/pkg/utils/utils";
8+
import { aNow, getStorageName } from "@App/pkg/utils/utils";
99
import type { ValueUpdateDataEncoded, ValueUpdateSendData, ValueUpdateSender } from "../content/types";
1010
import type { TScriptValueUpdate } from "../queue";
1111
import { type TDeleteScript } from "../queue";
@@ -15,8 +15,6 @@ import { stackAsyncTask } from "@App/pkg/utils/async_queue";
1515
import { encodeMessage } from "@App/pkg/utils/message_value";
1616
import { isEarlyStartScript } from "../content/utils";
1717

18-
let lastUpdateTime = 0;
19-
2018
type ValueUpdateTaskInfo = {
2119
uuid: string;
2220
id: string;
@@ -143,6 +141,7 @@ export class ValueService {
143141
}
144142

145143
async setValuesByStorageName(storageName: string) {
144+
let valueModel: Value | undefined = await this.valueDAO.get(storageName);
146145
const cacheKey = `${CACHE_KEY_SET_VALUE}${storageName}`;
147146
const taskListRef = valueUpdateTasks.get(cacheKey);
148147
if (!taskListRef?.length) return;
@@ -151,19 +150,13 @@ export class ValueService {
151150
// ------ 读取 & 更新 ------
152151
let updatetime = 0;
153152
const listRetToTab: Record<string, ValueUpdateDataEncoded[]> = {};
154-
let valueModel: Value | undefined = await this.valueDAO.get(storageName);
155153
let valueModelUpdated = false;
156154
let hasValueUpdated = false;
157155
for (const task of taskList) {
158156
const entries = [] as [string, any, any][];
159157
const { uuid, values, removeNotProvided } = task;
160158
let oldValueRecord: { [key: string]: any } = {};
161-
let now = Date.now();
162-
// 保证严格递增
163-
if (lastUpdateTime >= now) {
164-
now = lastUpdateTime + 0.0009765625; // 2^-10
165-
}
166-
lastUpdateTime = now;
159+
const now = aNow(); // 保证严格递增
167160
let newData;
168161
if (!valueModel) {
169162
const dataModel: { [key: string]: any } = {};

src/pkg/utils/utils.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
import { describe, expect, it } from "vitest";
2-
import { checkSilenceUpdate, cleanFileName, stringMatching } from "./utils";
2+
import { aNow, checkSilenceUpdate, cleanFileName, stringMatching } from "./utils";
33
import { ltever, versionCompare } from "@App/pkg/utils/semver";
44
import { nextTime } from "./cron";
55
import dayjs from "dayjs";
66

7+
describe.concurrent("aNow", () => {
8+
it.sequential("aNow is Strictly Increasing", () => {
9+
const p1 = [aNow(), aNow(), aNow(), aNow(), aNow(), aNow()];
10+
expect(p1[0]).lessThan(p1[1]);
11+
expect(p1[1]).lessThan(p1[2]);
12+
expect(p1[2]).lessThan(p1[3]);
13+
expect(p1[3]).lessThan(p1[4]);
14+
expect(p1[4]).lessThan(p1[5]);
15+
const p2 = [...p1].sort();
16+
expect(p1).toEqual(p2);
17+
});
18+
it.sequential("t1 > t2 (busy) and t3 = t4 (idle)", async () => {
19+
const _p1 = [aNow(), aNow(), aNow(), aNow(), aNow(), aNow()];
20+
const t1 = aNow();
21+
const t2 = Date.now();
22+
expect(t1).greaterThan(t2);
23+
await new Promise((resolve) => setTimeout(resolve, 10));
24+
const t3 = aNow();
25+
const t4 = Date.now();
26+
expect(t3).toEqual(t4);
27+
});
28+
});
29+
730
describe.concurrent("nextTime", () => {
831
const date = new Date(1737275107000);
932
it.concurrent("每分钟表达式", () => {

src/pkg/utils/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ export function randomMessageFlag(): string {
1111
return `-${Date.now().toString(36)}.${randNum(8e11, 2e12).toString(36)}`;
1212
}
1313

14+
let prevNow = 0;
15+
/**
16+
* accumulated "now".
17+
* 用 aNow 取得的现在时间能保证严格递增
18+
*/
19+
export const aNow = () => {
20+
let now = Date.now();
21+
if (prevNow >= now) now = prevNow + 0.0009765625; // 2^-10
22+
prevNow = now;
23+
return now;
24+
};
25+
1426
export function isFirefox() {
1527
//@ts-ignore
1628
return typeof mozInnerScreenX !== "undefined";

0 commit comments

Comments
 (0)