Skip to content
This repository has been archived by the owner on Dec 30, 2018. It is now read-only.

Commit

Permalink
Deduplicate model updates in DeckViewModel.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstorey committed Apr 4, 2013
1 parent ecf859b commit 3c67534
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/srsrb/deck_view.rb
Expand Up @@ -62,18 +62,21 @@ def handle_card_edited id, event
end

def handle_model_named id, event
self._card_models = _card_models.put id, CardModel.new(id: id, name: event.name)
update_model(id) { |_| CardModel.new(id: id, name: event.name) }
end

def update_model id, &block
new_model = block.call _card_models[id]

self._card_models = _card_models.put id, new_model
self._card_model_ids = _card_model_ids.add id if not _card_model_ids.include? id
pp _card_model_ids: _card_model_ids.map(&:to_guid).to_a
end

def handle_model_field_added id, event
if not model = _card_models[id]
model = CardModel.new(id: id)
end
model = model.set_fields (model.fields.add event.field)
self._card_models = _card_models.put id, model
self._card_model_ids = _card_model_ids.add id if not _card_model_ids.include? id
update_model(id) { |old_model|
model = old_model || CardModel.new(id: id)
model.set_fields model.fields.add(event.field)
}
end

attr_accessor :queue, :cards, :event_store, :_card_models, :_card_model_ids
Expand Down

0 comments on commit 3c67534

Please sign in to comment.