-
-
Notifications
You must be signed in to change notification settings - Fork 638
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
fix(utils): Initial data for atomWithObservable #1058
Changes from 3 commits
3c93b7c
0182706
14aca92
1ea4c7b
4eb0d3b
5fa37fd
e8f976b
2686800
a55f7e3
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -29,69 +29,52 @@ type ObservableLike<T> = { | |||||||||
|
||||||||||
type SubjectLike<T> = ObservableLike<T> & Observer<T> | ||||||||||
|
||||||||||
type InitialDataFunction<T> = () => T | undefined | ||||||||||
|
||||||||||
export type AtomWithObservableOptions<TData> = { | ||||||||||
initialData?: TData | InitialDataFunction<TData> | ||||||||||
} | ||||||||||
export type CreateObservableOptions<TData> = { | ||||||||||
initialData?: TData | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we name it
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed everywhere to initialValue |
||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this. Leftover?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, accidentally copied that from the previous pr. Thanks for noticing |
||||||||||
|
||||||||||
export function atomWithObservable<TData>( | ||||||||||
createObservable: (get: Getter) => SubjectLike<TData> | ||||||||||
createObservable: (get: Getter) => SubjectLike<TData>, | ||||||||||
options?: AtomWithObservableOptions<TData> | ||||||||||
): WritableAtom<TData, TData> | ||||||||||
|
||||||||||
export function atomWithObservable<TData>( | ||||||||||
createObservable: (get: Getter) => ObservableLike<TData> | ||||||||||
createObservable: (get: Getter) => ObservableLike<TData>, | ||||||||||
options?: AtomWithObservableOptions<TData> | ||||||||||
): Atom<TData> | ||||||||||
|
||||||||||
export function atomWithObservable<TData>( | ||||||||||
createObservable: (get: Getter) => ObservableLike<TData> | SubjectLike<TData> | ||||||||||
createObservable: (get: Getter) => ObservableLike<TData> | SubjectLike<TData>, | ||||||||||
options?: AtomWithObservableOptions<TData> | ||||||||||
) { | ||||||||||
const observableResultAtom = atom((get) => { | ||||||||||
let settlePromise: ((data: TData | null, err?: unknown) => void) | null = | ||||||||||
null | ||||||||||
let observable = createObservable(get) | ||||||||||
const itself = observable[Symbol.observable]?.() | ||||||||||
if (itself) { | ||||||||||
observable = itself | ||||||||||
} | ||||||||||
const dataAtom = atom<TData | Promise<TData>>( | ||||||||||
new Promise<TData>((resolve, reject) => { | ||||||||||
settlePromise = (data, err) => { | ||||||||||
if (err) { | ||||||||||
reject(err) | ||||||||||
} else { | ||||||||||
resolve(data as TData) | ||||||||||
} | ||||||||||
} | ||||||||||
}) | ||||||||||
|
||||||||||
const dataAtom = atom( | ||||||||||
options?.initialData | ||||||||||
? getInitialData(options) | ||||||||||
: firstValueFrom(observable) | ||||||||||
) | ||||||||||
let setData: (data: TData | Promise<TData>) => void = () => { | ||||||||||
throw new Error('setting data without mount') | ||||||||||
} | ||||||||||
const dataListener = (data: TData) => { | ||||||||||
if (settlePromise) { | ||||||||||
settlePromise(data) | ||||||||||
settlePromise = null | ||||||||||
if (subscription && !setData) { | ||||||||||
subscription.unsubscribe() | ||||||||||
subscription = null | ||||||||||
} | ||||||||||
} else { | ||||||||||
setData(data) | ||||||||||
} | ||||||||||
setData(data) | ||||||||||
} | ||||||||||
const errorListener = (error: unknown) => { | ||||||||||
if (settlePromise) { | ||||||||||
settlePromise(null, error) | ||||||||||
settlePromise = null | ||||||||||
if (subscription && !setData) { | ||||||||||
subscription.unsubscribe() | ||||||||||
subscription = null | ||||||||||
} | ||||||||||
} else { | ||||||||||
setData(Promise.reject<TData>(error)) | ||||||||||
} | ||||||||||
setData(Promise.reject<TData>(error)) | ||||||||||
} | ||||||||||
let subscription: Subscription | null = null | ||||||||||
subscription = observable.subscribe(dataListener, errorListener) | ||||||||||
if (!settlePromise) { | ||||||||||
subscription.unsubscribe() | ||||||||||
subscription = null | ||||||||||
} | ||||||||||
|
||||||||||
dataAtom.onMount = (update) => { | ||||||||||
setData = update | ||||||||||
if (!subscription) { | ||||||||||
|
@@ -107,6 +90,7 @@ export function atomWithObservable<TData>( | |||||||||
const observableAtom = atom( | ||||||||||
(get) => { | ||||||||||
const { dataAtom } = get(observableResultAtom) | ||||||||||
|
||||||||||
return get(dataAtom) | ||||||||||
}, | ||||||||||
(get, _set, data: TData) => { | ||||||||||
|
@@ -120,3 +104,32 @@ export function atomWithObservable<TData>( | |||||||||
) | ||||||||||
return observableAtom | ||||||||||
} | ||||||||||
|
||||||||||
function getInitialData<TData>(options: AtomWithObservableOptions<TData>) { | ||||||||||
const initialData = options.initialData | ||||||||||
return initialData instanceof Function ? initialData() : initialData | ||||||||||
} | ||||||||||
|
||||||||||
function firstValueFrom<T>(source: ObservableLike<T>): Promise<T> { | ||||||||||
return new Promise<T>((resolve, reject) => { | ||||||||||
let resolved = false | ||||||||||
const subscription = source.subscribe({ | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although this looks cleaner, I think there's a risk of memory leaks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dai-shi hmm, I thought that the suspended component is not mounted before the promise resolves. So, we don't actually have an option to unsubscribe in any case or do I miss something? To illustrate, here is an atom that suspends and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you are right about it. The risk is actually the case In jotai/src/urql/atomWithSubscription.ts Lines 86 to 89 in 2c751b0
Given that this isn't ideal, and this is such a rare case anyway. I think I can merge this PR. Maybe, we leave some comments for the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW I've already changed other things. Should I do anything else here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a comment like this: // FIXME this implementation is not fully compatible with concurrent rendering.
// we need to deal with the case `onMount` is not invoked after the atom is initialized. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dai-shi I've added the comment. Please have a look |
||||||||||
next: (value) => { | ||||||||||
resolve(value) | ||||||||||
resolved = true | ||||||||||
if (subscription) { | ||||||||||
subscription.unsubscribe() | ||||||||||
} | ||||||||||
}, | ||||||||||
error: reject, | ||||||||||
complete: () => { | ||||||||||
reject() | ||||||||||
}, | ||||||||||
}) | ||||||||||
|
||||||||||
if (resolved) { | ||||||||||
// If subscription was resolved synchronously | ||||||||||
subscription.unsubscribe() | ||||||||||
} | ||||||||||
}) | ||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's a bit controversial, but we prefer not exporting types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure