Skip to content

Commit 4f6edf1

Browse files
committed
Fixed merging of multiple shouldComponentUpdate callbacks.
1 parent 111809c commit 4f6edf1

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

core/src/main/scala/japgolly/scalajs/react/ReactComponentB.scala

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,29 @@ final class ReactComponentB[Props](name: String) {
4444
case class B4[C] private[ReactComponentB](
4545
__render: ScopeU => VDom,
4646
__c: ComponentConstructor[Props, State, Backend] => C
47-
, getDefaultProps: UndefOr[() => Props]
48-
, componentWillMount: UndefOr[ScopeU => Unit]
49-
, componentDidMount: UndefOr[ScopeM => Unit]
50-
, componentWillUnmount: UndefOr[ScopeM => Unit]
51-
, componentWillUpdate: UndefOr[(ScopeWU, Props, State) => Unit]
52-
, componentDidUpdate: UndefOr[(ScopeM, Props, State) => Unit]
53-
, componentWillReceiveProps: UndefOr[(ScopeM, Props) => Unit]
54-
, shouldComponentUpdate: UndefOr[(ScopeM, Props, State) => Boolean]
47+
, getDefaultProps : UndefOr[() => Props]
48+
, componentWillMount : UndefOr[ScopeU => Unit]
49+
, componentDidMount : UndefOr[ScopeM => Unit]
50+
, componentWillUnmount : UndefOr[ScopeM => Unit]
51+
, componentWillUpdate : UndefOr[(ScopeWU, Props, State) => Unit]
52+
, componentDidUpdate : UndefOr[(ScopeM, Props, State) => Unit]
53+
, componentWillReceiveProps : UndefOr[(ScopeM, Props) => Unit]
54+
, shouldComponentUpdate : UndefOr[(ScopeM, Props, State) => Boolean]
5555
) {
56-
import ReactComponentB.composeUndef
56+
import ReactComponentB.Internal._
5757

58-
def getDefaultProps(f: => Props): B4[C] = copy(getDefaultProps = () => f)
59-
def propsDefault(f: => Props): B4[CCOP] = copy(__c = new CompCtorOP(_, None, () => f))
60-
def propsAlways(f: => Props): B4[CCNP] = copy(__c = new CompCtorNP(_, None, () => f))
58+
def getDefaultProps(f: => Props): B4[C] = copy(getDefaultProps = () => f)
59+
def propsDefault (f: => Props): B4[CCOP] = copy(__c = new CompCtorOP(_, None, () => f))
60+
def propsAlways (f: => Props): B4[CCNP] = copy(__c = new CompCtorNP(_, None, () => f))
6161
// def propsAlways(f: => Props): B4[CCNP] = getDefaultProps(f).copy(__c = new CompCtorNP(_))
6262

63-
def componentWillMount(f: ScopeU => Unit): B4[C] = copy(componentWillMount = composeUndef(componentWillMount, f))
64-
def componentDidMount(f: ScopeM => Unit): B4[C] = copy(componentDidMount = composeUndef(componentDidMount, f))
65-
def componentWillUnmount(f: ScopeM => Unit): B4[C] = copy(componentWillUnmount = composeUndef(componentWillUnmount, f))
66-
def componentWillUpdate(f: (ScopeWU, Props, State) => Unit): B4[C] = copy(componentWillUpdate = composeUndef(componentWillUpdate, f))
67-
def componentDidUpdate(f: (ScopeM, Props, State) => Unit): B4[C] = copy(componentDidUpdate = composeUndef(componentDidUpdate, f))
68-
def componentWillReceiveProps(f: (ScopeM, Props) => Unit): B4[C] = copy(componentWillReceiveProps = composeUndef(componentWillReceiveProps, f))
69-
def shouldComponentUpdate(f: (ScopeM, Props, State) => Boolean): B4[C] = copy(shouldComponentUpdate = composeUndef(shouldComponentUpdate, f))
63+
def componentWillMount (f: ScopeU => Unit ): B4[C] = copy(componentWillMount = fcUnit(componentWillMount , f))
64+
def componentDidMount (f: ScopeM => Unit ): B4[C] = copy(componentDidMount = fcUnit(componentDidMount , f))
65+
def componentWillUnmount (f: ScopeM => Unit ): B4[C] = copy(componentWillUnmount = fcUnit(componentWillUnmount , f))
66+
def componentWillUpdate (f: (ScopeWU, Props, State) => Unit ): B4[C] = copy(componentWillUpdate = fcUnit(componentWillUpdate , f))
67+
def componentDidUpdate (f: (ScopeM, Props, State) => Unit ): B4[C] = copy(componentDidUpdate = fcUnit(componentDidUpdate , f))
68+
def componentWillReceiveProps(f: (ScopeM, Props) => Unit ): B4[C] = copy(componentWillReceiveProps = fcUnit(componentWillReceiveProps, f))
69+
def shouldComponentUpdate (f: (ScopeM, Props, State) => Boolean): B4[C] = copy(shouldComponentUpdate = fcEither(shouldComponentUpdate , f))
7070

7171
def buildSpec = {
7272
val spec = Dynamic.literal(
@@ -119,18 +119,24 @@ final class ReactComponentB[Props](name: String) {
119119
object ReactComponentB {
120120
def apply[Props](name: String) = new ReactComponentB[Props](name)
121121

122-
private def composeUndef[A, R](f1: UndefOr[A => R], g: A => R) = f1.fold(g) { f => (a: A) =>
123-
f(a)
124-
g(a)
125-
}
122+
private object Internal {
123+
final class FnResults[R](aa: => R, bb: => R) {
124+
lazy val a = aa
125+
lazy val b = bb
126+
}
126127

127-
private def composeUndef[A, B, R](f1: UndefOr[(A, B) => R], g: (A, B) => R) = f1.fold(g) { f => (a: A, b: B) =>
128-
f(a, b)
129-
g(a, b)
130-
}
128+
final class FnComposer[R](m: FnResults[R] => R) {
129+
def apply[A](uf: UndefOr[A => R], g: A => R) =
130+
uf.fold(g)(f => a => m(new FnResults(f(a), g(a))))
131+
132+
def apply[A, B](uf: UndefOr[(A, B) => R], g: (A, B) => R) =
133+
uf.fold(g)(f => (a,b) => m(new FnResults(f(a,b), g(a,b))))
134+
135+
def apply[A, B, C](uf: UndefOr[(A, B, C) => R], g: (A, B, C) => R) =
136+
uf.fold(g)(f => (a,b,c) => m(new FnResults(f(a,b,c), g(a,b,c))))
137+
}
131138

132-
private def composeUndef[A, B, C, R](f1: UndefOr[(A, B, C) => R], g: (A, B, C) => R) = f1.fold(g) { f => (a: A, b: B, c: C) =>
133-
f(a, b, c)
134-
g(a, b, c)
139+
val fcUnit = new FnComposer[Unit](r => {r.a; r.b})
140+
val fcEither = new FnComposer[Boolean](r => r.a || r.b)
135141
}
136142
}

0 commit comments

Comments
 (0)