Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cleanup of timer nodes #76

Merged
merged 1 commit into from
Nov 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ typealias Input Node

Base.show(io::IO, n::Node) =
write(io, "Node{$(eltype(n))}($(n.value), nactions=$(length(n.actions))$(n.alive ? "" : ", closed"))")

value(n::Node) = n.value
eltype{T}(::Node{T}) = T
eltype{T}(::Type{Node{T}}) = T

##### Connections #####

function add_action!(f, node, recipient)
push!(node.actions, Action(recipient, f))
end
Expand All @@ -88,7 +88,7 @@ function close(n::Node, warn_nonleaf=true)
end
end

function send_value!(node, x, timestep)
function send_value!(node::Node, x, timestep)
# Dead node?
!node.alive && return

Expand All @@ -98,6 +98,7 @@ function send_value!(node, x, timestep)
do_action(action, timestep)
end
end
send_value!(wr::WeakRef, x, timestep) = send_value!(wr.value, x, timestep)

do_action(a::Action, timestep) =
isrequired(a) && a.f(a.recipient, timestep)
Expand All @@ -115,7 +116,9 @@ const CHANNEL_SIZE = 1024
const _messages = Channel{Any}(CHANNEL_SIZE)

# queue an update. meta comes back in a ReactiveException if there is an error
function Base.push!(n::Node, x, onerror=print_error)
Base.push!(n::Node, x, onerror=print_error) = _push!(n, x, onerror)

function _push!(n, x, onerror=print_error)
taken = Base.n_avail(_messages)
if taken >= CHANNEL_SIZE
warn("Message queue is full. Ordering may be incorrect.")
Expand Down
7 changes: 1 addition & 6 deletions src/time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ function every(dt)
n
end

function weakrefdo(ref, yes, no=()->nothing)
ref.value != nothing ? yes(ref.value) : no()
end

function every_connect(dt, output)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was using this in fpswhen below as well.

I added this function back and tried node = nothing after send_value! in core.jl. The fps node doesn't go away still.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent some time looking, but I confess I don't really understand the design of fps well enough to be of any use.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic idea of fps is that it will fire at most rate times a second. If a signal depending on this fps signal takes longer than 1/rate seconds to update (for example drawing a complex Compose image), it will try to give you the best possible frame rate. The implementation schedules an update 1/rate seconds from the current update (during the current update) to accomplish this. The fps signal itself contains the time deltas between the previous update and the current one...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very helpful, thanks. So the user gets enough information to make the decision, for example, whether to show the next frame of the movie or to skip ahead to the frame that should have been scheduled at the current moment? That's very nice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or compute the correct frame from a function such as update(current_frame, dt) -> next_frame. The animation would then just be: foldp(update, init_frame, fps(60)), of course you are also free to use time() and map with fps.

outputref = WeakRef(output)
timer = Timer(x -> weakrefdo(outputref, x->push!(x, time()), ()->close(timer)), dt, dt)
timer = Timer(x -> _push!(outputref, time(), ()->close(timer)), dt, dt)
finalizer(output, _->close(timer))
output
end
Expand Down Expand Up @@ -68,4 +64,3 @@ function fpswhen(switch, rate)
end

fps(rate) = fpswhen(Node(Bool, true, ()), rate)