Ver código fonte

ref: minor refactors

Sv443 3 semanas atrás
pai
commit
56038841e7
4 arquivos alterados com 14 adições e 5 exclusões
  1. 7 2
      lib/DataStoreSerializer.ts
  2. 1 1
      lib/Debouncer.ts
  3. 2 2
      lib/NanoEmitter.ts
  4. 4 0
      lib/math.ts

+ 7 - 2
lib/DataStoreSerializer.ts

@@ -146,7 +146,7 @@ export class DataStoreSerializer {
   public async deserializePartial(stores: StoreFilter, data: string | SerializedDataStore[]): Promise<void> {
   public async deserializePartial(stores: StoreFilter, data: string | SerializedDataStore[]): Promise<void> {
     const deserStores: SerializedDataStore[] = typeof data === "string" ? JSON.parse(data) : data;
     const deserStores: SerializedDataStore[] = typeof data === "string" ? JSON.parse(data) : data;
 
 
-    if(!Array.isArray(deserStores) || !deserStores.every(DataStoreSerializer.isSerializedDataStore))
+    if(!Array.isArray(deserStores) || !deserStores.every(DataStoreSerializer.isSerializedDataStoreObj))
       throw new TypeError("Invalid serialized data format! Expected an array of SerializedDataStore objects.");
       throw new TypeError("Invalid serialized data format! Expected an array of SerializedDataStore objects.");
 
 
     for(const storeData of deserStores.filter(s => typeof stores === "function" ? stores(s.id) : stores.includes(s.id))) {
     for(const storeData of deserStores.filter(s => typeof stores === "function" ? stores(s.id) : stores.includes(s.id))) {
@@ -216,8 +216,13 @@ export class DataStoreSerializer {
     );
     );
   }
   }
 
 
+  /** Checks if a given value is an array of SerializedDataStore objects */
+  public static isSerializedDataStoreObjArray(obj: unknown): obj is SerializedDataStore[] {
+    return Array.isArray(obj) && obj.every((o) => typeof o === "object" && o !== null && "id" in o && "data" in o && "formatVersion" in o && "encoded" in o);
+  }
+
   /** Checks if a given value is a SerializedDataStore object */
   /** Checks if a given value is a SerializedDataStore object */
-  public static isSerializedDataStore(obj: unknown): obj is SerializedDataStore {
+  public static isSerializedDataStoreObj(obj: unknown): obj is SerializedDataStore {
     return typeof obj === "object" && obj !== null && "id" in obj && "data" in obj && "formatVersion" in obj && "encoded" in obj;
     return typeof obj === "object" && obj !== null && "id" in obj && "data" in obj && "formatVersion" in obj && "encoded" in obj;
   }
   }
 
 

+ 1 - 1
lib/Debouncer.ts

@@ -123,7 +123,7 @@ export class Debouncer<TFunc extends AnyFunc> extends NanoEmitter<DebouncerEvent
     const cl = (...a: Parameters<TFunc>): void => {
     const cl = (...a: Parameters<TFunc>): void => {
       this.queuedCall = undefined;
       this.queuedCall = undefined;
       this.emit("call", ...a);
       this.emit("call", ...a);
-      this.listeners.forEach((l) => l.apply(this, a));
+      this.listeners.forEach((l) => l.call(this, ...a));
     };
     };
 
 
     /** Sets a timeout that will call the latest queued call and then set another timeout if there was a queued call */
     /** Sets a timeout that will call the latest queued call and then set another timeout if there was a queued call */

+ 2 - 2
lib/NanoEmitter.ts

@@ -48,12 +48,12 @@ export class NanoEmitter<TEvtMap extends EventsMap = DefaultEvents> {
       let unsub: Unsubscribe | undefined;
       let unsub: Unsubscribe | undefined;
 
 
       const onceProxy = ((...args: Parameters<TEvtMap[TKey]>) => {
       const onceProxy = ((...args: Parameters<TEvtMap[TKey]>) => {
-        unsub!();
         cb?.(...args);
         cb?.(...args);
+        unsub?.();
         resolve(args);
         resolve(args);
       }) as TEvtMap[TKey];
       }) as TEvtMap[TKey];
 
 
-      unsub = this.on(event, onceProxy);
+      unsub = this.events.on(event, onceProxy);
     });
     });
   }
   }
 
 

+ 4 - 0
lib/math.ts

@@ -28,6 +28,10 @@ export function mapRange(value: number, range1min: number, range1max: number, ra
  * For example, you can map the value 2 in the range of 0-5 to the range of 0-10 and you'd get a 4 as a result.
  * For example, you can map the value 2 in the range of 0-5 to the range of 0-10 and you'd get a 4 as a result.
  */
  */
 export function mapRange(value: number, range1max: number, range2max: number): number;
 export function mapRange(value: number, range1max: number, range2max: number): number;
+/**
+ * Transforms the value parameter from one numerical range to another.  
+ * For example, you can map the value 2 in the range of 0-5 to the range of 0-10 and you'd get a 4 as a result.
+ */
 export function mapRange(value: number, range1min: number, range1max: number, range2min?: number, range2max?: number): number {
 export function mapRange(value: number, range1min: number, range1max: number, range2min?: number, range2max?: number): number {
   if(typeof range2min === "undefined" || typeof range2max === "undefined") {
   if(typeof range2min === "undefined" || typeof range2max === "undefined") {
     range2max = range1max;
     range2max = range1max;