Minor change for the sake of simplification and flexibility.
This commit is contained in:
parent
02cef37128
commit
fb1f9c675b
@ -86,15 +86,30 @@ $mapInPlace = (col, iterator, ctx) ->
|
|||||||
|
|
||||||
class $MappedCollection2 extends $EventEmitter
|
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
|
# This option makes `set()` create missing rules when necessary
|
||||||
# instead of failing.
|
# instead of failing.
|
||||||
#
|
#
|
||||||
# TODO: should be replaced by a callback for flexibility.
|
# TODO: should be replaced by a callback for flexibility.
|
||||||
createMissingRules: false
|
createMissingRules: false
|
||||||
|
|
||||||
constructor: ({createMissingRules} = {}) ->
|
constructor: ->
|
||||||
@createMissingRules = !!createMissingRules if createMissingRules?
|
|
||||||
|
|
||||||
# Items are stored here indexed by key.
|
# Items are stored here indexed by key.
|
||||||
#
|
#
|
||||||
# The prototype of this object is set to `null` to avoid pollution
|
# 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.
|
# to use the `name of @_rules` syntax.
|
||||||
@_rules = Object.create null
|
@_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.
|
# Register a hook to run at a given point.
|
||||||
#
|
#
|
||||||
# A hook receives as parameter an event object with the following
|
# A hook receives as parameter an event object with the following
|
||||||
@ -221,8 +227,8 @@ class $MappedCollection2 extends $EventEmitter
|
|||||||
definition.call ctx
|
definition.call ctx
|
||||||
else
|
else
|
||||||
ctx = {
|
ctx = {
|
||||||
key: definition.key
|
key: definition?.key
|
||||||
val: definition.val
|
val: definition?.val
|
||||||
singleton
|
singleton
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,11 +276,10 @@ class $MappedCollection2 extends $EventEmitter
|
|||||||
remove: (keys) ->
|
remove: (keys) ->
|
||||||
@_removeItems (@_fetchItems keys)
|
@_removeItems (@_fetchItems keys)
|
||||||
|
|
||||||
set: (items, {add, update, remove, createMissingRules} = {}) ->
|
set: (items, {add, update, remove} = {}) ->
|
||||||
add = true unless add?
|
add = true unless add?
|
||||||
update = true unless update?
|
update = true unless update?
|
||||||
remove = false unless remove?
|
remove = false unless remove?
|
||||||
createMissingRules = false unless createMissingRules?
|
|
||||||
|
|
||||||
itemsToAdd = {}
|
itemsToAdd = {}
|
||||||
itemsToUpdate = {}
|
itemsToUpdate = {}
|
||||||
@ -294,15 +299,15 @@ class $MappedCollection2 extends $EventEmitter
|
|||||||
return unless @_runHook 'beforeDispatch', item
|
return unless @_runHook 'beforeDispatch', item
|
||||||
|
|
||||||
# Searches for a rule to handle it.
|
# Searches for a rule to handle it.
|
||||||
ruleName = @_dispatch.call item
|
ruleName = @dispatch.call item
|
||||||
rule = @_rules[ruleName]
|
rule = @_rules[ruleName]
|
||||||
|
|
||||||
unless rule?
|
unless rule?
|
||||||
@_assert(
|
@missingRule ruleName
|
||||||
@createMissingRules
|
|
||||||
"undefined rule “#{ruleName}”"
|
# The flow has not been interrupted, `missingRule()` must have
|
||||||
)
|
# created the rule.
|
||||||
rule = @rule ruleName, {}
|
rule = @_rules[ruleName]
|
||||||
|
|
||||||
# Checks if this is a singleton.
|
# Checks if this is a singleton.
|
||||||
@_assert(
|
@_assert(
|
||||||
@ -368,10 +373,6 @@ class $MappedCollection2 extends $EventEmitter
|
|||||||
_assert: (cond, message) ->
|
_assert: (cond, message) ->
|
||||||
throw new Error message unless cond
|
throw new Error message unless cond
|
||||||
|
|
||||||
# Default function used for dispatching.
|
|
||||||
_dispatch: ->
|
|
||||||
(@genval and @genval.rule ? @genval.type) ? 'unknown'
|
|
||||||
|
|
||||||
# Emits item related event.
|
# Emits item related event.
|
||||||
_emitEvent: (event, items) ->
|
_emitEvent: (event, items) ->
|
||||||
byRule = {}
|
byRule = {}
|
||||||
|
@ -26,8 +26,8 @@ describe '$MappedCollection2', ->
|
|||||||
|
|
||||||
#------------------------------
|
#------------------------------
|
||||||
|
|
||||||
it 'should provides genkey and genval to the dispatch function', ->
|
it 'should have genkey and genval', ->
|
||||||
collection.dispatch ->
|
collection.dispatch = ->
|
||||||
$expect(@genkey).to.equal 'a key'
|
$expect(@genkey).to.equal 'a key'
|
||||||
$expect(@genval).to.equal 'a value'
|
$expect(@genval).to.equal 'a value'
|
||||||
|
|
||||||
@ -39,8 +39,8 @@ describe '$MappedCollection2', ->
|
|||||||
|
|
||||||
#------------------------------
|
#------------------------------
|
||||||
|
|
||||||
it 'should register a dispatch function', ->
|
it 'should be used to dispatch an item', ->
|
||||||
collection.dispatch -> 'test'
|
collection.dispatch = -> 'test'
|
||||||
|
|
||||||
collection.set [
|
collection.set [
|
||||||
'any value'
|
'any value'
|
||||||
@ -48,14 +48,6 @@ describe '$MappedCollection2', ->
|
|||||||
|
|
||||||
$expect(collection.getRaw('0').rule).to.equal 'test'
|
$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', ->
|
describe 'item hooks', ->
|
||||||
@ -76,7 +68,7 @@ describe '$MappedCollection2', ->
|
|||||||
|
|
||||||
# It still is a dispatcher.
|
# It still is a dispatcher.
|
||||||
'test'
|
'test'
|
||||||
collection.dispatch dispatcher
|
collection.dispatch = dispatcher
|
||||||
|
|
||||||
beforeUpdate = $sinon.spy ->
|
beforeUpdate = $sinon.spy ->
|
||||||
$expect(dispatcher.called).to.true
|
$expect(dispatcher.called).to.true
|
||||||
|
@ -16,12 +16,13 @@ describe 'Helper', ->
|
|||||||
collection = $set = $sum = $val = null
|
collection = $set = $sum = $val = null
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
# Creates the collection.
|
# Creates the collection.
|
||||||
collection = new $MappedCollection2 {
|
collection = new $MappedCollection2()
|
||||||
createMissingRules: true
|
|
||||||
}
|
|
||||||
|
|
||||||
# Dispatcher used for tests.
|
# 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.
|
# # Monkey patch the collection to see all emitted events.
|
||||||
# emit = collection.emit
|
# emit = collection.emit
|
||||||
|
Loading…
Reference in New Issue
Block a user