Skip to content

Commit

Permalink
feat(util): arrow fn and untilEvent types
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Jan 30, 2023
1 parent cae2659 commit ced1de6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"url": "https://github.com/AliMD/alwatr/issues"
},
"dependencies": {
"tslib": "^2.4.1"
"tslib": "^2.5.0"
}
}
23 changes: 13 additions & 10 deletions core/util/src/delay.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
export function untilEvent(element: Element, eventName: string): Promise<Event> {
return new Promise((resolve) => element.addEventListener(eventName, resolve));
}

export function delay(duration: number): Promise<void> {
export const delay = (duration: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, duration));
}
};

export function untilNextFrame(): Promise<DOMHighResTimeStamp> {
export const untilNextFrame = (): Promise<DOMHighResTimeStamp> => {
return new Promise((resolve) => requestAnimationFrame(resolve));
}
};

export function untilIdle(timeout?: number): Promise<IdleDeadline> {
export const untilIdle = (timeout?: number): Promise<IdleDeadline> => {
return new Promise((resolve) => requestIdleCallback(resolve, {timeout}));
}
};

export const untilEvent = <T extends keyof HTMLElementEventMap>(
element: HTMLElement,
eventName: T,
): Promise<HTMLElementEventMap[T]> => {
return new Promise((resolve) => element.addEventListener(eventName, resolve, {once: true, passive: true}));
};

0 comments on commit ced1de6

Please sign in to comment.