@powfix/core-js 0.9.28 → 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();
|
|
@@ -54,12 +133,7 @@ class TimeService {
|
|
|
54
133
|
this.sync = this.sync.bind(this);
|
|
55
134
|
this.getOption = this.getOption.bind(this);
|
|
56
135
|
this.setOption = this.setOption.bind(this);
|
|
57
|
-
|
|
58
|
-
getOption() {
|
|
59
|
-
return this.option;
|
|
60
|
-
}
|
|
61
|
-
setOption(option) {
|
|
62
|
-
return this.option = option;
|
|
136
|
+
this.setSyncedAt = this.setSyncedAt.bind(this);
|
|
63
137
|
}
|
|
64
138
|
getOffset(defaultValue) {
|
|
65
139
|
if (this.offset !== undefined) {
|
|
@@ -129,81 +203,6 @@ class TimeService {
|
|
|
129
203
|
getStatus() {
|
|
130
204
|
return this.status;
|
|
131
205
|
}
|
|
132
|
-
start() {
|
|
133
|
-
if (this.status !== TimeServiceStatus.STOPPED) {
|
|
134
|
-
console.warn(LOG_TAG, 'service is not stopped');
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
// Change status
|
|
138
|
-
this.status = TimeServiceStatus.RUNNING;
|
|
139
|
-
// Sync immediately
|
|
140
|
-
this.sync().finally(() => { });
|
|
141
|
-
// Start sync
|
|
142
|
-
this.startSync();
|
|
143
|
-
}
|
|
144
|
-
stop() {
|
|
145
|
-
if (this.status !== TimeServiceStatus.RUNNING) {
|
|
146
|
-
console.warn(LOG_TAG, 'service is not running');
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
// Change status
|
|
150
|
-
this.status = TimeServiceStatus.RUNNING;
|
|
151
|
-
// Stop sync
|
|
152
|
-
this.stopSync();
|
|
153
|
-
// Reset offset
|
|
154
|
-
this.setOffset(undefined);
|
|
155
|
-
// Reset synced at
|
|
156
|
-
this.setSyncedAt(undefined);
|
|
157
|
-
}
|
|
158
|
-
sync() {
|
|
159
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
-
try {
|
|
161
|
-
// T1 (Client Request Time)
|
|
162
|
-
const requestedAt = Date.now();
|
|
163
|
-
// Fetch server time from server
|
|
164
|
-
const serverNtpResult = yield this.fetchServerNTPResult(requestedAt);
|
|
165
|
-
// Check is null
|
|
166
|
-
if (serverNtpResult === null) {
|
|
167
|
-
console.warn(LOG_TAG, 'Failed to get server time');
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
// T2 (Server Receive Time)
|
|
171
|
-
const { t2 } = serverNtpResult;
|
|
172
|
-
// Check is not a number
|
|
173
|
-
if (isNaN(Number(t2))) {
|
|
174
|
-
// Not a Number
|
|
175
|
-
console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
// T3 (Server Transmit Time)
|
|
179
|
-
const { t3 } = serverNtpResult;
|
|
180
|
-
// Check is not a number
|
|
181
|
-
if (isNaN(Number(t3))) {
|
|
182
|
-
// Not a Number
|
|
183
|
-
console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
// T4 (Client Receive Time)
|
|
187
|
-
const receivedAt = Date.now();
|
|
188
|
-
const ntpResult = {
|
|
189
|
-
t1: requestedAt,
|
|
190
|
-
t2: t2,
|
|
191
|
-
t3: t3,
|
|
192
|
-
t4: receivedAt,
|
|
193
|
-
};
|
|
194
|
-
// Calculate offset
|
|
195
|
-
const offset = TimeService.calculateNTPResultOffset(ntpResult);
|
|
196
|
-
// Save calculated offset
|
|
197
|
-
this.setOffset(offset);
|
|
198
|
-
// Mark synced timestamp
|
|
199
|
-
this.setSyncedAt(Date.now());
|
|
200
|
-
}
|
|
201
|
-
catch (e) {
|
|
202
|
-
console.error(e);
|
|
203
|
-
}
|
|
204
|
-
return null;
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
206
|
startSync() {
|
|
208
207
|
if (this.syncHandler !== undefined) {
|
|
209
208
|
console.warn('sync handler is not undefined', this.syncHandler);
|