on master help sign in
Add watch and watchSummary functions
commit5e1ae81505f9acb8b0b51d71dfaa5aaa7c367b3b authorEvan Czaplicki <info@elm-lang.org> dateSun 6 Apr 2014, 23:51 (13 years ago) parent2a03dd9
3 files changed+45−2
libraries/Debug.elm+10 libraries/Native/Debug.js+29−1 runtime/Debugger.js+6−1
libraries/Debug.elm+10
@@ -19,6 +19,16 @@ investigating bugs or performance problems.
1919log : String -> a -> a
2020log = Native.Debug.log
2121
22+{-| Watch a particular value in the reactive debugger.
23+-}
24+watch : String -> a -> a
25+watch = Native.Debug.watch
26+
27+{-| Watch a particular value in the reactive debugger.
28+-}
29+watchSummary : String -> (a -> b) -> a -> a
30+watchSummary = Native.Debug.watchSummary
31+
2232{-| Trace an Element over time in the reactive debugger.
2333-}
2434trace : String -> Element -> Element
libraries/Native/Debug.js+29−1
@@ -8,7 +8,7 @@ Elm.Native.Debug.make = function(elm) {
88
99 var show = Elm.Native.Show.make(elm).show;
1010
11− function log(tag,value) {
11+ function log(tag, value) {
1212 var msg = tag + ': ' + show(value);
1313 var process = process || {};
1414 if (process.stdout) {
@@ -24,9 +24,37 @@ Elm.Native.Debug.make = function(elm) {
2424 return elem;
2525 }
2626
27+ function WatchTracker() {
28+ this.frames = [{}];
29+ this.clear = function() {
30+ this.watches = {};
31+ };
32+ this.pushFrame = function() {
33+ var lastFrame = this.frames[this.frames.length - 1];
34+ this.frames.push(lastFrame);
35+ }
36+ this.notify = function(tag, value) {
37+ this.frames[this.frames.length - 1][tag] = value;
38+ };
39+ }
40+ var watchTracker = new WatchTracker();
41+
42+ function watch(tag, value) {
43+ watchTracker.notify(tag, value);
44+ return value;
45+ }
46+
47+ function watchSummary(tag, f, value) {
48+ watchTracker.notify(tag, f(value));
49+ return value;
50+ }
51+
2752 Elm.Native.Debug.values = {
2853 tracePath: F2(tracePath),
2954 log: F2(log),
55+ watch: F2(watch),
56+ watchSummary:F3(watchSummary),
57+ watchTracker: watchTracker
3058 };
3159 return elm.Native.Debug.values = Elm.Native.Debug.values;
3260};
runtime/Debugger.js+6−1
@@ -32,6 +32,7 @@ function debugModule(module, runtime) {
3232 var programPaused = false;
3333 var recordedEvents = [];
3434 var asyncCallbacks = [];
35+ var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
3536
3637 function wrapNotify(id, v) {
3738 var timestep = Date.now();
@@ -63,6 +64,7 @@ function debugModule(module, runtime) {
6364 }
6465
6566 function recordEvent(id, v, timestep) {
67+ watchTracker.pushFrame();
6668 recordedEvents.push({ id:id, value:v, timestep:timestep });
6769 }
6870
@@ -143,7 +145,8 @@ function debugModule(module, runtime) {
143145 loadRecordedEvents: loadRecordedEvents,
144146 getPaused: getPaused,
145147 setPaused: setPaused,
146− tracePath: tracePath
148+ tracePath: tracePath,
149+ watchTracker: watchTracker,
147150 };
148151}
149152
@@ -158,6 +161,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
158161
159162 function restartProgram() {
160163 resetProgram();
164+ debugModule.watchTracker.clear();
161165 debugModule.tracePath.clearTraces();
162166 debugModule.clearRecordedEvents();
163167 debugModule.setPaused(false);
@@ -262,6 +266,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
262266 getHotSwapState: getHotSwapState,
263267 dispose: dispose,
264268 allNodes: debugModule.moduleNodes,
269+ watchTracker: debugModule.watchTracker,
265270 mainNode: debugModule.moduleInstance.main
266271 };
267272