Skip to content

Commit

Permalink
Raise error if push is called from inside a lifted function
Browse files Browse the repository at this point in the history
  • Loading branch information
shashi committed Jun 16, 2014
1 parent be99d95 commit c81e9ab
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
73 changes: 41 additions & 32 deletions src/React.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,52 +156,61 @@ function update{T, U}(node :: SampleOn{T, U}, parent :: Signal{T})
return true
end

function push!{T}(inp :: Input{T}, val :: T)
inp.value = val
begin
local isupdating = false
function push!{T}(inp :: Input{T}, val :: T)
if isupdating
error("calling push! inside a lift is prohibited.")
end
isupdating = true
inp.value = val

heap = (Signal, Signal)[] # a min-heap of (child, parent)
ord = By(a -> a[1].rank) # ordered topologically by child.rank
heap = (Signal, Signal)[] # a min-heap of (child, parent)
ord = By(a -> a[1].rank) # ordered topologically by child.rank

# first dirty parent
merge_parent = Dict{Merge, Signal}()
for c in inp.children
if isa(c, Merge)
merge_parent[c] = inp
# first dirty parent
merge_parent = Dict{Merge, Signal}()
for c in inp.children
if isa(c, Merge)
merge_parent[c] = inp
end
heappush!(heap, (c, inp), ord)
end
heappush!(heap, (c, inp), ord)
end

prev = nothing
while !isempty(heap)
(n, parent) = heappop!(heap, ord)
if n == prev
continue # already processed
end
prev = nothing
while !isempty(heap)
(n, parent) = heappop!(heap, ord)
if n == prev
continue # already processed
end

# Merge is a special case!
if isa(n, Merge) && haskey(merge_parent, n)
propagate = update(n, merge_parent[n])
else
propagate = update(n, parent)
end
# Merge is a special case!
if isa(n, Merge) && haskey(merge_parent, n)
propagate = update(n, merge_parent[n])
else
propagate = update(n, parent)
end

if propagate
for c in n.children
if isa(c, Merge)
if haskey(merge_parent, c)
if c.ranks[n] < c.ranks[merge_parent[c]]
if propagate
for c in n.children
if isa(c, Merge)
if haskey(merge_parent, c)
if c.ranks[n] < c.ranks[merge_parent[c]]
merge_parent[c] = n
end
else
merge_parent[c] = n
end
else
merge_parent[c] = n
end
heappush!(heap, (c, n), ord)
end
heappush!(heap, (c, n), ord)
end
prev = n
end
prev = n
isupdating = false
end
end

push!{T}(inp :: Input{T}, val) = push!(inp, convert(T, val))

lift(f :: Function, output_type :: Type, inputs :: Signal...) =
Expand Down
2 changes: 1 addition & 1 deletion tests/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ b = lift(x -> x*x, Int, a)
# type conversion
push!(a, 1.0)
@test b.value == 1
@test_throws push!(a, 1.1) # inexact error
@test_throws InexactError push!(a, 1.1) # inexact error

push!(a, number())
@test b.value == a.value*a.value
Expand Down
2 changes: 1 addition & 1 deletion tests/concurrency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ function crash(x)
end

b = lift(crash, a)
@test_throws push!(a, 1)
@test_throws ErrorException push!(a, 1)

0 comments on commit c81e9ab

Please sign in to comment.