Formatting
This commit is contained in:
@@ -187,7 +187,11 @@ export class EventEmitter<T extends EventMap> {
|
|||||||
* @param timeoutMs - The timeout in milliseconds.
|
* @param timeoutMs - The timeout in milliseconds.
|
||||||
* @returns The event payload.
|
* @returns The event payload.
|
||||||
*/
|
*/
|
||||||
async waitFor<K extends keyof T>(type: K, predicate: (payload: DeeplyReadonly<T[K]>) => boolean, timeoutMs?: number): Promise<DeeplyReadonly<T[K]>> {
|
async waitFor<K extends keyof T>(
|
||||||
|
type: K,
|
||||||
|
predicate: (payload: DeeplyReadonly<T[K]>) => boolean,
|
||||||
|
timeoutMs?: number,
|
||||||
|
): Promise<DeeplyReadonly<T[K]>> {
|
||||||
// Create a promise to wait for the event to be emitted.
|
// Create a promise to wait for the event to be emitted.
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
|||||||
+7
-10
@@ -1,4 +1,4 @@
|
|||||||
import { DeeplyReadonly } from "./types";
|
import type { DeeplyReadonly } from './types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate the value is within the bounds, returning true if it is within the bounds, false otherwise
|
* Validate the value is within the bounds, returning true if it is within the bounds, false otherwise
|
||||||
@@ -39,20 +39,17 @@ export const tryAsync = async (fn: () => unknown, onError?: (error: Error) => vo
|
|||||||
* @returns The frozen object.
|
* @returns The frozen object.
|
||||||
*/
|
*/
|
||||||
export const deepFreeze = <T>(value: T): DeeplyReadonly<T> => {
|
export const deepFreeze = <T>(value: T): DeeplyReadonly<T> => {
|
||||||
if (
|
if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
|
||||||
value !== null &&
|
|
||||||
(typeof value === 'object' || typeof value === 'function')
|
|
||||||
) {
|
|
||||||
for (const key of Reflect.ownKeys(value)) {
|
for (const key of Reflect.ownKeys(value)) {
|
||||||
const descriptor = Reflect.getOwnPropertyDescriptor(value, key);
|
const descriptor = Reflect.getOwnPropertyDescriptor(value, key);
|
||||||
|
|
||||||
if (descriptor && 'value' in descriptor) {
|
if (descriptor && 'value' in descriptor) {
|
||||||
deepFreeze(descriptor.value);
|
deepFreeze(descriptor.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.freeze(value);
|
Object.freeze(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
};
|
||||||
|
|||||||
+2
-5
@@ -4,8 +4,5 @@
|
|||||||
* @returns The deeply readonly type.
|
* @returns The deeply readonly type.
|
||||||
*/
|
*/
|
||||||
export type DeeplyReadonly<T> = {
|
export type DeeplyReadonly<T> = {
|
||||||
readonly [K in keyof T]:
|
readonly [K in keyof T]: T[K] extends (...args: never[]) => unknown ? T[K] : DeeplyReadonly<T[K]>;
|
||||||
T[K] extends (...args: never[]) => unknown
|
};
|
||||||
? T[K]
|
|
||||||
: DeeplyReadonly<T[K]>;
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { EventEmitter } from '../source/event-emitter.ts';
|
|||||||
type TestEvents = {
|
type TestEvents = {
|
||||||
message: string;
|
message: string;
|
||||||
count: number;
|
count: number;
|
||||||
|
nested: { nested: { value: number } };
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user