@powfix/core-js 0.9.29 → 0.9.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -15,8 +15,8 @@ export declare class TimeService {
|
|
|
15
15
|
private readonly emit;
|
|
16
16
|
static calculateNTPResultOffset(ntpResult: TimeService.NTPResult): TimeService.Offset;
|
|
17
17
|
constructor(option: TimeService.Option);
|
|
18
|
-
getOption()
|
|
19
|
-
setOption(option: TimeService.Option)
|
|
18
|
+
getOption: () => TimeService.Option;
|
|
19
|
+
setOption: (option: TimeService.Option) => TimeService.Option;
|
|
20
20
|
getOffset(defaultValue: TimeService.Offset): TimeService.Offset;
|
|
21
21
|
getOffset(): TimeService.Offset | undefined;
|
|
22
22
|
setOffset(offset: TimeService.Offset): TimeService.Offset;
|
|
@@ -30,9 +30,9 @@ export declare class TimeService {
|
|
|
30
30
|
getTime(): number;
|
|
31
31
|
private readonly fetchServerNTPResult;
|
|
32
32
|
getStatus(): TimeServiceStatus;
|
|
33
|
-
start()
|
|
34
|
-
stop()
|
|
35
|
-
sync()
|
|
33
|
+
start: () => void;
|
|
34
|
+
stop: () => void;
|
|
35
|
+
sync: () => Promise<TimeService.Offset | null>;
|
|
36
36
|
private syncHandler?;
|
|
37
37
|
private startSync;
|
|
38
38
|
private stopSync;
|
|
@@ -32,6 +32,12 @@ class TimeService {
|
|
|
32
32
|
this.on = this.emitter.on;
|
|
33
33
|
this.off = this.emitter.off;
|
|
34
34
|
this.emit = this.emitter.emit;
|
|
35
|
+
this.getOption = () => {
|
|
36
|
+
return this.option;
|
|
37
|
+
};
|
|
38
|
+
this.setOption = (option) => {
|
|
39
|
+
return this.option = option;
|
|
40
|
+
};
|
|
35
41
|
this.fetchServerNTPResult = (t1) => __awaiter(this, void 0, void 0, function* () {
|
|
36
42
|
try {
|
|
37
43
|
if (typeof this.option.serverTimeProvider === 'function') {
|
|
@@ -43,6 +49,79 @@ class TimeService {
|
|
|
43
49
|
}
|
|
44
50
|
return null;
|
|
45
51
|
});
|
|
52
|
+
this.start = () => {
|
|
53
|
+
if (this.status !== TimeServiceStatus.STOPPED) {
|
|
54
|
+
console.warn(LOG_TAG, 'service is not stopped');
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
// Change status
|
|
58
|
+
this.status = TimeServiceStatus.RUNNING;
|
|
59
|
+
// Sync immediately
|
|
60
|
+
this.sync().finally(() => { });
|
|
61
|
+
// Start sync
|
|
62
|
+
this.startSync();
|
|
63
|
+
};
|
|
64
|
+
this.stop = () => {
|
|
65
|
+
if (this.status !== TimeServiceStatus.RUNNING) {
|
|
66
|
+
console.warn(LOG_TAG, 'service is not running');
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// Change status
|
|
70
|
+
this.status = TimeServiceStatus.RUNNING;
|
|
71
|
+
// Stop sync
|
|
72
|
+
this.stopSync();
|
|
73
|
+
// Reset offset
|
|
74
|
+
this.setOffset(undefined);
|
|
75
|
+
// Reset synced at
|
|
76
|
+
this.setSyncedAt(undefined);
|
|
77
|
+
};
|
|
78
|
+
this.sync = () => __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
try {
|
|
80
|
+
// T1 (Client Request Time)
|
|
81
|
+
const requestedAt = Date.now();
|
|
82
|
+
// Fetch server time from server
|
|
83
|
+
const serverNtpResult = yield this.fetchServerNTPResult(requestedAt);
|
|
84
|
+
// Check is null
|
|
85
|
+
if (serverNtpResult === null) {
|
|
86
|
+
console.warn(LOG_TAG, 'Failed to get server time');
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
// T2 (Server Receive Time)
|
|
90
|
+
const { t2 } = serverNtpResult;
|
|
91
|
+
// Check is not a number
|
|
92
|
+
if (isNaN(Number(t2))) {
|
|
93
|
+
// Not a Number
|
|
94
|
+
console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
// T3 (Server Transmit Time)
|
|
98
|
+
const { t3 } = serverNtpResult;
|
|
99
|
+
// Check is not a number
|
|
100
|
+
if (isNaN(Number(t3))) {
|
|
101
|
+
// Not a Number
|
|
102
|
+
console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
// T4 (Client Receive Time)
|
|
106
|
+
const receivedAt = Date.now();
|
|
107
|
+
const ntpResult = {
|
|
108
|
+
t1: requestedAt,
|
|
109
|
+
t2: t2,
|
|
110
|
+
t3: t3,
|
|
111
|
+
t4: receivedAt,
|
|
112
|
+
};
|
|
113
|
+
// Calculate offset
|
|
114
|
+
const offset = TimeService.calculateNTPResultOffset(ntpResult);
|
|
115
|
+
// Save calculated offset
|
|
116
|
+
this.setOffset(offset);
|
|
117
|
+
// Mark synced timestamp
|
|
118
|
+
this.setSyncedAt(Date.now());
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
console.error(e);
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
});
|
|
46
125
|
this.option = option;
|
|
47
126
|
if (option.autoStart) {
|
|
48
127
|
this.start();
|
|
@@ -56,12 +135,6 @@ class TimeService {
|
|
|
56
135
|
this.setOption = this.setOption.bind(this);
|
|
57
136
|
this.setSyncedAt = this.setSyncedAt.bind(this);
|
|
58
137
|
}
|
|
59
|
-
getOption() {
|
|
60
|
-
return this.option;
|
|
61
|
-
}
|
|
62
|
-
setOption(option) {
|
|
63
|
-
return this.option = option;
|
|
64
|
-
}
|
|
65
138
|
getOffset(defaultValue) {
|
|
66
139
|
if (this.offset !== undefined) {
|
|
67
140
|
return this.offset;
|
|
@@ -130,81 +203,6 @@ class TimeService {
|
|
|
130
203
|
getStatus() {
|
|
131
204
|
return this.status;
|
|
132
205
|
}
|
|
133
|
-
start() {
|
|
134
|
-
if (this.status !== TimeServiceStatus.STOPPED) {
|
|
135
|
-
console.warn(LOG_TAG, 'service is not stopped');
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
// Change status
|
|
139
|
-
this.status = TimeServiceStatus.RUNNING;
|
|
140
|
-
// Sync immediately
|
|
141
|
-
this.sync().finally(() => { });
|
|
142
|
-
// Start sync
|
|
143
|
-
this.startSync();
|
|
144
|
-
}
|
|
145
|
-
stop() {
|
|
146
|
-
if (this.status !== TimeServiceStatus.RUNNING) {
|
|
147
|
-
console.warn(LOG_TAG, 'service is not running');
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
// Change status
|
|
151
|
-
this.status = TimeServiceStatus.RUNNING;
|
|
152
|
-
// Stop sync
|
|
153
|
-
this.stopSync();
|
|
154
|
-
// Reset offset
|
|
155
|
-
this.setOffset(undefined);
|
|
156
|
-
// Reset synced at
|
|
157
|
-
this.setSyncedAt(undefined);
|
|
158
|
-
}
|
|
159
|
-
sync() {
|
|
160
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
-
try {
|
|
162
|
-
// T1 (Client Request Time)
|
|
163
|
-
const requestedAt = Date.now();
|
|
164
|
-
// Fetch server time from server
|
|
165
|
-
const serverNtpResult = yield this.fetchServerNTPResult(requestedAt);
|
|
166
|
-
// Check is null
|
|
167
|
-
if (serverNtpResult === null) {
|
|
168
|
-
console.warn(LOG_TAG, 'Failed to get server time');
|
|
169
|
-
return null;
|
|
170
|
-
}
|
|
171
|
-
// T2 (Server Receive Time)
|
|
172
|
-
const { t2 } = serverNtpResult;
|
|
173
|
-
// Check is not a number
|
|
174
|
-
if (isNaN(Number(t2))) {
|
|
175
|
-
// Not a Number
|
|
176
|
-
console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
// T3 (Server Transmit Time)
|
|
180
|
-
const { t3 } = serverNtpResult;
|
|
181
|
-
// Check is not a number
|
|
182
|
-
if (isNaN(Number(t3))) {
|
|
183
|
-
// Not a Number
|
|
184
|
-
console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
|
|
185
|
-
return null;
|
|
186
|
-
}
|
|
187
|
-
// T4 (Client Receive Time)
|
|
188
|
-
const receivedAt = Date.now();
|
|
189
|
-
const ntpResult = {
|
|
190
|
-
t1: requestedAt,
|
|
191
|
-
t2: t2,
|
|
192
|
-
t3: t3,
|
|
193
|
-
t4: receivedAt,
|
|
194
|
-
};
|
|
195
|
-
// Calculate offset
|
|
196
|
-
const offset = TimeService.calculateNTPResultOffset(ntpResult);
|
|
197
|
-
// Save calculated offset
|
|
198
|
-
this.setOffset(offset);
|
|
199
|
-
// Mark synced timestamp
|
|
200
|
-
this.setSyncedAt(Date.now());
|
|
201
|
-
}
|
|
202
|
-
catch (e) {
|
|
203
|
-
console.error(e);
|
|
204
|
-
}
|
|
205
|
-
return null;
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
206
|
startSync() {
|
|
209
207
|
if (this.syncHandler !== undefined) {
|
|
210
208
|
console.warn('sync handler is not undefined', this.syncHandler);
|