-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock.js
88 lines (77 loc) · 2.49 KB
/
clock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { iterateObjectPaths } from "./objectPaths.js";
import get from "lodash/get.js";
export const initTimestamp = (nodeId = 0) => `${nodeId}-0`;
export const incrementTimestamp = (timestamp) => {
const [nodeId, time] = timestamp.split("-");
return `${parseInt(nodeId)}-${parseInt(time) + 1}`;
};
export const validateTimestamp = (timestamp) => {
return /^\d+-\d+$/.test(timestamp);
};
export const counterFromTimestamp = (timestamp) => {
return parseInt(timestamp.split("-")[1]);
};
export const nodeIdFromTimestamp = (timestamp) => {
return parseInt(timestamp.split("-")[0]);
};
export const compareTimestamps = (timestamp1, timestamp2) => {
if (!validateTimestamp(timestamp1) || !validateTimestamp(timestamp2)) {
throw new Error(`Invalid timestamp in ${timestamp1} or ${timestamp2}`);
}
const [nodeId1, time1] = timestamp1.split("-").map((x) => parseInt(x));
const [nodeId2, time2] = timestamp2.split("-").map((x) => parseInt(x));
return time1 > time2 || (time1 === time2 && nodeId1 > nodeId2);
};
export const getHighestTimestamp = (timestamps) => {
if (typeof timestamps !== "object") {
throw new Error("Timestamps must be object");
}
let maxTimestamp = initTimestamp();
iterateObjectPaths(timestamps, (path) => {
const timestamp = get(timestamps, path);
if (typeof timestamp !== "string") {
throw new Error(`Timestamps must be strings but is ${typeof timestamp}`);
}
if (!validateTimestamp(timestamp)) {
throw new Error(`Invalid timestamp: ${timestamp}`);
}
if (compareTimestamps(timestamp, maxTimestamp)) {
maxTimestamp = timestamp;
}
});
return maxTimestamp;
};
/**
* 一个简单的时钟,用于跟踪当前时间戳。
* 此外,它还使用 nodeId 来打破具有相同时间戳的计数器之间的联系。
*/
export class Clock {
constructor(nodeId, count = 0) {
if (nodeId < 0 || !Number.isInteger(nodeId)) {
throw new Error("nodeId must be a positive integer");
}
this.nodeId = nodeId;
this.count = count;
}
tick() {
this.count++;
return this.timestamp();
}
update(timestamp) {
const counter = counterFromTimestamp(timestamp);
this.count = Math.max(this.count, counter);
}
timestamp() {
return `${this.nodeId}-${this.count}`;
}
toString() {
return this.timestamp();
}
counter() {
return this.count;
}
static fromString(timestamp) {
const [nodeId, count] = timestamp.split("-").map((x) => parseInt(x));
return new Clock(nodeId, count);
}
}