-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
90 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,51 @@ | ||
import type { Selection } from '../Selection'; | ||
|
||
export class RetryEvent<TData> extends Event { | ||
constructor( | ||
readonly promise: Promise<TData>, | ||
readonly selections: Set<Selection>, | ||
readonly isLastTry = false | ||
) { | ||
super('gqty$retry'); | ||
} | ||
} | ||
const retryEventListeners = new Set<(event: RetryEvent) => void>(); | ||
|
||
export type RetryEvent = { | ||
readonly promise: Promise<unknown>; | ||
readonly selections: Set<Selection>; | ||
readonly isLastTry: boolean; | ||
}; | ||
|
||
export const notifyRetry = ( | ||
promise: Promise<unknown>, | ||
selections: Set<Selection>, | ||
isLastTry = false | ||
) => { | ||
globalThis.dispatchEvent?.(new RetryEvent(promise, selections, isLastTry)); | ||
for (const listener of retryEventListeners) { | ||
listener({ promise, selections, isLastTry }); | ||
} | ||
}; | ||
|
||
export const subscribeRetry = ( | ||
callback: (event: RetryEvent<unknown>) => void | ||
) => { | ||
// @ts-expect-error A hack for useMetaState() | ||
globalThis.addEventListener?.('gqty$retry', callback); | ||
export const subscribeRetry = (callback: (event: RetryEvent) => void) => { | ||
retryEventListeners.add(callback); | ||
|
||
return () => { | ||
// @ts-expect-error A hack for useMetaState() | ||
globalThis.removeEventListener?.('gqty$retry', callback); | ||
retryEventListeners.delete(callback); | ||
}; | ||
}; | ||
|
||
export class Fetchevent<TData> extends Event { | ||
constructor( | ||
readonly promise: Promise<TData>, | ||
readonly selections: Set<Selection> | ||
) { | ||
super('gqty$fetch'); | ||
} | ||
} | ||
const fetchEventListeners = new Set<(event: FetchEvent) => void>(); | ||
|
||
export type FetchEvent = { | ||
readonly promise: Promise<unknown>; | ||
readonly selections: Set<Selection>; | ||
}; | ||
|
||
export const notifyFetch = ( | ||
promise: Promise<unknown>, | ||
selections: Set<Selection> | ||
) => { | ||
globalThis.dispatchEvent?.(new Fetchevent(promise, selections)); | ||
for (const listener of fetchEventListeners) { | ||
listener({ promise, selections }); | ||
} | ||
}; | ||
|
||
export const subscribeFetch = ( | ||
callback: (event: Fetchevent<unknown>) => void | ||
) => { | ||
// @ts-expect-error A hack for useMetaState() | ||
globalThis.addEventListener?.('gqty$fetch', callback); | ||
export const subscribeFetch = (callback: (event: FetchEvent) => void) => { | ||
fetchEventListeners.add(callback); | ||
|
||
return () => { | ||
// @ts-expect-error A hack for useMetaState() | ||
globalThis.removeEventListener?.('gqty$fetch', callback); | ||
fetchEventListeners.delete(callback); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.