socket-function 0.12.11 → 0.12.13

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.12.11",
3
+ "version": "0.12.13",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "note1": "note on node-forge fork, see https://github.com/digitalbazaar/forge/issues/744 for details",
@@ -205,4 +205,29 @@ export function formatBinaryNumber(count: number | undefined, maxAbsoluteValue?:
205
205
  let maxDecimals = noDecimal ? 0 : 3;
206
206
 
207
207
  return formatMaxDecimals(count, maxDecimals, maxAbsoluteValue, currencyDecimalsNeeded ? 2 : undefined) + suffix;
208
+ }
209
+
210
+ /** YYYY/MM/DD HH:MM:SS PM/AM */
211
+ export function formatDateTime(time: number) {
212
+ function p(s: number) {
213
+ return s.toString().padStart(2, "0");
214
+ }
215
+ let date = new Date(time);
216
+ let hours = date.getHours();
217
+ let minutes = date.getMinutes();
218
+ let seconds = date.getSeconds();
219
+ let ampm = hours >= 12 ? "PM" : "AM";
220
+ hours = hours % 12;
221
+ hours = hours ? hours : 12; // the hour '0' should be '12'
222
+ let strTime = p(hours) + ":" + p(minutes) + ":" + p(seconds) + " " + ampm;
223
+ return date.getFullYear() + "/" + p(date.getMonth() + 1) + "/" + p(date.getDate()) + " " + strTime;
224
+ }
225
+
226
+ /** YYYY/MM/DD */
227
+ export function formatDate(time: number) {
228
+ function p(s: number) {
229
+ return s.toString().padStart(2, "0");
230
+ }
231
+ let date = new Date(time);
232
+ return date.getFullYear() + "/" + p(date.getMonth() + 1) + "/" + p(date.getDate());
208
233
  }
package/src/misc.ts CHANGED
@@ -228,7 +228,7 @@ export function throttleFunction<Args extends any[]>(
228
228
  afterCall(Date.now() + delay);
229
229
  }
230
230
  }
231
- function afterCall(setNextAllowedCall: number | undefined) {
231
+ function afterCall(setNextAllowedCall: number | undefined, time = Date.now()) {
232
232
 
233
233
  // NOTE: Ignore error, we really shouldn't have any here
234
234
  if (setNextAllowedCall) {
@@ -237,7 +237,7 @@ export function throttleFunction<Args extends any[]>(
237
237
  if (nextAllowedCall === Number.POSITIVE_INFINITY) return;
238
238
  }
239
239
  if (!pendingArgs) return;
240
- if (Date.now() > nextAllowedCall) {
240
+ if (time > nextAllowedCall) {
241
241
  let args = pendingArgs;
242
242
  pendingArgs = undefined;
243
243
  // Delay, so we don't turn a series of sequential calls to a series of nested calls
@@ -251,7 +251,7 @@ export function throttleFunction<Args extends any[]>(
251
251
  pendingArgs = undefined;
252
252
  doCall(args.args, args.promiseObj);
253
253
  }
254
- }, nextAllowedCall - Date.now());
254
+ }, nextAllowedCall - time);
255
255
  }
256
256
  }
257
257
  return function (...args: Args): Promise<void> {
@@ -266,7 +266,7 @@ export function throttleFunction<Args extends any[]>(
266
266
  return promise.promise;
267
267
  } else {
268
268
  pendingArgs = { args, promiseObj: promiseObj() };
269
- afterCall(undefined);
269
+ afterCall(undefined, time);
270
270
  return pendingArgs.promiseObj.promise;
271
271
  }
272
272
  };
@@ -144,9 +144,14 @@ setInterval(() => {
144
144
  if (baseGetTime() < nextUpdateTime) return;
145
145
  nextUpdateTime = baseGetTime() + UPDATE_INTERVAL;
146
146
  updateTimeOffset().catch((e) => {
147
- console.error("Error updating time offset:", e);
147
+ console.warn("Error updating time offset:", e);
148
148
  });
149
149
  }, UPDATE_SUB_INTERVAL);
150
+ setImmediate(() => {
151
+ updateTimeOffset().catch((e) => {
152
+ console.error("Error updating initial offset:", e);
153
+ });
154
+ });
150
155
 
151
156
 
152
157
  class TimeControllerBase {