-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a01624f
commit f9a236c
Showing
10 changed files
with
195 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {globalState} from "../core/globalstate"; | ||
import {ComputedValue} from "../core/computedvalue"; | ||
import {Reaction} from "../core/reaction"; | ||
import {getAtom} from "../types/type-utils"; | ||
import {invariant} from "../utils/utils"; | ||
|
||
function log(msg: string): string { | ||
console.log(msg); | ||
return msg; | ||
} | ||
|
||
export function whyRun(thing?: any, prop?: string) { | ||
switch (arguments.length) { | ||
case 0: | ||
thing = globalState.derivationStack[globalState.derivationStack.length - 1]; | ||
if (!thing) | ||
return log("whyRun() can only be used if a derivation is active, or by passing an computed value / reaction explicitly. If you invoked whyRun from inside a computation; the computation is currently suspended but re-evaluating because somebody requested it's value."); | ||
break; | ||
case 2: | ||
thing = getAtom(thing, prop); | ||
break; | ||
} | ||
thing = getAtom(thing); | ||
if (thing instanceof ComputedValue) | ||
return log(thing.whyRun()); | ||
else if (thing instanceof Reaction) | ||
return log(thing.whyRun()); | ||
else | ||
invariant(false, "whyRun can only be used on reactions and computed values"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"use strict" | ||
|
||
const test = require('tape'); | ||
const mobx = require('../'); | ||
const noop = () => {}; | ||
|
||
test("whyrun", t => { | ||
const baselog = console.log; | ||
let lastButOneLine = ""; | ||
let lastLine = ""; | ||
|
||
const whyRun = function () { | ||
lastButOneLine = lastLine; | ||
console.log = noop; | ||
lastLine = mobx.whyRun.apply(null, arguments); | ||
console.log = baselog; | ||
return lastLine; | ||
} | ||
|
||
const x = mobx.observable({ | ||
firstname: "Michel", | ||
lastname: "Weststrate", | ||
fullname: function() { | ||
var res = this.firstname + " " + this.lastname; | ||
whyRun(); | ||
return res; | ||
} | ||
}); | ||
|
||
x.fullname; | ||
t.ok(lastLine.match(/suspended/), "just accessed fullname"); // no normal report, just a notification that nothing is being derived atm | ||
|
||
t.ok(whyRun(x, "fullname").match(/\[idle\]/)); | ||
t.ok(whyRun(x, "fullname").match(/suspended/)); | ||
|
||
const d = mobx.autorun(() => { | ||
x.fullname; | ||
whyRun(); | ||
}) | ||
|
||
t.ok(lastButOneLine.match(/\[started\]/), "created autorun"); | ||
t.ok(lastButOneLine.match(/will re-run/)); | ||
t.ok(lastButOneLine.match(/\.firstname/)); | ||
t.ok(lastButOneLine.match(/\.lastname/)); | ||
t.ok(lastButOneLine.match(/Autorun@/)); | ||
|
||
t.ok(lastLine.match(/\[running\]/)); | ||
t.ok(lastLine.match(/\.fullname/)); | ||
|
||
t.ok(whyRun(x, "fullname").match(/\[idle\]/)); | ||
t.ok(whyRun(x, "fullname").match(/\.firstname/)); | ||
t.ok(whyRun(x, "fullname").match(/\.lastname/)); | ||
t.ok(whyRun(x, "fullname").match(/Autorun@/)); | ||
|
||
t.ok(whyRun(d).match(/\[idle\]/)); | ||
t.ok(whyRun(d).match(/\.fullname/)); | ||
|
||
t.ok(whyRun(d).match(/Autorun@/)); | ||
|
||
mobx.transaction(() => { | ||
x.firstname = "Veria"; | ||
t.ok(whyRun(x, "fullname").match(/\[idle\]/), "made change in transaction"); | ||
t.ok(whyRun(x, "fullname").match(/next run is scheduled/)); | ||
|
||
t.ok(whyRun(d).match(/\[scheduled\]/)); | ||
}) | ||
|
||
t.ok(lastButOneLine.match(/\[invalidated\]/),"post transaction"); | ||
t.ok(lastButOneLine.match(/will re-run/)); | ||
t.ok(lastButOneLine.match(/\.firstname/)); | ||
t.ok(lastButOneLine.match(/\.lastname/)); | ||
t.ok(lastButOneLine.match(/\Autorun@/)); | ||
|
||
t.ok(lastLine.match(/\[running\]/)); | ||
t.ok(lastLine.match(/\.fullname/)); | ||
|
||
t.ok(whyRun(x, "fullname").match(/\[idle\]/)); | ||
t.ok(whyRun(x, "fullname").match(/\.firstname/)); | ||
t.ok(whyRun(x, "fullname").match(/\.lastname/)); | ||
t.ok(whyRun(x, "fullname").match(/Autorun@/)); | ||
|
||
t.ok(whyRun(d).match(/\[idle\]/)); | ||
t.ok(whyRun(d).match(/\.fullname/)); | ||
t.ok(whyRun(d).match(/Autorun@/)); | ||
|
||
d(); | ||
|
||
t.ok(whyRun(d).match(/\[stopped\]/)); | ||
t.ok(whyRun(x, "fullname").match(/\[idle\]/)); | ||
t.ok(whyRun(x, "fullname").match(/suspended/)); | ||
|
||
t.end(); | ||
}) |