@sv443-network/coreutils 3.2.0 → 3.3.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.
- package/CHANGELOG.md +23 -0
- package/dist/CoreUtils.cjs +249 -225
- package/dist/CoreUtils.min.cjs +3 -3
- package/dist/CoreUtils.min.mjs +3 -3
- package/dist/CoreUtils.min.umd.js +3 -3
- package/dist/CoreUtils.mjs +249 -225
- package/dist/CoreUtils.umd.js +764 -792
- package/dist/lib/DataStore.d.ts +32 -4
- package/package.json +1 -1
package/dist/CoreUtils.mjs
CHANGED
|
@@ -463,9 +463,208 @@ function truncStr(input, length, endStr = "...") {
|
|
|
463
463
|
return finalStr.length > length ? finalStr.substring(0, length) : finalStr;
|
|
464
464
|
}
|
|
465
465
|
|
|
466
|
+
// node_modules/.pnpm/nanoevents@9.1.0/node_modules/nanoevents/index.js
|
|
467
|
+
var createNanoEvents = () => ({
|
|
468
|
+
emit(event, ...args) {
|
|
469
|
+
for (let callbacks = this.events[event] || [], i = 0, length = callbacks.length; i < length; i++) {
|
|
470
|
+
callbacks[i](...args);
|
|
471
|
+
}
|
|
472
|
+
},
|
|
473
|
+
events: {},
|
|
474
|
+
on(event, cb) {
|
|
475
|
+
;
|
|
476
|
+
(this.events[event] ||= []).push(cb);
|
|
477
|
+
return () => {
|
|
478
|
+
var _a;
|
|
479
|
+
this.events[event] = (_a = this.events[event]) == null ? void 0 : _a.filter((i) => cb !== i);
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
// lib/NanoEmitter.ts
|
|
485
|
+
var NanoEmitter = class {
|
|
486
|
+
events = createNanoEvents();
|
|
487
|
+
eventUnsubscribes = [];
|
|
488
|
+
emitterOptions;
|
|
489
|
+
/** Creates a new instance of NanoEmitter - a lightweight event emitter with helper methods and a strongly typed event map */
|
|
490
|
+
constructor(options = {}) {
|
|
491
|
+
this.emitterOptions = {
|
|
492
|
+
publicEmit: false,
|
|
493
|
+
...options
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
//#region on
|
|
497
|
+
/**
|
|
498
|
+
* Subscribes to an event and calls the callback when it's emitted.
|
|
499
|
+
* @param event The event to subscribe to. Use `as "_"` in case your event names aren't thoroughly typed (like when using a template literal, e.g. \`event-${val}\` as "_")
|
|
500
|
+
* @returns Returns a function that can be called to unsubscribe the event listener
|
|
501
|
+
* @example ```ts
|
|
502
|
+
* const emitter = new NanoEmitter<{
|
|
503
|
+
* foo: (bar: string) => void;
|
|
504
|
+
* }>({
|
|
505
|
+
* publicEmit: true,
|
|
506
|
+
* });
|
|
507
|
+
*
|
|
508
|
+
* let i = 0;
|
|
509
|
+
* const unsub = emitter.on("foo", (bar) => {
|
|
510
|
+
* // unsubscribe after 10 events:
|
|
511
|
+
* if(++i === 10) unsub();
|
|
512
|
+
* console.log(bar);
|
|
513
|
+
* });
|
|
514
|
+
*
|
|
515
|
+
* emitter.emit("foo", "bar");
|
|
516
|
+
* ```
|
|
517
|
+
*/
|
|
518
|
+
on(event, cb) {
|
|
519
|
+
let unsub;
|
|
520
|
+
const unsubProxy = () => {
|
|
521
|
+
if (!unsub)
|
|
522
|
+
return;
|
|
523
|
+
unsub();
|
|
524
|
+
this.eventUnsubscribes = this.eventUnsubscribes.filter((u) => u !== unsub);
|
|
525
|
+
};
|
|
526
|
+
unsub = this.events.on(event, cb);
|
|
527
|
+
this.eventUnsubscribes.push(unsub);
|
|
528
|
+
return unsubProxy;
|
|
529
|
+
}
|
|
530
|
+
//#region once
|
|
531
|
+
/**
|
|
532
|
+
* Subscribes to an event and calls the callback or resolves the Promise only once when it's emitted.
|
|
533
|
+
* @param event The event to subscribe to. Use `as "_"` in case your event names aren't thoroughly typed (like when using a template literal, e.g. \`event-${val}\` as "_")
|
|
534
|
+
* @param cb The callback to call when the event is emitted - if provided or not, the returned Promise will resolve with the event arguments
|
|
535
|
+
* @returns Returns a Promise that resolves with the event arguments when the event is emitted
|
|
536
|
+
* @example ```ts
|
|
537
|
+
* const emitter = new NanoEmitter<{
|
|
538
|
+
* foo: (bar: string) => void;
|
|
539
|
+
* }>();
|
|
540
|
+
*
|
|
541
|
+
* // Promise syntax:
|
|
542
|
+
* const [bar] = await emitter.once("foo");
|
|
543
|
+
* console.log(bar);
|
|
544
|
+
*
|
|
545
|
+
* // Callback syntax:
|
|
546
|
+
* emitter.once("foo", (bar) => console.log(bar));
|
|
547
|
+
* ```
|
|
548
|
+
*/
|
|
549
|
+
once(event, cb) {
|
|
550
|
+
return new Promise((resolve) => {
|
|
551
|
+
let unsub;
|
|
552
|
+
const onceProxy = ((...args) => {
|
|
553
|
+
cb == null ? void 0 : cb(...args);
|
|
554
|
+
unsub == null ? void 0 : unsub();
|
|
555
|
+
resolve(args);
|
|
556
|
+
});
|
|
557
|
+
unsub = this.events.on(event, onceProxy);
|
|
558
|
+
this.eventUnsubscribes.push(unsub);
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
//#region onMulti
|
|
562
|
+
/**
|
|
563
|
+
* Allows subscribing to multiple events and calling the callback only when one of, all of, or a subset of the events are emitted, either continuously or only once.
|
|
564
|
+
* @param options An object or array of objects with the following properties:
|
|
565
|
+
* `callback` (required) is the function that will be called when the conditions are met.
|
|
566
|
+
*
|
|
567
|
+
* Set `once` to true to call the callback only once for the first event (or set of events) that match the criteria, then stop listening.
|
|
568
|
+
* If `signal` is provided, the subscription will be canceled when the given signal is aborted.
|
|
569
|
+
*
|
|
570
|
+
* If `oneOf` is used, the callback will be called when any of the matching events are emitted.
|
|
571
|
+
* If `allOf` is used, the callback will be called after all of the matching events are emitted at least once, then any time any of them are emitted.
|
|
572
|
+
* If both `oneOf` and `allOf` are used together, the callback will be called when any of the `oneOf` events are emitted AND all of the `allOf` events have been emitted at least once.
|
|
573
|
+
* At least one of `oneOf` or `allOf` must be provided.
|
|
574
|
+
*
|
|
575
|
+
* @returns Returns a function that can be called to unsubscribe all listeners created by this call. Alternatively, pass an `AbortSignal` to all options objects to achieve the same effect or for finer control.
|
|
576
|
+
*/
|
|
577
|
+
onMulti(options) {
|
|
578
|
+
const allUnsubs = [];
|
|
579
|
+
const unsubAll = () => {
|
|
580
|
+
for (const unsub of allUnsubs)
|
|
581
|
+
unsub();
|
|
582
|
+
allUnsubs.splice(0, allUnsubs.length);
|
|
583
|
+
this.eventUnsubscribes = this.eventUnsubscribes.filter((u) => !allUnsubs.includes(u));
|
|
584
|
+
};
|
|
585
|
+
for (const opts of Array.isArray(options) ? options : [options]) {
|
|
586
|
+
const optsWithDefaults = {
|
|
587
|
+
allOf: [],
|
|
588
|
+
oneOf: [],
|
|
589
|
+
once: false,
|
|
590
|
+
...opts
|
|
591
|
+
};
|
|
592
|
+
const {
|
|
593
|
+
oneOf,
|
|
594
|
+
allOf,
|
|
595
|
+
once,
|
|
596
|
+
signal,
|
|
597
|
+
callback
|
|
598
|
+
} = optsWithDefaults;
|
|
599
|
+
if (signal == null ? void 0 : signal.aborted)
|
|
600
|
+
return unsubAll;
|
|
601
|
+
if (oneOf.length === 0 && allOf.length === 0)
|
|
602
|
+
throw new TypeError("NanoEmitter.onMulti(): Either `oneOf` or `allOf` or both must be provided in the options");
|
|
603
|
+
const curEvtUnsubs = [];
|
|
604
|
+
const checkUnsubAllEvt = (force = false) => {
|
|
605
|
+
if (!(signal == null ? void 0 : signal.aborted) && !force)
|
|
606
|
+
return;
|
|
607
|
+
for (const unsub of curEvtUnsubs)
|
|
608
|
+
unsub();
|
|
609
|
+
curEvtUnsubs.splice(0, curEvtUnsubs.length);
|
|
610
|
+
this.eventUnsubscribes = this.eventUnsubscribes.filter((u) => !curEvtUnsubs.includes(u));
|
|
611
|
+
};
|
|
612
|
+
const allOfEmitted = /* @__PURE__ */ new Set();
|
|
613
|
+
const allOfConditionMet = () => allOf.length === 0 || allOfEmitted.size === allOf.length;
|
|
614
|
+
for (const event of oneOf) {
|
|
615
|
+
const unsub = this.events.on(event, ((...args) => {
|
|
616
|
+
checkUnsubAllEvt();
|
|
617
|
+
if (allOfConditionMet()) {
|
|
618
|
+
callback(event, ...args);
|
|
619
|
+
if (once)
|
|
620
|
+
checkUnsubAllEvt(true);
|
|
621
|
+
}
|
|
622
|
+
}));
|
|
623
|
+
curEvtUnsubs.push(unsub);
|
|
624
|
+
}
|
|
625
|
+
for (const event of allOf) {
|
|
626
|
+
const unsub = this.events.on(event, ((...args) => {
|
|
627
|
+
checkUnsubAllEvt();
|
|
628
|
+
allOfEmitted.add(event);
|
|
629
|
+
if (allOfConditionMet() && (oneOf.length === 0 || oneOf.includes(event))) {
|
|
630
|
+
callback(event, ...args);
|
|
631
|
+
if (once)
|
|
632
|
+
checkUnsubAllEvt(true);
|
|
633
|
+
}
|
|
634
|
+
}));
|
|
635
|
+
curEvtUnsubs.push(unsub);
|
|
636
|
+
}
|
|
637
|
+
allUnsubs.push(() => checkUnsubAllEvt(true));
|
|
638
|
+
}
|
|
639
|
+
return unsubAll;
|
|
640
|
+
}
|
|
641
|
+
//#region emit
|
|
642
|
+
/**
|
|
643
|
+
* Emits an event on this instance.
|
|
644
|
+
* - ⚠️ Needs `publicEmit` to be set to true in the NanoEmitter constructor or super() call!
|
|
645
|
+
* @param event The event to emit
|
|
646
|
+
* @param args The arguments to pass to the event listeners
|
|
647
|
+
* @returns Returns true if `publicEmit` is true and the event was emitted successfully
|
|
648
|
+
*/
|
|
649
|
+
emit(event, ...args) {
|
|
650
|
+
if (this.emitterOptions.publicEmit) {
|
|
651
|
+
this.events.emit(event, ...args);
|
|
652
|
+
return true;
|
|
653
|
+
}
|
|
654
|
+
return false;
|
|
655
|
+
}
|
|
656
|
+
//#region unsubscribeAll
|
|
657
|
+
/** Unsubscribes all event listeners from this instance */
|
|
658
|
+
unsubscribeAll() {
|
|
659
|
+
for (const unsub of this.eventUnsubscribes)
|
|
660
|
+
unsub();
|
|
661
|
+
this.eventUnsubscribes = [];
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
|
|
466
665
|
// lib/DataStore.ts
|
|
467
666
|
var dsFmtVer = 1;
|
|
468
|
-
var DataStore = class {
|
|
667
|
+
var DataStore = class extends NanoEmitter {
|
|
469
668
|
id;
|
|
470
669
|
formatVersion;
|
|
471
670
|
defaultData;
|
|
@@ -497,6 +696,7 @@ var DataStore = class {
|
|
|
497
696
|
* @param opts The options for this DataStore instance
|
|
498
697
|
*/
|
|
499
698
|
constructor(opts) {
|
|
699
|
+
super(opts.nanoEmitterOptions);
|
|
500
700
|
this.id = opts.id;
|
|
501
701
|
this.formatVersion = opts.formatVersion;
|
|
502
702
|
this.defaultData = opts.defaultData;
|
|
@@ -567,30 +767,26 @@ var DataStore = class {
|
|
|
567
767
|
this.migrateIds = [];
|
|
568
768
|
}
|
|
569
769
|
const storedDataRaw = await this.engine.getValue(`${this.keyPrefix}${this.id}-dat`, null);
|
|
570
|
-
|
|
770
|
+
const storedFmtVer = Number(await this.engine.getValue(`${this.keyPrefix}${this.id}-ver`, NaN));
|
|
571
771
|
if (typeof storedDataRaw !== "string" && typeof storedDataRaw !== "object" || storedDataRaw === null || isNaN(storedFmtVer)) {
|
|
572
|
-
await this.saveDefaultData();
|
|
573
|
-
|
|
772
|
+
await this.saveDefaultData(false);
|
|
773
|
+
const data = this.engine.deepCopy(this.defaultData);
|
|
774
|
+
this.events.emit("loadData", data);
|
|
775
|
+
return data;
|
|
574
776
|
}
|
|
575
777
|
const storedData = storedDataRaw ?? JSON.stringify(this.defaultData);
|
|
576
778
|
const encodingFmt = String(await this.engine.getValue(`${this.keyPrefix}${this.id}-enf`, null));
|
|
577
779
|
const isEncoded = encodingFmt !== "null" && encodingFmt !== "false" && encodingFmt !== "0" && encodingFmt !== "" && encodingFmt !== null;
|
|
578
|
-
let saveData = false;
|
|
579
|
-
if (isNaN(storedFmtVer)) {
|
|
580
|
-
await this.engine.setValue(`${this.keyPrefix}${this.id}-ver`, storedFmtVer = this.formatVersion);
|
|
581
|
-
saveData = true;
|
|
582
|
-
}
|
|
583
780
|
let parsed = typeof storedData === "string" ? await this.engine.deserializeData(storedData, isEncoded) : storedData;
|
|
584
781
|
if (storedFmtVer < this.formatVersion && this.migrations)
|
|
585
782
|
parsed = await this.runMigrations(parsed, storedFmtVer);
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
return this.cachedData = this.engine.deepCopy(parsed);
|
|
590
|
-
else
|
|
591
|
-
return this.engine.deepCopy(parsed);
|
|
783
|
+
const result = this.memoryCache ? this.cachedData = this.engine.deepCopy(parsed) : this.engine.deepCopy(parsed);
|
|
784
|
+
this.events.emit("loadData", result);
|
|
785
|
+
return result;
|
|
592
786
|
} catch (err) {
|
|
787
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
593
788
|
console.warn("Error while parsing JSON data, resetting it to the default value.", err);
|
|
789
|
+
this.events.emit("error", error);
|
|
594
790
|
await this.saveDefaultData();
|
|
595
791
|
return this.defaultData;
|
|
596
792
|
}
|
|
@@ -609,27 +805,47 @@ var DataStore = class {
|
|
|
609
805
|
//#region setData
|
|
610
806
|
/** Saves the data synchronously to the in-memory cache and asynchronously to the persistent storage */
|
|
611
807
|
setData(data) {
|
|
612
|
-
|
|
808
|
+
const dataCopy = this.engine.deepCopy(data);
|
|
809
|
+
if (this.memoryCache) {
|
|
613
810
|
this.cachedData = data;
|
|
811
|
+
this.events.emit("updateDataSync", dataCopy);
|
|
812
|
+
}
|
|
614
813
|
return new Promise(async (resolve) => {
|
|
615
|
-
await Promise.allSettled([
|
|
814
|
+
const results = await Promise.allSettled([
|
|
616
815
|
this.engine.setValue(`${this.keyPrefix}${this.id}-dat`, await this.engine.serializeData(data, this.encodingEnabled())),
|
|
617
816
|
this.engine.setValue(`${this.keyPrefix}${this.id}-ver`, this.formatVersion),
|
|
618
817
|
this.engine.setValue(`${this.keyPrefix}${this.id}-enf`, this.compressionFormat)
|
|
619
818
|
]);
|
|
819
|
+
if (results.every((r) => r.status === "fulfilled"))
|
|
820
|
+
this.events.emit("updateData", dataCopy);
|
|
821
|
+
else {
|
|
822
|
+
const error = new Error("Error while saving data to persistent storage: " + results.map((r) => r.status === "rejected" ? r.reason : null).filter(Boolean).join("; "));
|
|
823
|
+
console.error(error);
|
|
824
|
+
this.events.emit("error", error);
|
|
825
|
+
}
|
|
620
826
|
resolve();
|
|
621
827
|
});
|
|
622
828
|
}
|
|
623
829
|
//#region saveDefaultData
|
|
624
|
-
/**
|
|
625
|
-
|
|
830
|
+
/**
|
|
831
|
+
* Saves the default data passed in the constructor synchronously to the in-memory cache and asynchronously to persistent storage.
|
|
832
|
+
* @param emitEvent Whether to emit the `setDefaultData` event - set to `false` to prevent event emission (used internally during initial population in {@linkcode loadData()})
|
|
833
|
+
*/
|
|
834
|
+
async saveDefaultData(emitEvent = true) {
|
|
626
835
|
if (this.memoryCache)
|
|
627
836
|
this.cachedData = this.defaultData;
|
|
628
|
-
await Promise.allSettled([
|
|
837
|
+
const results = await Promise.allSettled([
|
|
629
838
|
this.engine.setValue(`${this.keyPrefix}${this.id}-dat`, await this.engine.serializeData(this.defaultData, this.encodingEnabled())),
|
|
630
839
|
this.engine.setValue(`${this.keyPrefix}${this.id}-ver`, this.formatVersion),
|
|
631
840
|
this.engine.setValue(`${this.keyPrefix}${this.id}-enf`, this.compressionFormat)
|
|
632
841
|
]);
|
|
842
|
+
if (results.every((r) => r.status === "fulfilled"))
|
|
843
|
+
emitEvent && this.events.emit("setDefaultData", this.defaultData);
|
|
844
|
+
else {
|
|
845
|
+
const error = new Error("Error while saving default data to persistent storage: " + results.map((r) => r.status === "rejected" ? r.reason : null).filter(Boolean).join("; "));
|
|
846
|
+
console.error(error);
|
|
847
|
+
this.events.emit("error", error);
|
|
848
|
+
}
|
|
633
849
|
}
|
|
634
850
|
//#region deleteData
|
|
635
851
|
/**
|
|
@@ -645,6 +861,7 @@ var DataStore = class {
|
|
|
645
861
|
this.engine.deleteValue(`${this.keyPrefix}${this.id}-enf`)
|
|
646
862
|
]);
|
|
647
863
|
await ((_b = (_a = this.engine).deleteStorage) == null ? void 0 : _b.call(_a));
|
|
864
|
+
this.events.emit("deleteData");
|
|
648
865
|
}
|
|
649
866
|
//#region encodingEnabled
|
|
650
867
|
/** Returns whether encoding and decoding are enabled for this DataStore instance */
|
|
@@ -665,16 +882,22 @@ var DataStore = class {
|
|
|
665
882
|
let newData = oldData;
|
|
666
883
|
const sortedMigrations = Object.entries(this.migrations).sort(([a], [b]) => Number(a) - Number(b));
|
|
667
884
|
let lastFmtVer = oldFmtVer;
|
|
668
|
-
for (
|
|
885
|
+
for (let i = 0; i < sortedMigrations.length; i++) {
|
|
886
|
+
const [fmtVer, migrationFunc] = sortedMigrations[i];
|
|
669
887
|
const ver = Number(fmtVer);
|
|
670
888
|
if (oldFmtVer < this.formatVersion && oldFmtVer < ver) {
|
|
671
889
|
try {
|
|
672
890
|
const migRes = migrationFunc(newData);
|
|
673
891
|
newData = migRes instanceof Promise ? await migRes : migRes;
|
|
674
892
|
lastFmtVer = oldFmtVer = ver;
|
|
893
|
+
const isFinal = ver >= this.formatVersion || i === sortedMigrations.length - 1;
|
|
894
|
+
this.events.emit("migrateData", ver, newData, isFinal);
|
|
675
895
|
} catch (err) {
|
|
896
|
+
const migError = new MigrationError(`Error while running migration function for format version '${fmtVer}'`, { cause: err });
|
|
897
|
+
this.events.emit("migrationError", ver, migError);
|
|
898
|
+
this.events.emit("error", migError);
|
|
676
899
|
if (!resetOnError)
|
|
677
|
-
throw
|
|
900
|
+
throw migError;
|
|
678
901
|
await this.saveDefaultData();
|
|
679
902
|
return this.engine.deepCopy(this.defaultData);
|
|
680
903
|
}
|
|
@@ -685,10 +908,9 @@ var DataStore = class {
|
|
|
685
908
|
this.engine.setValue(`${this.keyPrefix}${this.id}-ver`, lastFmtVer),
|
|
686
909
|
this.engine.setValue(`${this.keyPrefix}${this.id}-enf`, this.compressionFormat)
|
|
687
910
|
]);
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
return this.engine.deepCopy(newData);
|
|
911
|
+
const result = this.memoryCache ? this.cachedData = this.engine.deepCopy(newData) : this.engine.deepCopy(newData);
|
|
912
|
+
this.events.emit("updateData", result);
|
|
913
|
+
return result;
|
|
692
914
|
}
|
|
693
915
|
//#region migrateId
|
|
694
916
|
/**
|
|
@@ -717,6 +939,7 @@ var DataStore = class {
|
|
|
717
939
|
this.engine.deleteValue(`${this.keyPrefix}${id}-ver`),
|
|
718
940
|
this.engine.deleteValue(`${this.keyPrefix}${id}-enf`)
|
|
719
941
|
]);
|
|
942
|
+
this.events.emit("migrateId", id, this.id);
|
|
720
943
|
}));
|
|
721
944
|
}
|
|
722
945
|
};
|
|
@@ -1085,205 +1308,6 @@ Has: ${checksum}`);
|
|
|
1085
1308
|
}
|
|
1086
1309
|
};
|
|
1087
1310
|
|
|
1088
|
-
// node_modules/.pnpm/nanoevents@9.1.0/node_modules/nanoevents/index.js
|
|
1089
|
-
var createNanoEvents = () => ({
|
|
1090
|
-
emit(event, ...args) {
|
|
1091
|
-
for (let callbacks = this.events[event] || [], i = 0, length = callbacks.length; i < length; i++) {
|
|
1092
|
-
callbacks[i](...args);
|
|
1093
|
-
}
|
|
1094
|
-
},
|
|
1095
|
-
events: {},
|
|
1096
|
-
on(event, cb) {
|
|
1097
|
-
;
|
|
1098
|
-
(this.events[event] ||= []).push(cb);
|
|
1099
|
-
return () => {
|
|
1100
|
-
var _a;
|
|
1101
|
-
this.events[event] = (_a = this.events[event]) == null ? void 0 : _a.filter((i) => cb !== i);
|
|
1102
|
-
};
|
|
1103
|
-
}
|
|
1104
|
-
});
|
|
1105
|
-
|
|
1106
|
-
// lib/NanoEmitter.ts
|
|
1107
|
-
var NanoEmitter = class {
|
|
1108
|
-
events = createNanoEvents();
|
|
1109
|
-
eventUnsubscribes = [];
|
|
1110
|
-
emitterOptions;
|
|
1111
|
-
/** Creates a new instance of NanoEmitter - a lightweight event emitter with helper methods and a strongly typed event map */
|
|
1112
|
-
constructor(options = {}) {
|
|
1113
|
-
this.emitterOptions = {
|
|
1114
|
-
publicEmit: false,
|
|
1115
|
-
...options
|
|
1116
|
-
};
|
|
1117
|
-
}
|
|
1118
|
-
//#region on
|
|
1119
|
-
/**
|
|
1120
|
-
* Subscribes to an event and calls the callback when it's emitted.
|
|
1121
|
-
* @param event The event to subscribe to. Use `as "_"` in case your event names aren't thoroughly typed (like when using a template literal, e.g. \`event-${val}\` as "_")
|
|
1122
|
-
* @returns Returns a function that can be called to unsubscribe the event listener
|
|
1123
|
-
* @example ```ts
|
|
1124
|
-
* const emitter = new NanoEmitter<{
|
|
1125
|
-
* foo: (bar: string) => void;
|
|
1126
|
-
* }>({
|
|
1127
|
-
* publicEmit: true,
|
|
1128
|
-
* });
|
|
1129
|
-
*
|
|
1130
|
-
* let i = 0;
|
|
1131
|
-
* const unsub = emitter.on("foo", (bar) => {
|
|
1132
|
-
* // unsubscribe after 10 events:
|
|
1133
|
-
* if(++i === 10) unsub();
|
|
1134
|
-
* console.log(bar);
|
|
1135
|
-
* });
|
|
1136
|
-
*
|
|
1137
|
-
* emitter.emit("foo", "bar");
|
|
1138
|
-
* ```
|
|
1139
|
-
*/
|
|
1140
|
-
on(event, cb) {
|
|
1141
|
-
let unsub;
|
|
1142
|
-
const unsubProxy = () => {
|
|
1143
|
-
if (!unsub)
|
|
1144
|
-
return;
|
|
1145
|
-
unsub();
|
|
1146
|
-
this.eventUnsubscribes = this.eventUnsubscribes.filter((u) => u !== unsub);
|
|
1147
|
-
};
|
|
1148
|
-
unsub = this.events.on(event, cb);
|
|
1149
|
-
this.eventUnsubscribes.push(unsub);
|
|
1150
|
-
return unsubProxy;
|
|
1151
|
-
}
|
|
1152
|
-
//#region once
|
|
1153
|
-
/**
|
|
1154
|
-
* Subscribes to an event and calls the callback or resolves the Promise only once when it's emitted.
|
|
1155
|
-
* @param event The event to subscribe to. Use `as "_"` in case your event names aren't thoroughly typed (like when using a template literal, e.g. \`event-${val}\` as "_")
|
|
1156
|
-
* @param cb The callback to call when the event is emitted - if provided or not, the returned Promise will resolve with the event arguments
|
|
1157
|
-
* @returns Returns a Promise that resolves with the event arguments when the event is emitted
|
|
1158
|
-
* @example ```ts
|
|
1159
|
-
* const emitter = new NanoEmitter<{
|
|
1160
|
-
* foo: (bar: string) => void;
|
|
1161
|
-
* }>();
|
|
1162
|
-
*
|
|
1163
|
-
* // Promise syntax:
|
|
1164
|
-
* const [bar] = await emitter.once("foo");
|
|
1165
|
-
* console.log(bar);
|
|
1166
|
-
*
|
|
1167
|
-
* // Callback syntax:
|
|
1168
|
-
* emitter.once("foo", (bar) => console.log(bar));
|
|
1169
|
-
* ```
|
|
1170
|
-
*/
|
|
1171
|
-
once(event, cb) {
|
|
1172
|
-
return new Promise((resolve) => {
|
|
1173
|
-
let unsub;
|
|
1174
|
-
const onceProxy = ((...args) => {
|
|
1175
|
-
cb == null ? void 0 : cb(...args);
|
|
1176
|
-
unsub == null ? void 0 : unsub();
|
|
1177
|
-
resolve(args);
|
|
1178
|
-
});
|
|
1179
|
-
unsub = this.events.on(event, onceProxy);
|
|
1180
|
-
this.eventUnsubscribes.push(unsub);
|
|
1181
|
-
});
|
|
1182
|
-
}
|
|
1183
|
-
//#region onMulti
|
|
1184
|
-
/**
|
|
1185
|
-
* Allows subscribing to multiple events and calling the callback only when one of, all of, or a subset of the events are emitted, either continuously or only once.
|
|
1186
|
-
* @param options An object or array of objects with the following properties:
|
|
1187
|
-
* `callback` (required) is the function that will be called when the conditions are met.
|
|
1188
|
-
*
|
|
1189
|
-
* Set `once` to true to call the callback only once for the first event (or set of events) that match the criteria, then stop listening.
|
|
1190
|
-
* If `signal` is provided, the subscription will be canceled when the given signal is aborted.
|
|
1191
|
-
*
|
|
1192
|
-
* If `oneOf` is used, the callback will be called when any of the matching events are emitted.
|
|
1193
|
-
* If `allOf` is used, the callback will be called after all of the matching events are emitted at least once, then any time any of them are emitted.
|
|
1194
|
-
* If both `oneOf` and `allOf` are used together, the callback will be called when any of the `oneOf` events are emitted AND all of the `allOf` events have been emitted at least once.
|
|
1195
|
-
* At least one of `oneOf` or `allOf` must be provided.
|
|
1196
|
-
*
|
|
1197
|
-
* @returns Returns a function that can be called to unsubscribe all listeners created by this call. Alternatively, pass an `AbortSignal` to all options objects to achieve the same effect or for finer control.
|
|
1198
|
-
*/
|
|
1199
|
-
onMulti(options) {
|
|
1200
|
-
const allUnsubs = [];
|
|
1201
|
-
const unsubAll = () => {
|
|
1202
|
-
for (const unsub of allUnsubs)
|
|
1203
|
-
unsub();
|
|
1204
|
-
allUnsubs.splice(0, allUnsubs.length);
|
|
1205
|
-
this.eventUnsubscribes = this.eventUnsubscribes.filter((u) => !allUnsubs.includes(u));
|
|
1206
|
-
};
|
|
1207
|
-
for (const opts of Array.isArray(options) ? options : [options]) {
|
|
1208
|
-
const optsWithDefaults = {
|
|
1209
|
-
allOf: [],
|
|
1210
|
-
oneOf: [],
|
|
1211
|
-
once: false,
|
|
1212
|
-
...opts
|
|
1213
|
-
};
|
|
1214
|
-
const {
|
|
1215
|
-
oneOf,
|
|
1216
|
-
allOf,
|
|
1217
|
-
once,
|
|
1218
|
-
signal,
|
|
1219
|
-
callback
|
|
1220
|
-
} = optsWithDefaults;
|
|
1221
|
-
if (signal == null ? void 0 : signal.aborted)
|
|
1222
|
-
return unsubAll;
|
|
1223
|
-
if (oneOf.length === 0 && allOf.length === 0)
|
|
1224
|
-
throw new TypeError("NanoEmitter.onMulti(): Either `oneOf` or `allOf` or both must be provided in the options");
|
|
1225
|
-
const curEvtUnsubs = [];
|
|
1226
|
-
const checkUnsubAllEvt = (force = false) => {
|
|
1227
|
-
if (!(signal == null ? void 0 : signal.aborted) && !force)
|
|
1228
|
-
return;
|
|
1229
|
-
for (const unsub of curEvtUnsubs)
|
|
1230
|
-
unsub();
|
|
1231
|
-
curEvtUnsubs.splice(0, curEvtUnsubs.length);
|
|
1232
|
-
this.eventUnsubscribes = this.eventUnsubscribes.filter((u) => !curEvtUnsubs.includes(u));
|
|
1233
|
-
};
|
|
1234
|
-
const allOfEmitted = /* @__PURE__ */ new Set();
|
|
1235
|
-
const allOfConditionMet = () => allOf.length === 0 || allOfEmitted.size === allOf.length;
|
|
1236
|
-
for (const event of oneOf) {
|
|
1237
|
-
const unsub = this.events.on(event, ((...args) => {
|
|
1238
|
-
checkUnsubAllEvt();
|
|
1239
|
-
if (allOfConditionMet()) {
|
|
1240
|
-
callback(event, ...args);
|
|
1241
|
-
if (once)
|
|
1242
|
-
checkUnsubAllEvt(true);
|
|
1243
|
-
}
|
|
1244
|
-
}));
|
|
1245
|
-
curEvtUnsubs.push(unsub);
|
|
1246
|
-
}
|
|
1247
|
-
for (const event of allOf) {
|
|
1248
|
-
const unsub = this.events.on(event, ((...args) => {
|
|
1249
|
-
checkUnsubAllEvt();
|
|
1250
|
-
allOfEmitted.add(event);
|
|
1251
|
-
if (allOfConditionMet() && (oneOf.length === 0 || oneOf.includes(event))) {
|
|
1252
|
-
callback(event, ...args);
|
|
1253
|
-
if (once)
|
|
1254
|
-
checkUnsubAllEvt(true);
|
|
1255
|
-
}
|
|
1256
|
-
}));
|
|
1257
|
-
curEvtUnsubs.push(unsub);
|
|
1258
|
-
}
|
|
1259
|
-
allUnsubs.push(() => checkUnsubAllEvt(true));
|
|
1260
|
-
}
|
|
1261
|
-
return unsubAll;
|
|
1262
|
-
}
|
|
1263
|
-
//#region emit
|
|
1264
|
-
/**
|
|
1265
|
-
* Emits an event on this instance.
|
|
1266
|
-
* - ⚠️ Needs `publicEmit` to be set to true in the NanoEmitter constructor or super() call!
|
|
1267
|
-
* @param event The event to emit
|
|
1268
|
-
* @param args The arguments to pass to the event listeners
|
|
1269
|
-
* @returns Returns true if `publicEmit` is true and the event was emitted successfully
|
|
1270
|
-
*/
|
|
1271
|
-
emit(event, ...args) {
|
|
1272
|
-
if (this.emitterOptions.publicEmit) {
|
|
1273
|
-
this.events.emit(event, ...args);
|
|
1274
|
-
return true;
|
|
1275
|
-
}
|
|
1276
|
-
return false;
|
|
1277
|
-
}
|
|
1278
|
-
//#region unsubscribeAll
|
|
1279
|
-
/** Unsubscribes all event listeners from this instance */
|
|
1280
|
-
unsubscribeAll() {
|
|
1281
|
-
for (const unsub of this.eventUnsubscribes)
|
|
1282
|
-
unsub();
|
|
1283
|
-
this.eventUnsubscribes = [];
|
|
1284
|
-
}
|
|
1285
|
-
};
|
|
1286
|
-
|
|
1287
1311
|
// lib/Debouncer.ts
|
|
1288
1312
|
var Debouncer = class extends NanoEmitter {
|
|
1289
1313
|
/**
|