Minor change for the sake of simplification and flexibility.

This commit is contained in:
Julien Fontanet 2014-01-11 19:07:21 +01:00
parent 02cef37128
commit fb1f9c675b
3 changed files with 37 additions and 43 deletions

View File

@ -86,15 +86,30 @@ $mapInPlace = (col, iterator, ctx) ->
class $MappedCollection2 extends $EventEmitter
# The dispatch function is called whenever a new item has to be
# processed and returns the name of the rule to use.
#
# To change the way it is dispatched, just override this it.
dispatch: ->
(@genval and (@genval.rule ? @genval.type)) ? 'unknown'
# This function is called when an item has been dispatched to a
# missing rule.
#
# The default behavior is to throw an error but you may instead
# choose to create a rule:
#
# collection.missingRule = collection.rule
missingRule: (name) ->
throw new Error "undefined rule “#{name}"
# This option makes `set()` create missing rules when necessary
# instead of failing.
#
# TODO: should be replaced by a callback for flexibility.
createMissingRules: false
constructor: ({createMissingRules} = {}) ->
@createMissingRules = !!createMissingRules if createMissingRules?
constructor: ->
# Items are stored here indexed by key.
#
# The prototype of this object is set to `null` to avoid pollution
@ -117,15 +132,6 @@ class $MappedCollection2 extends $EventEmitter
# to use the `name of @_rules` syntax.
@_rules = Object.create null
# Register a dispatch function.
#
# The dispatch function is called whenever a new item has to be
# processed and returns the name of the rule to use.
dispatch: (fn) ->
return @_dispatch unless fn?
@_dispatch = fn
# Register a hook to run at a given point.
#
# A hook receives as parameter an event object with the following
@ -221,8 +227,8 @@ class $MappedCollection2 extends $EventEmitter
definition.call ctx
else
ctx = {
key: definition.key
val: definition.val
key: definition?.key
val: definition?.val
singleton
}
@ -270,11 +276,10 @@ class $MappedCollection2 extends $EventEmitter
remove: (keys) ->
@_removeItems (@_fetchItems keys)
set: (items, {add, update, remove, createMissingRules} = {}) ->
set: (items, {add, update, remove} = {}) ->
add = true unless add?
update = true unless update?
remove = false unless remove?
createMissingRules = false unless createMissingRules?
itemsToAdd = {}
itemsToUpdate = {}
@ -294,15 +299,15 @@ class $MappedCollection2 extends $EventEmitter
return unless @_runHook 'beforeDispatch', item
# Searches for a rule to handle it.
ruleName = @_dispatch.call item
ruleName = @dispatch.call item
rule = @_rules[ruleName]
unless rule?
@_assert(
@createMissingRules
"undefined rule “#{ruleName}"
)
rule = @rule ruleName, {}
@missingRule ruleName
# The flow has not been interrupted, `missingRule()` must have
# created the rule.
rule = @_rules[ruleName]
# Checks if this is a singleton.
@_assert(
@ -368,10 +373,6 @@ class $MappedCollection2 extends $EventEmitter
_assert: (cond, message) ->
throw new Error message unless cond
# Default function used for dispatching.
_dispatch: ->
(@genval and @genval.rule ? @genval.type) ? 'unknown'
# Emits item related event.
_emitEvent: (event, items) ->
byRule = {}

View File

@ -26,8 +26,8 @@ describe '$MappedCollection2', ->
#------------------------------
it 'should provides genkey and genval to the dispatch function', ->
collection.dispatch ->
it 'should have genkey and genval', ->
collection.dispatch = ->
$expect(@genkey).to.equal 'a key'
$expect(@genval).to.equal 'a value'
@ -39,8 +39,8 @@ describe '$MappedCollection2', ->
#------------------------------
it 'should register a dispatch function', ->
collection.dispatch -> 'test'
it 'should be used to dispatch an item', ->
collection.dispatch = -> 'test'
collection.set [
'any value'
@ -48,14 +48,6 @@ describe '$MappedCollection2', ->
$expect(collection.getRaw('0').rule).to.equal 'test'
#------------------------------
it 'should return the dispatch function with no parameter', ->
dispatcher = -> 'test'
collection.dispatch dispatcher
$expect(collection.dispatch()).to.equal dispatcher
#-------------------------------------------------------------------
describe 'item hooks', ->
@ -76,7 +68,7 @@ describe '$MappedCollection2', ->
# It still is a dispatcher.
'test'
collection.dispatch dispatcher
collection.dispatch = dispatcher
beforeUpdate = $sinon.spy ->
$expect(dispatcher.called).to.true

View File

@ -16,12 +16,13 @@ describe 'Helper', ->
collection = $set = $sum = $val = null
beforeEach ->
# Creates the collection.
collection = new $MappedCollection2 {
createMissingRules: true
}
collection = new $MappedCollection2()
# Dispatcher used for tests.
collection.dispatch -> (@genkey.split '.')[0]
collection.dispatch = -> (@genkey.split '.')[0]
# Missing rules should be automatically created.
collection.missingRule = collection.rule
# # Monkey patch the collection to see all emitted events.
# emit = collection.emit