@powfix/core-js 0.13.39 → 0.14.1

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,60 +1,31 @@
1
- import EventEmitter3 from 'eventemitter3';
2
- export declare enum TimeServiceStatus {
3
- STOPPED = 0,
4
- RUNNING = 1
5
- }
6
- export declare class TimeService {
7
- private static readonly DEFAULT_SYNC_INTERVAL;
8
- protected status: TimeServiceStatus;
1
+ import EventEmitter from 'eventemitter3';
2
+ import type { TimeServiceClientTimeProvider, TimeServiceEvent, TimeServiceNtpResult, TimeServiceOffset, TimeServiceOptions, TimeServiceServerTimeProvider, TimeServiceTimeStamp } from "./TimeService.type";
3
+ export declare class TimeService extends EventEmitter<TimeServiceEvent> {
4
+ private syncHandler?;
9
5
  private offset?;
10
- private option;
11
6
  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
- static calculateNTPResultOffset(ntpResult: TimeService.NTPResult): TimeService.Offset;
17
- constructor(option: TimeService.Option);
18
- getOption(): TimeService.Option;
19
- setOption(option: TimeService.Option): TimeService.Option;
20
- getOffset(defaultValue: TimeService.Offset): TimeService.Offset;
21
- getOffset(): TimeService.Offset | undefined;
22
- setOffset(offset: TimeService.Offset): TimeService.Offset;
23
- setOffset(offset: TimeService.Offset | undefined): TimeService.Offset;
24
- getSyncedAt(): TimeService.TimeStamp | undefined;
7
+ protected syncInterval?: number | null;
8
+ protected clientTimeProvider?: TimeServiceClientTimeProvider;
9
+ protected serverTimeProvider?: TimeServiceServerTimeProvider;
10
+ static calculateNTPResultOffset(ntpResult: TimeServiceNtpResult): TimeServiceOffset;
11
+ constructor(option: TimeServiceOptions);
12
+ getOffset(defaultValue: TimeServiceOffset): TimeServiceOffset;
13
+ getOffset(): TimeServiceOffset | undefined;
14
+ setOffset(offset: TimeServiceOffset): TimeServiceOffset;
15
+ setOffset(offset: TimeServiceOffset | undefined): TimeServiceOffset;
16
+ getSyncedAt(): TimeServiceTimeStamp | undefined;
25
17
  private setSyncedAt;
26
- getSyncInterval(): number | null;
27
- setSyncInterval(interval: TimeService.Option['syncInterval']): void;
28
- getClientTime(defaultValue?: TimeService.TimeStamp): TimeService.TimeStamp;
29
- getServerTime(): TimeService.TimeStamp | null;
18
+ getSyncInterval(): number | null | undefined;
19
+ setSyncInterval(interval: TimeServiceOptions['syncInterval']): void;
20
+ getClientTimeProvider(): TimeServiceClientTimeProvider | undefined;
21
+ setClientTimeProvider(provider: TimeServiceClientTimeProvider | null | undefined): void;
22
+ getClientTime(): number | undefined;
23
+ getServerTimeProvider(): TimeServiceServerTimeProvider | undefined;
24
+ setServerTimeProvider(provider: TimeServiceServerTimeProvider | null | undefined): void;
25
+ getServerTime(): number | undefined;
30
26
  getTime(): number;
31
27
  private fetchServerNTPResult;
32
- getStatus(): TimeServiceStatus;
33
- start(): void;
34
- stop(): void;
35
- sync(): Promise<TimeService.Offset | null>;
36
- private syncHandler?;
28
+ sync(): Promise<TimeServiceOffset | null>;
37
29
  private startSync;
38
30
  private stopSync;
39
31
  }
40
- export declare namespace TimeService {
41
- type Offset = number;
42
- type TimeStamp = number;
43
- interface NTPResult {
44
- t1: TimeStamp;
45
- t2: TimeStamp;
46
- t3: TimeStamp;
47
- t4: TimeStamp;
48
- }
49
- type Event = 'SYNCED' | 'SYNC_INTERVAL_CHANGED';
50
- interface ServerNTPResult extends Pick<NTPResult, 't2' | 't3'> {
51
- }
52
- type ClientTimeProvider = () => TimeStamp;
53
- type ServerTimeProvider = (t1: NTPResult['t1']) => (ServerNTPResult | null) | (Promise<ServerNTPResult | null>);
54
- interface Option {
55
- autoStart?: boolean;
56
- syncInterval?: number | null | undefined;
57
- clientTimeProvider?: ClientTimeProvider | undefined;
58
- serverTimeProvider?: ServerTimeProvider | undefined;
59
- }
60
- }
@@ -12,42 +12,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.TimeService = exports.TimeServiceStatus = void 0;
15
+ exports.TimeService = void 0;
16
16
  const eventemitter3_1 = __importDefault(require("eventemitter3"));
17
17
  const LOG_TAG = 'TimeService';
18
- var TimeServiceStatus;
19
- (function (TimeServiceStatus) {
20
- TimeServiceStatus[TimeServiceStatus["STOPPED"] = 0] = "STOPPED";
21
- TimeServiceStatus[TimeServiceStatus["RUNNING"] = 1] = "RUNNING";
22
- })(TimeServiceStatus || (exports.TimeServiceStatus = TimeServiceStatus = {}));
23
- class TimeService {
18
+ class TimeService extends eventemitter3_1.default {
24
19
  static calculateNTPResultOffset(ntpResult) {
25
20
  const { t1, t2, t3, t4 } = ntpResult;
26
21
  return ((t2 - t1) + (t3 - t4)) / 2;
27
22
  }
28
23
  constructor(option) {
29
- this.status = TimeServiceStatus.STOPPED;
30
- // Emitter
31
- this.emitter = new eventemitter3_1.default();
32
- this.on = this.emitter.on.bind(this.emitter);
33
- this.off = this.emitter.off.bind(this.emitter);
34
- this.emit = this.emitter.emit.bind(this.emitter);
35
- this.option = option;
36
- if (option.autoStart) {
37
- this.start();
38
- }
24
+ super();
25
+ // Options
26
+ this.syncInterval = option.syncInterval;
27
+ this.clientTimeProvider = option.clientTimeProvider;
28
+ this.serverTimeProvider = option.serverTimeProvider;
39
29
  // Bind
40
30
  this.sync = this.sync.bind(this);
41
- this.start = this.start.bind(this);
42
- this.stop = this.stop.bind(this);
43
31
  this.fetchServerNTPResult = this.fetchServerNTPResult.bind(this);
44
32
  }
45
- getOption() {
46
- return this.option;
47
- }
48
- setOption(option) {
49
- return this.option = option;
50
- }
51
33
  getOffset(defaultValue) {
52
34
  if (this.offset !== undefined) {
53
35
  return this.offset;
@@ -64,60 +46,57 @@ class TimeService {
64
46
  return this.syncedAt;
65
47
  }
66
48
  setSyncedAt(syncedAt) {
67
- this.syncedAt = syncedAt;
68
- // Emit
69
- this.emit('SYNCED', syncedAt);
70
- return syncedAt;
49
+ return (this.syncedAt = syncedAt);
71
50
  }
72
51
  getSyncInterval() {
73
- if (this.option.syncInterval === undefined) {
74
- // If option is undefined using default value
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;
52
+ return this.syncInterval;
82
53
  }
83
54
  setSyncInterval(interval) {
84
- this.option.syncInterval = interval;
55
+ this.syncInterval = interval;
85
56
  // Emit
86
57
  this.emit('SYNC_INTERVAL_CHANGED', interval);
87
- if (this.status === TimeServiceStatus.RUNNING) {
88
- if (this.syncHandler !== undefined) {
89
- this.stopSync();
90
- this.startSync();
91
- }
58
+ if (this.syncHandler !== undefined) {
59
+ this.stopSync();
60
+ this.startSync();
92
61
  }
93
62
  }
94
- getClientTime(defaultValue = Date.now()) {
95
- try {
96
- if (typeof this.option.clientTimeProvider === 'function') {
97
- return this.option.clientTimeProvider();
98
- }
99
- }
100
- catch (e) {
101
- console.error(e);
102
- }
103
- return defaultValue;
63
+ getClientTimeProvider() {
64
+ return this.clientTimeProvider;
65
+ }
66
+ setClientTimeProvider(provider) {
67
+ this.clientTimeProvider = provider !== null && provider !== void 0 ? provider : undefined;
68
+ }
69
+ getClientTime() {
70
+ var _a;
71
+ return (_a = this.getClientTimeProvider()) === null || _a === void 0 ? void 0 : _a();
72
+ }
73
+ getServerTimeProvider() {
74
+ return this.serverTimeProvider;
75
+ }
76
+ setServerTimeProvider(provider) {
77
+ this.serverTimeProvider = provider !== null && provider !== void 0 ? provider : undefined;
104
78
  }
105
79
  getServerTime() {
106
80
  const offset = this.getOffset();
107
- if (offset === undefined) {
108
- return null;
81
+ if (offset == null) {
82
+ return offset;
109
83
  }
110
84
  const clientTime = this.getClientTime();
85
+ if (clientTime == null) {
86
+ return clientTime;
87
+ }
111
88
  return clientTime + offset;
112
89
  }
113
90
  getTime() {
114
- return this.getServerTime() || this.getClientTime();
91
+ var _a, _b;
92
+ return (_b = (_a = this.getServerTime()) !== null && _a !== void 0 ? _a : this.getClientTime()) !== null && _b !== void 0 ? _b : Date.now();
115
93
  }
116
94
  fetchServerNTPResult(t1) {
117
95
  return __awaiter(this, void 0, void 0, function* () {
118
96
  try {
119
- if (typeof this.option.serverTimeProvider === 'function') {
120
- return yield this.option.serverTimeProvider(t1);
97
+ const provider = this.getServerTimeProvider();
98
+ if (typeof provider === 'function') {
99
+ return yield provider(t1);
121
100
  }
122
101
  }
123
102
  catch (e) {
@@ -126,35 +105,6 @@ class TimeService {
126
105
  return null;
127
106
  });
128
107
  }
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
108
  sync() {
159
109
  return __awaiter(this, void 0, void 0, function* () {
160
110
  try {
@@ -196,7 +146,9 @@ class TimeService {
196
146
  // Save calculated offset
197
147
  this.setOffset(offset);
198
148
  // Mark synced timestamp
199
- this.setSyncedAt(Date.now());
149
+ const syncedAt = Date.now();
150
+ this.setSyncedAt(syncedAt);
151
+ this.emit('SYNCED', offset, syncedAt);
200
152
  }
201
153
  catch (e) {
202
154
  console.error(e);
@@ -205,25 +157,24 @@ class TimeService {
205
157
  });
206
158
  }
207
159
  startSync() {
208
- if (this.syncHandler !== undefined) {
209
- console.warn('sync handler is not undefined', this.syncHandler);
160
+ if (this.syncHandler != null) {
161
+ console.warn('sync is already started');
210
162
  return;
211
163
  }
212
164
  const syncInterval = this.getSyncInterval();
213
- if (syncInterval === null) {
165
+ if (syncInterval == null || syncInterval <= 0) {
166
+ console.warn('sync is not started', 'syncInterval', syncInterval);
214
167
  return;
215
168
  }
216
- this.syncHandler = setInterval(this.sync, syncInterval);
169
+ this.syncHandler = setInterval(this.sync.bind(this), syncInterval);
217
170
  }
218
171
  ;
219
172
  stopSync() {
220
- if (this.syncHandler === undefined) {
221
- return;
173
+ if (this.syncHandler != null) {
174
+ clearInterval(this.syncHandler);
175
+ this.syncHandler = undefined;
222
176
  }
223
- clearInterval(this.syncHandler);
224
- this.syncHandler = undefined;
225
177
  }
226
178
  ;
227
179
  }
228
180
  exports.TimeService = TimeService;
229
- TimeService.DEFAULT_SYNC_INTERVAL = 60000;
@@ -0,0 +1,21 @@
1
+ export type TimeServiceOffset = number;
2
+ export type TimeServiceTimeStamp = number;
3
+ export interface TimeServiceNtpResult {
4
+ t1: TimeServiceTimeStamp;
5
+ t2: TimeServiceTimeStamp;
6
+ t3: TimeServiceTimeStamp;
7
+ t4: TimeServiceTimeStamp;
8
+ }
9
+ export interface TimeServiceServerNtpResult extends Pick<TimeServiceNtpResult, 't2' | 't3'> {
10
+ }
11
+ export type TimeServiceClientTimeProvider = () => TimeServiceTimeStamp;
12
+ export type TimeServiceServerTimeProvider = (t1: TimeServiceNtpResult['t1']) => (TimeServiceServerNtpResult | null) | (Promise<TimeServiceServerNtpResult | null>);
13
+ export type TimeServiceEvent = {
14
+ SYNCED: (offset: TimeServiceOffset, syncedAt: TimeServiceTimeStamp) => void;
15
+ SYNC_INTERVAL_CHANGED: (interval: number | undefined) => void;
16
+ };
17
+ export interface TimeServiceOptions {
18
+ syncInterval?: number;
19
+ clientTimeProvider?: TimeServiceClientTimeProvider;
20
+ serverTimeProvider?: TimeServiceServerTimeProvider;
21
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powfix/core-js",
3
- "version": "0.13.39",
3
+ "version": "0.14.1",
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
  }