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

Reactive Components #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Overseer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Overseer
include("group.jl")
include("system.jl")
include("ledger.jl")
include("reactive.jl")

export AbstractLedger, Ledger, System, Stage, Component, SharedComponent, GroupedComponent, ComponentData, Entity
export @component, @shared_component, @grouped_component
Expand Down
74 changes: 74 additions & 0 deletions src/reactive.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
mutable struct ReactiveComponent{T, C<:AbstractComponent} <: AbstractComponent{T}
component::C
changed::Bool
end
ReactiveComponent{T}() where {T <: ComponentData} = ReactiveComponent{T, Component{T}}(Component{T}(), false)

macro reactive_component(typedef)
return esc(Overseer._reactive_component(typedef, __module__))
end

function _reactive_component(typedef, mod)
t = process_typedef(typedef, mod)
t1, tn = t
return quote
$t1
Overseer.component_type(::Type{$tn}) = Overseer.ReactiveComponent
end
end

function Base.getproperty(c::ReactiveComponent, f::Symbol)
if f == :component
return getfield(c, :component)
elseif f == :changed
return getfield(c, :changed)
else
return getproperty(c.component, f)
end
end

# Base mutating functions
for f in (:delete!, :permute!, :setindex!, :empty!, :pop!)
@eval function Base.$f(c::ReactiveComponent, args...)
c.changed = true
$f(c.component, args...)
end
end

# Overseer mutating functions
for f in (:swap_order!, :pop_indices_data!, :ensure_entity_id!)
@eval function $f(c::ReactiveComponent, args...)
c.changed = true
$f(c.component, args...)
end
end

# Base non-mutating
for f in (:getindex, :pointer, :iterate, :sortperm, :eltype)
@eval Base.$f(c::ReactiveComponent, args...) = $f(c.component, args...)
end

function update_reactive(l::AbstractLedger)
changed_comps = filter(x -> x isa ReactiveComponent && x.changed, collect(values(components(l))))
if isempty(changed_comps)
return
end
types = eltype.(changed_comps)
systems_to_update = System[]
for stage in stages(l)
for sys in stage[2]
if any(x -> x ∈ in_components(sys), types)
push!(systems_to_update, sys)
end
end
end
for sys in systems_to_update
update(sys, l)
end
for c in changed_comps
c.changed = false
end

# Now recurse for possible updated components
update_reactive(l)
end
1 change: 1 addition & 0 deletions src/system.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
update(::S, m::AbstractLedger) where {S<:System}= error("No update method implemented for $S")

requested_components(::System) = ()
in_components(::System) = ()

const Stage = Pair{Symbol, Vector{System}}

Expand Down