socket-function 0.12.10 → 0.12.12
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/SocketFunction.ts +6 -3
- package/package.json +1 -1
- package/src/formatting/format.ts +16 -0
- package/src/misc.ts +4 -4
- package/time/trueTimeShim.ts +6 -1
package/SocketFunction.ts
CHANGED
|
@@ -15,11 +15,13 @@ import { JSONLACKS } from "./src/JSONLACKS/JSONLACKS";
|
|
|
15
15
|
import "./SetProcessVariables";
|
|
16
16
|
import cborx from "cbor-x";
|
|
17
17
|
import { setFlag } from "./require/compileFlags";
|
|
18
|
-
import { shimDateNow, waitForFirstTimeSync } from "./time/trueTimeShim";
|
|
19
18
|
import { isNode } from "./src/misc";
|
|
20
19
|
|
|
21
20
|
/** Always shim Date.now(), because we usually DO want an accurate time... */
|
|
22
|
-
|
|
21
|
+
setImmediate(async () => {
|
|
22
|
+
const { shimDateNow } = await import("./time/trueTimeShim");
|
|
23
|
+
shimDateNow();
|
|
24
|
+
});
|
|
23
25
|
|
|
24
26
|
setFlag(require, "cbor-x", "allowclient", true);
|
|
25
27
|
let cborxInstance = new cborx.Encoder({ structuredClone: true });
|
|
@@ -247,6 +249,7 @@ export class SocketFunction {
|
|
|
247
249
|
this.mountedIP = config.ip;
|
|
248
250
|
}
|
|
249
251
|
|
|
252
|
+
const { waitForFirstTimeSync } = await import("./time/trueTimeShim");
|
|
250
253
|
await waitForFirstTimeSync();
|
|
251
254
|
|
|
252
255
|
// Wait for any additionals functions to expose themselves
|
|
@@ -291,7 +294,7 @@ export class SocketFunction {
|
|
|
291
294
|
}
|
|
292
295
|
|
|
293
296
|
public static browserNodeId() {
|
|
294
|
-
if (
|
|
297
|
+
if (isNode()) {
|
|
295
298
|
throw new Error("Cannot get browser nodeId on server");
|
|
296
299
|
}
|
|
297
300
|
return SocketFunction.connect({ address: location.hostname, port: +location.port || 443 });
|
package/package.json
CHANGED
package/src/formatting/format.ts
CHANGED
|
@@ -205,4 +205,20 @@ 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;
|
|
208
224
|
}
|
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 (
|
|
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 -
|
|
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
|
};
|
package/time/trueTimeShim.ts
CHANGED
|
@@ -144,9 +144,14 @@ setInterval(() => {
|
|
|
144
144
|
if (baseGetTime() < nextUpdateTime) return;
|
|
145
145
|
nextUpdateTime = baseGetTime() + UPDATE_INTERVAL;
|
|
146
146
|
updateTimeOffset().catch((e) => {
|
|
147
|
-
console.
|
|
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 {
|