Skip to content

Commit 6191786

Browse files
authored
Merge pull request #97 from jeetiss/use-constant
Reduce object creation by useConstant
2 parents ea5006d + 9b86698 commit 6191786

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"webpack-dev-server": "^3.9.0"
5757
},
5858
"dependencies": {
59-
"tslib": "^1.10.0"
59+
"tslib": "^1.10.0",
60+
"use-constant": "^1.0.0"
6061
},
6162
"lint-staged": {
6263
"*.js": [

src/use-event-callback.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useState, useCallback } from 'react'
2+
import useConstant from 'use-constant'
23
import { Observable, BehaviorSubject, Subject } from 'rxjs'
34

45
import { RestrictArray, VoidAsNull, Not } from './type'
@@ -7,11 +8,11 @@ export type VoidableEventCallback<EventValue> = EventValue extends void ? () =>
78

89
export type EventCallbackState<EventValue, State, Inputs = void> = [
910
VoidableEventCallback<EventValue>,
10-
[State extends void ? null : State, BehaviorSubject<State | null>, BehaviorSubject<RestrictArray<Inputs> | null>]
11+
[State extends void ? null : State, BehaviorSubject<State | null>, BehaviorSubject<RestrictArray<Inputs> | null>],
1112
]
1213
export type ReturnedState<EventValue, State, Inputs> = [
1314
EventCallbackState<EventValue, State, Inputs>[0],
14-
EventCallbackState<EventValue, State, Inputs>[1][0]
15+
EventCallbackState<EventValue, State, Inputs>[1][0],
1516
]
1617

1718
export type EventCallback<EventValue, State, Inputs> = Not<
@@ -43,16 +44,17 @@ export function useEventCallback<EventValue, State = void, Inputs = void>(
4344
inputs?: RestrictArray<Inputs>,
4445
): ReturnedState<EventValue, State | null, Inputs> {
4546
const initialValue = (typeof initialState !== 'undefined' ? initialState : null) as VoidAsNull<State>
46-
const inputSubject$ = new BehaviorSubject<RestrictArray<Inputs> | null>(typeof inputs === 'undefined' ? null : inputs)
47-
const stateSubject$ = new BehaviorSubject<State | null>(initialValue)
4847
const [state, setState] = useState(initialValue)
49-
const [event$] = useState(new Subject<EventValue>())
48+
const event$ = useConstant(() => new Subject<EventValue>())
49+
const state$ = useConstant(() => new BehaviorSubject<State | null>(initialValue))
50+
const inputs$ = useConstant(
51+
() => new BehaviorSubject<RestrictArray<Inputs> | null>(typeof inputs === 'undefined' ? null : inputs),
52+
)
53+
5054
function eventCallback(e: EventValue) {
5155
return event$.next(e)
5256
}
5357
const returnedCallback = useCallback(eventCallback, [])
54-
const [state$] = useState(stateSubject$)
55-
const [inputs$] = useState(inputSubject$)
5658

5759
useEffect(() => {
5860
inputs$.next(inputs!)

src/use-observable.ts

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Observable, BehaviorSubject } from 'rxjs'
2-
import { useState, useEffect, useMemo } from 'react'
2+
import { useState, useEffect } from 'react'
3+
import useConstant from 'use-constant'
34

45
import { RestrictArray } from './type'
56

@@ -22,45 +23,35 @@ export function useObservable<State, Inputs extends ReadonlyArray<any>>(
2223
): State | null {
2324
const [state, setState] = useState(typeof initialState !== 'undefined' ? initialState : null)
2425

25-
const { state$, inputs$ } = useMemo(() => {
26-
const stateSubject$ = new BehaviorSubject<State | undefined>(initialState)
27-
const inputSubject$ = new BehaviorSubject<RestrictArray<Inputs> | undefined>(inputs)
28-
29-
return {
30-
state$: stateSubject$,
31-
inputs$: inputSubject$,
32-
}
33-
}, [])
26+
const state$ = useConstant(() => new BehaviorSubject<State | undefined>(initialState))
27+
const inputs$ = useConstant(() => new BehaviorSubject<RestrictArray<Inputs> | undefined>(inputs))
3428

3529
useEffect(() => {
3630
inputs$.next(inputs)
3731
}, inputs || [])
3832

39-
useEffect(
40-
() => {
41-
let output$: BehaviorSubject<State>
42-
if (inputs) {
43-
output$ = (inputFactory as (
44-
inputs$: Observable<RestrictArray<Inputs> | undefined>,
45-
state$: Observable<State | undefined>,
46-
) => Observable<State>)(inputs$, state$) as BehaviorSubject<State>
47-
} else {
48-
output$ = (inputFactory as (state$: Observable<State | undefined>) => Observable<State>)(
49-
state$,
50-
) as BehaviorSubject<State>
51-
}
52-
const subscription = output$.subscribe((value) => {
53-
state$.next(value)
54-
setState(value)
55-
})
56-
return () => {
57-
subscription.unsubscribe()
58-
inputs$.complete()
59-
state$.complete()
60-
}
61-
},
62-
[], // immutable forever
63-
)
33+
useEffect(() => {
34+
let output$: BehaviorSubject<State>
35+
if (inputs) {
36+
output$ = (inputFactory as (
37+
inputs$: Observable<RestrictArray<Inputs> | undefined>,
38+
state$: Observable<State | undefined>,
39+
) => Observable<State>)(inputs$, state$) as BehaviorSubject<State>
40+
} else {
41+
output$ = (inputFactory as (state$: Observable<State | undefined>) => Observable<State>)(
42+
state$,
43+
) as BehaviorSubject<State>
44+
}
45+
const subscription = output$.subscribe((value) => {
46+
state$.next(value)
47+
setState(value)
48+
})
49+
return () => {
50+
subscription.unsubscribe()
51+
inputs$.complete()
52+
state$.complete()
53+
}
54+
}, []) // immutable forever
6455

6556
return state
6657
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7587,6 +7587,11 @@ url@^0.11.0:
75877587
punycode "1.3.2"
75887588
querystring "0.2.0"
75897589

7590+
use-constant@^1.0.0:
7591+
version "1.0.0"
7592+
resolved "https://registry.yarnpkg.com/use-constant/-/use-constant-1.0.0.tgz#cfab998e6fc19ddc62b056875709f1420e339a10"
7593+
integrity sha512-HmVrMl3+1tEr64ace4UtP5WTdnLyrvYKwF54JVf7B7lSB76JSERDvvgWkaaxlOM3S0dSl1U3WH1l9PupNnzsvQ==
7594+
75907595
use@^3.1.0:
75917596
version "3.1.1"
75927597
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"

0 commit comments

Comments
 (0)