Skip to content

Commit d1ae005

Browse files
committed
Added LogLifecycle. Logs during each lifecycle callback.
1 parent eba74b5 commit d1ae005

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

HISTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ History
66
* Fixed Scalatags rejecting `VDom`.
77
* Added `ScalazReact.ReactS.setM`.
88
* Added `Listenable.install{IO,F}`, added `M[_]` to `Listenable.installS`.
9+
* Added `LogLifecycle` which when applied to a component, logs during each lifecycle callback.
910

1011
### 0.5.0 ([commit log](https://github.com/japgolly/scalajs-react/compare/v0.4.1...v0.5.0))
1112

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ object ReactComponentB {
6666

6767
import ReactComponentB.LifeCycle
6868

69-
final class ReactComponentB[P, S, B](name: String,
69+
final class ReactComponentB[P, S, B](val name: String,
7070
initF: P => S,
7171
backF: BackendScope[P, S] => B,
7272
rendF: ComponentScopeU[P, S, B] => VDom,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package japgolly.scalajs.react.experiment
2+
3+
import japgolly.scalajs.react.ReactComponentB
4+
import org.scalajs.dom.console
5+
import scala.scalajs.js
6+
7+
object LogLifecycle {
8+
@inline private[this] def fmt(m: String, a: Any) =
9+
Seq[js.Any](s"\n $m: $a")
10+
11+
@inline private[this] def log(m: js.Any, ps: js.Any*) =
12+
console.log(m, ps: _*)
13+
14+
@inline private[this] def logc(m: js.Any, c: js.Any, ps: js.Any*) =
15+
log(m + "\n ", c +: ps: _*)
16+
17+
@inline private[this] def log1(m: String) = (c: js.Any) =>
18+
logc(m, c)
19+
20+
@inline private[this] def logp(m: String) = (c: js.Any, p: Any) =>
21+
logc(m, c, fmt("Props", p): _*)
22+
23+
@inline private[this] def logps(m: String) = (c: js.Any, p: Any, s: Any) =>
24+
logc(m, c, fmt("Props", p) ++ fmt("State", s): _*)
25+
26+
def short[P, S, B] = (rc: ReactComponentB[P, S, B]) => {
27+
val name = rc.name
28+
rc.componentWillMount (_ => log(s"$name.componentWillMount"))
29+
.componentDidMount (_ => log(s"$name.componentDidMount"))
30+
.componentWillUnmount (_ => log(s"$name.componentWillUnmount"))
31+
.componentWillUpdate ((_,_,_) => log(s"$name.componentWillUpdate"))
32+
.componentDidUpdate ((_,_,_) => log(s"$name.componentDidUpdate"))
33+
.componentWillReceiveProps((_,_) => log(s"$name.componentWillReceiveProps"))
34+
}
35+
36+
def verbose[P, S, B] = (rc: ReactComponentB[P, S, B]) => {
37+
val name = rc.name
38+
rc.componentWillMount (log1 (s"$name.componentWillMount"))
39+
.componentDidMount (log1 (s"$name.componentDidMount"))
40+
.componentWillUnmount (log1 (s"$name.componentWillUnmount"))
41+
.componentWillUpdate (logps(s"$name.componentWillUpdate"))
42+
.componentDidUpdate (logps(s"$name.componentDidUpdate"))
43+
.componentWillReceiveProps(logp (s"$name.componentWillReceiveProps"))
44+
}
45+
}

0 commit comments

Comments
 (0)