@powfix/core-js 0.13.39 → 0.14.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.
|
@@ -1,39 +1,36 @@
|
|
|
1
|
-
import
|
|
1
|
+
import EventEmitter from 'eventemitter3';
|
|
2
2
|
export declare enum TimeServiceStatus {
|
|
3
3
|
STOPPED = 0,
|
|
4
4
|
RUNNING = 1
|
|
5
5
|
}
|
|
6
|
-
export declare class TimeService {
|
|
6
|
+
export declare class TimeService extends EventEmitter<TimeService.Event> {
|
|
7
7
|
private static readonly DEFAULT_SYNC_INTERVAL;
|
|
8
|
-
|
|
8
|
+
private static DEFAULT_CLIENT_TIME_PROVIDER;
|
|
9
|
+
private syncHandler?;
|
|
10
|
+
private syncInterval?;
|
|
11
|
+
private clientTimeProvider?;
|
|
12
|
+
private serverTimeProvider?;
|
|
9
13
|
private offset?;
|
|
10
|
-
private option;
|
|
11
14
|
private syncedAt?;
|
|
12
|
-
private emitter;
|
|
13
|
-
readonly on: <T extends TimeService.Event>(event: T, fn: (...args: any[]) => void, context?: any) => EventEmitter3<TimeService.Event, any>;
|
|
14
|
-
readonly off: <T extends TimeService.Event>(event: T, fn?: ((...args: any[]) => void) | undefined, context?: any, once?: boolean) => EventEmitter3<TimeService.Event, any>;
|
|
15
|
-
private readonly emit;
|
|
16
15
|
static calculateNTPResultOffset(ntpResult: TimeService.NTPResult): TimeService.Offset;
|
|
17
16
|
constructor(option: TimeService.Option);
|
|
18
|
-
getOption(): TimeService.Option;
|
|
19
|
-
setOption(option: TimeService.Option): TimeService.Option;
|
|
20
17
|
getOffset(defaultValue: TimeService.Offset): TimeService.Offset;
|
|
21
18
|
getOffset(): TimeService.Offset | undefined;
|
|
22
19
|
setOffset(offset: TimeService.Offset): TimeService.Offset;
|
|
23
20
|
setOffset(offset: TimeService.Offset | undefined): TimeService.Offset;
|
|
24
21
|
getSyncedAt(): TimeService.TimeStamp | undefined;
|
|
25
22
|
private setSyncedAt;
|
|
26
|
-
getSyncInterval(): number
|
|
23
|
+
getSyncInterval(): number;
|
|
27
24
|
setSyncInterval(interval: TimeService.Option['syncInterval']): void;
|
|
25
|
+
getClientTimeProvider(): TimeService.ClientTimeProvider;
|
|
26
|
+
setClientTimeProvider(provider: TimeService.ClientTimeProvider): void;
|
|
28
27
|
getClientTime(defaultValue?: TimeService.TimeStamp): TimeService.TimeStamp;
|
|
28
|
+
getServerTimeProvider(): TimeService.ServerTimeProvider | null | undefined;
|
|
29
|
+
setServerTimeProvider(provider: TimeService.ServerTimeProvider): void;
|
|
29
30
|
getServerTime(): TimeService.TimeStamp | null;
|
|
30
31
|
getTime(): number;
|
|
31
32
|
private fetchServerNTPResult;
|
|
32
|
-
getStatus(): TimeServiceStatus;
|
|
33
|
-
start(): void;
|
|
34
|
-
stop(): void;
|
|
35
33
|
sync(): Promise<TimeService.Offset | null>;
|
|
36
|
-
private syncHandler?;
|
|
37
34
|
private startSync;
|
|
38
35
|
private stopSync;
|
|
39
36
|
}
|
|
@@ -52,9 +49,8 @@ export declare namespace TimeService {
|
|
|
52
49
|
type ClientTimeProvider = () => TimeStamp;
|
|
53
50
|
type ServerTimeProvider = (t1: NTPResult['t1']) => (ServerNTPResult | null) | (Promise<ServerNTPResult | null>);
|
|
54
51
|
interface Option {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
serverTimeProvider?: ServerTimeProvider | undefined;
|
|
52
|
+
syncInterval?: number;
|
|
53
|
+
clientTimeProvider?: ClientTimeProvider;
|
|
54
|
+
serverTimeProvider?: ServerTimeProvider;
|
|
59
55
|
}
|
|
60
56
|
}
|
|
@@ -20,34 +20,24 @@ var TimeServiceStatus;
|
|
|
20
20
|
TimeServiceStatus[TimeServiceStatus["STOPPED"] = 0] = "STOPPED";
|
|
21
21
|
TimeServiceStatus[TimeServiceStatus["RUNNING"] = 1] = "RUNNING";
|
|
22
22
|
})(TimeServiceStatus || (exports.TimeServiceStatus = TimeServiceStatus = {}));
|
|
23
|
-
class TimeService {
|
|
23
|
+
class TimeService extends eventemitter3_1.default {
|
|
24
|
+
static DEFAULT_CLIENT_TIME_PROVIDER() {
|
|
25
|
+
return Date.now();
|
|
26
|
+
}
|
|
24
27
|
static calculateNTPResultOffset(ntpResult) {
|
|
25
28
|
const { t1, t2, t3, t4 } = ntpResult;
|
|
26
29
|
return ((t2 - t1) + (t3 - t4)) / 2;
|
|
27
30
|
}
|
|
28
31
|
constructor(option) {
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
this.
|
|
34
|
-
this.emit = this.emitter.emit.bind(this.emitter);
|
|
35
|
-
this.option = option;
|
|
36
|
-
if (option.autoStart) {
|
|
37
|
-
this.start();
|
|
38
|
-
}
|
|
32
|
+
super();
|
|
33
|
+
// Options
|
|
34
|
+
this.syncInterval = option.syncInterval;
|
|
35
|
+
this.clientTimeProvider = option.clientTimeProvider;
|
|
36
|
+
this.serverTimeProvider = option.serverTimeProvider;
|
|
39
37
|
// Bind
|
|
40
38
|
this.sync = this.sync.bind(this);
|
|
41
|
-
this.start = this.start.bind(this);
|
|
42
|
-
this.stop = this.stop.bind(this);
|
|
43
39
|
this.fetchServerNTPResult = this.fetchServerNTPResult.bind(this);
|
|
44
40
|
}
|
|
45
|
-
getOption() {
|
|
46
|
-
return this.option;
|
|
47
|
-
}
|
|
48
|
-
setOption(option) {
|
|
49
|
-
return this.option = option;
|
|
50
|
-
}
|
|
51
41
|
getOffset(defaultValue) {
|
|
52
42
|
if (this.offset !== undefined) {
|
|
53
43
|
return this.offset;
|
|
@@ -70,41 +60,37 @@ class TimeService {
|
|
|
70
60
|
return syncedAt;
|
|
71
61
|
}
|
|
72
62
|
getSyncInterval() {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return TimeService.DEFAULT_SYNC_INTERVAL;
|
|
76
|
-
}
|
|
77
|
-
if (this.option.syncInterval === null || this.option.syncInterval === -1) {
|
|
78
|
-
// If option is null, do not sync automatically
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
return this.option.syncInterval;
|
|
63
|
+
var _a;
|
|
64
|
+
return (_a = this.syncInterval) !== null && _a !== void 0 ? _a : TimeService.DEFAULT_SYNC_INTERVAL;
|
|
82
65
|
}
|
|
83
66
|
setSyncInterval(interval) {
|
|
84
|
-
this.
|
|
67
|
+
this.syncInterval = interval;
|
|
85
68
|
// Emit
|
|
86
69
|
this.emit('SYNC_INTERVAL_CHANGED', interval);
|
|
87
|
-
if (this.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
this.startSync();
|
|
91
|
-
}
|
|
70
|
+
if (this.syncHandler !== undefined) {
|
|
71
|
+
this.stopSync();
|
|
72
|
+
this.startSync();
|
|
92
73
|
}
|
|
93
74
|
}
|
|
75
|
+
getClientTimeProvider() {
|
|
76
|
+
var _a;
|
|
77
|
+
return (_a = this.clientTimeProvider) !== null && _a !== void 0 ? _a : TimeService.DEFAULT_CLIENT_TIME_PROVIDER;
|
|
78
|
+
}
|
|
79
|
+
setClientTimeProvider(provider) {
|
|
80
|
+
this.clientTimeProvider = provider;
|
|
81
|
+
}
|
|
94
82
|
getClientTime(defaultValue = Date.now()) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
return defaultValue;
|
|
83
|
+
return this.getClientTimeProvider()();
|
|
84
|
+
}
|
|
85
|
+
getServerTimeProvider() {
|
|
86
|
+
return this.serverTimeProvider;
|
|
87
|
+
}
|
|
88
|
+
setServerTimeProvider(provider) {
|
|
89
|
+
this.serverTimeProvider = provider;
|
|
104
90
|
}
|
|
105
91
|
getServerTime() {
|
|
106
92
|
const offset = this.getOffset();
|
|
107
|
-
if (offset
|
|
93
|
+
if (offset == null) {
|
|
108
94
|
return null;
|
|
109
95
|
}
|
|
110
96
|
const clientTime = this.getClientTime();
|
|
@@ -116,8 +102,9 @@ class TimeService {
|
|
|
116
102
|
fetchServerNTPResult(t1) {
|
|
117
103
|
return __awaiter(this, void 0, void 0, function* () {
|
|
118
104
|
try {
|
|
119
|
-
|
|
120
|
-
|
|
105
|
+
const provider = this.getServerTimeProvider();
|
|
106
|
+
if (typeof provider === 'function') {
|
|
107
|
+
return yield provider(t1);
|
|
121
108
|
}
|
|
122
109
|
}
|
|
123
110
|
catch (e) {
|
|
@@ -126,35 +113,6 @@ class TimeService {
|
|
|
126
113
|
return null;
|
|
127
114
|
});
|
|
128
115
|
}
|
|
129
|
-
getStatus() {
|
|
130
|
-
return this.status;
|
|
131
|
-
}
|
|
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
116
|
sync() {
|
|
159
117
|
return __awaiter(this, void 0, void 0, function* () {
|
|
160
118
|
try {
|
|
@@ -205,25 +163,25 @@ class TimeService {
|
|
|
205
163
|
});
|
|
206
164
|
}
|
|
207
165
|
startSync() {
|
|
208
|
-
if (this.syncHandler
|
|
209
|
-
console.warn('sync
|
|
166
|
+
if (this.syncHandler != null) {
|
|
167
|
+
console.warn('sync is already started');
|
|
210
168
|
return;
|
|
211
169
|
}
|
|
212
170
|
const syncInterval = this.getSyncInterval();
|
|
213
|
-
if (syncInterval
|
|
171
|
+
if (syncInterval == null || syncInterval <= 0) {
|
|
172
|
+
console.warn('sync is not started', 'syncInterval', syncInterval);
|
|
214
173
|
return;
|
|
215
174
|
}
|
|
216
|
-
this.syncHandler = setInterval(this.sync, syncInterval);
|
|
175
|
+
this.syncHandler = setInterval(this.sync.bind(this), syncInterval);
|
|
217
176
|
}
|
|
218
177
|
;
|
|
219
178
|
stopSync() {
|
|
220
|
-
if (this.syncHandler
|
|
221
|
-
|
|
179
|
+
if (this.syncHandler != null) {
|
|
180
|
+
clearInterval(this.syncHandler);
|
|
181
|
+
this.syncHandler = undefined;
|
|
222
182
|
}
|
|
223
|
-
clearInterval(this.syncHandler);
|
|
224
|
-
this.syncHandler = undefined;
|
|
225
183
|
}
|
|
226
184
|
;
|
|
227
185
|
}
|
|
228
186
|
exports.TimeService = TimeService;
|
|
229
|
-
TimeService.DEFAULT_SYNC_INTERVAL =
|
|
187
|
+
TimeService.DEFAULT_SYNC_INTERVAL = 60 * 1000;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powfix/core-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "core package",
|
|
5
5
|
"author": "Kwon Kyung-Min <powfix@gmail.com>",
|
|
6
6
|
"private": false,
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"typescript": "5.8.3"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"moment": ">=2.0.0"
|
|
46
|
+
"moment": ">=2.0.0",
|
|
47
|
+
"eventemitter3": ">=5"
|
|
47
48
|
}
|
|
48
49
|
}
|