Skip to content

Commit

Permalink
chore(core): decrease bundle-size
Browse files Browse the repository at this point in the history
  • Loading branch information
josepot committed Oct 20, 2020
1 parent 14398b3 commit 2780e17
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/Subscribe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export const Subscribe: React.FC<{
useEffect(() => {
const subscription = source$.subscribe()
setMounted(1)
return () => subscription.unsubscribe()
return () => {
subscription.unsubscribe()
}
}, [source$])
return <>{mounted ? children : fallback}</>
}
6 changes: 3 additions & 3 deletions packages/core/src/internal/react-enhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BehaviorObservable } from "./BehaviorObservable"
import { EMPTY_VALUE } from "./empty-value"

const reactEnhancer = <T>(source$: BehaviorObservable<T>): (() => T) => {
let promise: Promise<T | void> | undefined
let promise: Promise<T | void> | null
let error: any = EMPTY_VALUE

return (): T => {
Expand Down Expand Up @@ -46,11 +46,11 @@ const reactEnhancer = <T>(source$: BehaviorObservable<T>): (() => T) => {
subscription.unsubscribe()
}
}).finally(() => {
promise = undefined
promise = null
})

if (value !== EMPTY_VALUE) {
promise = undefined
promise = null
return value
}

Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/internal/share-latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const shareLatest = <T>(
shouldComplete = true,
teardown = noop,
): BehaviorObservable<T> => {
let subject: Subject<T> | undefined
let subscription: Subscription | undefined | null
let subject: Subject<T> | null
let subscription: Subscription | null
let refCount = 0
let currentValue: T = EMPTY_VALUE

Expand All @@ -25,16 +25,16 @@ const shareLatest = <T>(
},
(err) => {
const _subject = subject
subscription = undefined
subject = undefined
subscription = null
subject = null
_subject!.error(err)
},
() => {
subscription = undefined
subscription = null
shouldComplete && subject!.complete()
},
)
if (subscription.closed) subscription = undefined
if (subscription.closed) subscription = null
} else {
innerSub = subject.subscribe(subscriber)
if (currentValue !== EMPTY_VALUE) {
Expand All @@ -51,8 +51,8 @@ const shareLatest = <T>(
subscription.unsubscribe()
}
teardown()
subject = undefined
subscription = undefined
subject = null
subscription = null
}
}
}) as BehaviorObservable<T>
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/internal/useObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export const useObservable = <O>(
})
}

let subscription = source$.subscribe((val) => (syncVal = val), onError)
let subscription = source$.subscribe((val) => {
syncVal = val
}, onError)
if (err !== EMPTY_VALUE) return

const set = (val: O | (() => O)) => {
Expand All @@ -37,7 +39,9 @@ export const useObservable = <O>(
}, onError)
t.unsubscribe()

return () => subscription.unsubscribe()
return () => {
subscription.unsubscribe()
}
}, keys)

return state as Exclude<O, typeof SUSPENSE>
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/useSubscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { useEffect } from "react"
export const useSubscribe = <T>(source$: Observable<T>) => {
useEffect(() => {
const subscription = source$.subscribe()
return () => subscription.unsubscribe()
return () => {
subscription.unsubscribe()
}
}, [source$])
}

0 comments on commit 2780e17

Please sign in to comment.