-
Notifications
You must be signed in to change notification settings - Fork 97
refactor(react): Simplify onError and onSuccess handlers in lazy loading #1945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/stable-lazy
Are you sure you want to change the base?
Changes from 1 commit
92edcc3
ac28989
73fcb86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| 'use client' | ||
| import { type ComponentType, type LazyExoticComponent, lazy as originalLazy } from 'react' | ||
|
|
||
| export const reloadOnErrorStorageKeyAccessKeySymbol = Symbol('reloadOnErrorStorageKeyAccessKey') | ||
|
|
||
| interface LazyOptions { | ||
| onSuccess?: ({ load }: { load: () => Promise<{ default: ComponentType<any> }> }) => void | ||
| onError?: ({ error, load }: { error: unknown; load: () => Promise<{ default: ComponentType<any> }> }) => void | ||
| onSuccess?: () => void | ||
| onError?: (options: { error: unknown }) => void | ||
| } | ||
|
Comment on lines
6
to
9
|
||
|
|
||
| /** | ||
|
|
@@ -47,14 +49,31 @@ export const createLazy = | |
| ): LazyExoticComponent<T> & { | ||
| load: () => Promise<{ default: T }> | ||
| } => { | ||
| const storageKey = load.toString() | ||
|
|
||
| const composedOnSuccess = () => { | ||
| options?.onSuccess?.({ load }) | ||
| defaultOptions.onSuccess?.({ load }) | ||
| ;(options?.onSuccess as undefined | ((arg: { [reloadOnErrorStorageKeyAccessKeySymbol]: string }) => void))?.({ | ||
| [reloadOnErrorStorageKeyAccessKeySymbol]: storageKey, | ||
|
||
| }) | ||
| ;( | ||
| defaultOptions.onSuccess as undefined | ((arg: { [reloadOnErrorStorageKeyAccessKeySymbol]: string }) => void) | ||
| )?.({ [reloadOnErrorStorageKeyAccessKeySymbol]: storageKey }) | ||
| } | ||
|
|
||
| const composedOnError = (error: unknown) => { | ||
| options?.onError?.({ error, load }) | ||
| defaultOptions.onError?.({ error, load }) | ||
| ;( | ||
| options?.onError as | ||
| | undefined | ||
| | ((arg: { error: unknown; [reloadOnErrorStorageKeyAccessKeySymbol]: string }) => void) | ||
| )?.({ | ||
| error, | ||
| [reloadOnErrorStorageKeyAccessKeySymbol]: storageKey, | ||
| }) | ||
| ;( | ||
| defaultOptions.onError as | ||
| | undefined | ||
| | ((arg: { error: unknown; [reloadOnErrorStorageKeyAccessKeySymbol]: string }) => void) | ||
| )?.({ error, [reloadOnErrorStorageKeyAccessKeySymbol]: storageKey }) | ||
| } | ||
|
|
||
| return Object.assign( | ||
|
|
@@ -188,14 +207,14 @@ export const reloadOnError = ({ | |
|
|
||
| return { | ||
| ...options, | ||
| onSuccess: ({ load }) => { | ||
| options.onSuccess?.({ load }) | ||
| reloadStorage.removeItem(load.toString()) | ||
| }, | ||
| onError: ({ error, load }) => { | ||
| options.onError?.({ error, load }) | ||
|
|
||
| const storageKey = load.toString() | ||
| onSuccess: ((arg: any) => { | ||
| ;(options.onSuccess as any)?.(arg) | ||
| const storageKey = arg?.[reloadOnErrorStorageKeyAccessKeySymbol] as string | ||
| reloadStorage.removeItem(storageKey) | ||
| }) as LazyOptions['onSuccess'], | ||
|
Comment on lines
+208
to
+212
|
||
| onError: (errorOptions: { error: unknown }) => { | ||
| options.onError?.(errorOptions) | ||
| const storageKey = (errorOptions as any)[reloadOnErrorStorageKeyAccessKeySymbol] as string | ||
| let currentRetryCount = 0 | ||
|
|
||
|
Comment on lines
+213
to
217
|
||
| if (typeof retry === 'number') { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.