@powfix/core-js 0.9.29 → 0.9.31

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(): TimeService.Option;
19
- setOption(option: TimeService.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(): void;
34
- stop(): void;
35
- sync(): Promise<TimeService.Offset | null>;
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,18 @@ 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
+ };
41
+ this.setSyncedAt = (syncedAt) => {
42
+ this.syncedAt = syncedAt;
43
+ // Emit
44
+ this.emit('SYNCED', syncedAt);
45
+ return syncedAt;
46
+ };
35
47
  this.fetchServerNTPResult = (t1) => __awaiter(this, void 0, void 0, function* () {
36
48
  try {
37
49
  if (typeof this.option.serverTimeProvider === 'function') {
@@ -43,24 +55,91 @@ class TimeService {
43
55
  }
44
56
  return null;
45
57
  });
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
+ });
46
131
  this.option = option;
47
132
  if (option.autoStart) {
48
133
  this.start();
49
134
  }
50
135
  // Binding
51
- this.emit = this.emit.bind(this);
52
- this.start = this.start.bind(this);
53
- this.stop = this.stop.bind(this);
54
- this.sync = this.sync.bind(this);
55
- this.getOption = this.getOption.bind(this);
56
- this.setOption = this.setOption.bind(this);
57
- this.setSyncedAt = this.setSyncedAt.bind(this);
58
- }
59
- getOption() {
60
- return this.option;
61
- }
62
- setOption(option) {
63
- return this.option = option;
136
+ // this.emit = this.emit.bind(this);
137
+ // this.start = this.start.bind(this);
138
+ // this.stop = this.stop.bind(this);
139
+ // this.sync = this.sync.bind(this);
140
+ // this.getOption = this.getOption.bind(this);
141
+ // this.setOption = this.setOption.bind(this);
142
+ // this.setSyncedAt = this.setSyncedAt.bind(this);
64
143
  }
65
144
  getOffset(defaultValue) {
66
145
  if (this.offset !== undefined) {
@@ -77,12 +156,6 @@ class TimeService {
77
156
  getSyncedAt() {
78
157
  return this.syncedAt;
79
158
  }
80
- setSyncedAt(syncedAt) {
81
- this.syncedAt = syncedAt;
82
- // Emit
83
- this.emit('SYNCED', syncedAt);
84
- return syncedAt;
85
- }
86
159
  getSyncInterval() {
87
160
  if (this.option.syncInterval === undefined) {
88
161
  // If option is undefined using default value
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powfix/core-js",
3
- "version": "0.9.29",
3
+ "version": "0.9.31",
4
4
  "description": "core package",
5
5
  "author": "Kwon Kyung-Min <powfix@gmail.com>",
6
6
  "private": false,