Skip to content

Commit

Permalink
chore(core/useObservable): improve implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
josepot committed Nov 11, 2020
1 parent 4d9f5dd commit 611775e
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions packages/core/src/internal/useObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,19 @@ export const useObservable = <O>(
}, onError)
if (err !== EMPTY_VALUE) return

const set = (value: O | (() => O)) => {
const set = (value: O) => {
if (!Object.is(prevStateRef.current, value)) {
prevStateRef.current = value
if (typeof value === "function") {
setState(() => [(value as any)(), source$])
} else {
setState([value, source$])
}
setState([(prevStateRef.current = value), source$])
}
}

if (syncVal === EMPTY_VALUE) {
set(defaultValue)
}
if (syncVal === EMPTY_VALUE) set(defaultValue)

const t = subscription
subscription = source$.subscribe((value: O | typeof SUSPENSE) => {
set(value === SUSPENSE ? gV : value)
if (value === SUSPENSE) {
setState(gV as any)
} else set(value)
}, onError)
t.unsubscribe()

Expand Down

0 comments on commit 611775e

Please sign in to comment.