@@ -32,6 +32,10 @@ the [`Time`](/docs/Signal/Time.elm) library.
3232import Native.Signal
3333import List (foldr)
3434
35+import Basics (otherwise, not)
36+import Native.Error
37+import Maybe as M
38+
3539data Signal a = Signal
3640
3741{-| Create a constant signal that never changes. -}
@@ -122,7 +126,18 @@ Until the first signal becomes false again, all events will be propagated. Elm
122126does not allow undefined signals, so a base case must be provided in case the
123127first signal is not true initially. -}
124128keepWhen : Signal Bool -> a -> Signal a -> Signal a
125−keepWhen = Native.Signal.keepWhen
129+keepWhen bs def sig =
130+ let toMaybe : Bool -> a -> M.Maybe a
131+ toMaybe b x = if b
132+ then M.Just x
133+ else M.Nothing
134+
135+ fromJust : M.Maybe a -> a
136+ fromJust m = case m of
137+ M.Just x -> x
138+ M.Nothing -> Native.Error.raise "Internal bug in keepWhen! Please report at https://github.com/evancz/Elm/issues?state=open"
139+ in
140+ fromJust <~ (keepIf M.isJust (M.Just def) (toMaybe <~ (sampleOn sig bs) ~ sig))
126141
127142{-| Drop events when the first signal is true. When the first signal becomes
128143false, the most recent value of the second signal will be propagated. Until the
@@ -130,7 +145,7 @@ first signal becomes true again, all events will be propagated. Elm does not
130145allow undefined signals, s oa base case must be provided in case the first
131146signal is true initially. -}
132147dropWhen : Signal Bool -> a -> Signal a -> Signal a
133−dropWhen = Native.Signal.dropWhen
148+dropWhen bs = keepWhen (not <~ bs)
134149
135150{-| Drop updates that repeat the current value of the signal.
136151