@powfix/core-js 0.9.32 → 0.10.0
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
|
|
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,18 +32,6 @@ class TimeService {
|
|
|
32
32
|
this.on = this.emitter.on.bind(this.emitter);
|
|
33
33
|
this.off = this.emitter.off.bind(this.emitter);
|
|
34
34
|
this.emit = this.emitter.emit.bind(this.emitter);
|
|
35
|
-
this.getOption = () => {
|
|
36
|
-
return this.option;
|
|
37
|
-
};
|
|
38
|
-
this.setOption = (option) => {
|
|
39
|
-
return this.option = option;
|
|
40
|
-
};
|
|
41
|
-
this.setSyncedAt = (syncedAt) => {
|
|
42
|
-
this.syncedAt = syncedAt;
|
|
43
|
-
// Emit
|
|
44
|
-
this.emit('SYNCED', syncedAt);
|
|
45
|
-
return syncedAt;
|
|
46
|
-
};
|
|
47
35
|
this.fetchServerNTPResult = (t1) => __awaiter(this, void 0, void 0, function* () {
|
|
48
36
|
try {
|
|
49
37
|
if (typeof this.option.serverTimeProvider === 'function') {
|
|
@@ -55,91 +43,16 @@ class TimeService {
|
|
|
55
43
|
}
|
|
56
44
|
return null;
|
|
57
45
|
});
|
|
58
|
-
this.start = () => {
|
|
59
|
-
if (this.status !== TimeServiceStatus.STOPPED) {
|
|
60
|
-
console.warn(LOG_TAG, 'service is not stopped');
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
// Change status
|
|
64
|
-
this.status = TimeServiceStatus.RUNNING;
|
|
65
|
-
// Sync immediately
|
|
66
|
-
this.sync().finally(() => { });
|
|
67
|
-
// Start sync
|
|
68
|
-
this.startSync();
|
|
69
|
-
};
|
|
70
|
-
this.stop = () => {
|
|
71
|
-
if (this.status !== TimeServiceStatus.RUNNING) {
|
|
72
|
-
console.warn(LOG_TAG, 'service is not running');
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
// Change status
|
|
76
|
-
this.status = TimeServiceStatus.RUNNING;
|
|
77
|
-
// Stop sync
|
|
78
|
-
this.stopSync();
|
|
79
|
-
// Reset offset
|
|
80
|
-
this.setOffset(undefined);
|
|
81
|
-
// Reset synced at
|
|
82
|
-
this.setSyncedAt(undefined);
|
|
83
|
-
};
|
|
84
|
-
this.sync = () => __awaiter(this, void 0, void 0, function* () {
|
|
85
|
-
try {
|
|
86
|
-
// T1 (Client Request Time)
|
|
87
|
-
const requestedAt = Date.now();
|
|
88
|
-
// Fetch server time from server
|
|
89
|
-
const serverNtpResult = yield this.fetchServerNTPResult(requestedAt);
|
|
90
|
-
// Check is null
|
|
91
|
-
if (serverNtpResult === null) {
|
|
92
|
-
console.warn(LOG_TAG, 'Failed to get server time');
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
// T2 (Server Receive Time)
|
|
96
|
-
const { t2 } = serverNtpResult;
|
|
97
|
-
// Check is not a number
|
|
98
|
-
if (isNaN(Number(t2))) {
|
|
99
|
-
// Not a Number
|
|
100
|
-
console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
// T3 (Server Transmit Time)
|
|
104
|
-
const { t3 } = serverNtpResult;
|
|
105
|
-
// Check is not a number
|
|
106
|
-
if (isNaN(Number(t3))) {
|
|
107
|
-
// Not a Number
|
|
108
|
-
console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
|
|
109
|
-
return null;
|
|
110
|
-
}
|
|
111
|
-
// T4 (Client Receive Time)
|
|
112
|
-
const receivedAt = Date.now();
|
|
113
|
-
const ntpResult = {
|
|
114
|
-
t1: requestedAt,
|
|
115
|
-
t2: t2,
|
|
116
|
-
t3: t3,
|
|
117
|
-
t4: receivedAt,
|
|
118
|
-
};
|
|
119
|
-
// Calculate offset
|
|
120
|
-
const offset = TimeService.calculateNTPResultOffset(ntpResult);
|
|
121
|
-
// Save calculated offset
|
|
122
|
-
this.setOffset(offset);
|
|
123
|
-
// Mark synced timestamp
|
|
124
|
-
this.setSyncedAt(Date.now());
|
|
125
|
-
}
|
|
126
|
-
catch (e) {
|
|
127
|
-
console.error(e);
|
|
128
|
-
}
|
|
129
|
-
return null;
|
|
130
|
-
});
|
|
131
46
|
this.option = option;
|
|
132
47
|
if (option.autoStart) {
|
|
133
48
|
this.start();
|
|
134
49
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// this.setOption = this.setOption.bind(this);
|
|
142
|
-
// this.setSyncedAt = this.setSyncedAt.bind(this);
|
|
50
|
+
}
|
|
51
|
+
getOption() {
|
|
52
|
+
return this.option;
|
|
53
|
+
}
|
|
54
|
+
setOption(option) {
|
|
55
|
+
return this.option = option;
|
|
143
56
|
}
|
|
144
57
|
getOffset(defaultValue) {
|
|
145
58
|
if (this.offset !== undefined) {
|
|
@@ -156,6 +69,12 @@ class TimeService {
|
|
|
156
69
|
getSyncedAt() {
|
|
157
70
|
return this.syncedAt;
|
|
158
71
|
}
|
|
72
|
+
setSyncedAt(syncedAt) {
|
|
73
|
+
this.syncedAt = syncedAt;
|
|
74
|
+
// Emit
|
|
75
|
+
this.emit('SYNCED', syncedAt);
|
|
76
|
+
return syncedAt;
|
|
77
|
+
}
|
|
159
78
|
getSyncInterval() {
|
|
160
79
|
if (this.option.syncInterval === undefined) {
|
|
161
80
|
// If option is undefined using default value
|
|
@@ -203,6 +122,81 @@ class TimeService {
|
|
|
203
122
|
getStatus() {
|
|
204
123
|
return this.status;
|
|
205
124
|
}
|
|
125
|
+
start() {
|
|
126
|
+
if (this.status !== TimeServiceStatus.STOPPED) {
|
|
127
|
+
console.warn(LOG_TAG, 'service is not stopped');
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
// Change status
|
|
131
|
+
this.status = TimeServiceStatus.RUNNING;
|
|
132
|
+
// Sync immediately
|
|
133
|
+
this.sync().finally(() => { });
|
|
134
|
+
// Start sync
|
|
135
|
+
this.startSync();
|
|
136
|
+
}
|
|
137
|
+
stop() {
|
|
138
|
+
if (this.status !== TimeServiceStatus.RUNNING) {
|
|
139
|
+
console.warn(LOG_TAG, 'service is not running');
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
// Change status
|
|
143
|
+
this.status = TimeServiceStatus.RUNNING;
|
|
144
|
+
// Stop sync
|
|
145
|
+
this.stopSync();
|
|
146
|
+
// Reset offset
|
|
147
|
+
this.setOffset(undefined);
|
|
148
|
+
// Reset synced at
|
|
149
|
+
this.setSyncedAt(undefined);
|
|
150
|
+
}
|
|
151
|
+
sync() {
|
|
152
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
try {
|
|
154
|
+
// T1 (Client Request Time)
|
|
155
|
+
const requestedAt = Date.now();
|
|
156
|
+
// Fetch server time from server
|
|
157
|
+
const serverNtpResult = yield this.fetchServerNTPResult(requestedAt);
|
|
158
|
+
// Check is null
|
|
159
|
+
if (serverNtpResult === null) {
|
|
160
|
+
console.warn(LOG_TAG, 'Failed to get server time');
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
// T2 (Server Receive Time)
|
|
164
|
+
const { t2 } = serverNtpResult;
|
|
165
|
+
// Check is not a number
|
|
166
|
+
if (isNaN(Number(t2))) {
|
|
167
|
+
// Not a Number
|
|
168
|
+
console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
// T3 (Server Transmit Time)
|
|
172
|
+
const { t3 } = serverNtpResult;
|
|
173
|
+
// Check is not a number
|
|
174
|
+
if (isNaN(Number(t3))) {
|
|
175
|
+
// Not a Number
|
|
176
|
+
console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
// T4 (Client Receive Time)
|
|
180
|
+
const receivedAt = Date.now();
|
|
181
|
+
const ntpResult = {
|
|
182
|
+
t1: requestedAt,
|
|
183
|
+
t2: t2,
|
|
184
|
+
t3: t3,
|
|
185
|
+
t4: receivedAt,
|
|
186
|
+
};
|
|
187
|
+
// Calculate offset
|
|
188
|
+
const offset = TimeService.calculateNTPResultOffset(ntpResult);
|
|
189
|
+
// Save calculated offset
|
|
190
|
+
this.setOffset(offset);
|
|
191
|
+
// Mark synced timestamp
|
|
192
|
+
this.setSyncedAt(Date.now());
|
|
193
|
+
}
|
|
194
|
+
catch (e) {
|
|
195
|
+
console.error(e);
|
|
196
|
+
}
|
|
197
|
+
return null;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
206
200
|
startSync() {
|
|
207
201
|
if (this.syncHandler !== undefined) {
|
|
208
202
|
console.warn('sync handler is not undefined', this.syncHandler);
|