From d64e0a950f00e5d24a367bd2f4eb32b052c67e83 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Wed, 8 Aug 2018 16:06:21 +0200 Subject: [PATCH] Fix #1714 conditionally halting stream (#2200) * Fix #1714 conditionally halting stream * Add note in changelog --- docs/change-log.md | 1 + stream/stream.js | 2 +- stream/tests/test-stream.js | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/change-log.md b/docs/change-log.md index 8734fc55..67043e4d 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -26,6 +26,7 @@ - API: `m.request` will no longer reject the Promise on server errors (eg. status >= 400) if the caller supplies an `extract` callback. This gives applications more control over handling server responses. - hyperscript: when attributes have a `null` or `undefined` value, they are treated as if they were absent. [#1773](https://github.com/MithrilJS/mithril.js/issues/1773) ([#2174](https://github.com/MithrilJS/mithril.js/pull/2174)) - hyperscript: when an attribute is defined on both the first and second argument (as a CSS selector and an `attrs` field, respectively), the latter takes precedence, except for `class` attributes that are still added together. [#2172](https://github.com/MithrilJS/mithril.js/issues/2172) ([#2174](https://github.com/MithrilJS/mithril.js/pull/2174)) +- stream: when a stream conditionally returns HALT, dependant stream will also end ([#2200](https://github.com/MithrilJS/mithril.js/pull/2200)) #### News diff --git a/stream/stream.js b/stream/stream.js index 20fd6378..9bf1fed5 100644 --- a/stream/stream.js +++ b/stream/stream.js @@ -53,7 +53,7 @@ function updateDependency(stream, mustSync) { var state = stream._state, parents = state.parents if (parents.length > 0 && parents.every(active) && (mustSync || parents.some(changed))) { var value = stream._state.derive() - if (value === HALT) return false + if (value === HALT) return unregisterStream(stream) updateState(stream, value) } } diff --git a/stream/tests/test-stream.js b/stream/tests/test-stream.js index 7ea944d7..69ca811d 100644 --- a/stream/tests/test-stream.js +++ b/stream/tests/test-stream.js @@ -164,6 +164,27 @@ o.spec("stream", function() { o(b()).equals(undefined) o(count).equals(0) }) + o("combine can conditionaly halt", function() { + var count = 0 + var halt = false + var a = Stream(1) + var b = Stream.combine(function(a) { + if (halt) { + return Stream.HALT + } + return a() + }, [a])["fantasy-land/map"](function(a) { + count++ + return a + }) + o(b()).equals(1) + o(count).equals(1) + halt = true + count = 0 + a(2) + o(b()).equals(1) + o(count).equals(0) + }) o("combine will throw with a helpful error if given non-stream values", function () { var spy = o.spy() var a = Stream(1)