diff --git a/event.js b/event.js index ba904f9..7ae5f80 100644 --- a/event.js +++ b/event.js @@ -131,6 +131,8 @@ function(name, func, options={}){ }) } +module.TRIGGER = {doc: 'force event method to trigger'} + // Extends Eventfull(..) adding ability to bind events via the // resulting method directly by passing it a function... // @@ -157,6 +159,16 @@ function(name, func, options={}){ // func(handle, ...args) // // +// Special case: +// +// Force trigger event... +// method(TRIGGER, ...args) +// -> this +// +// This will pass args to the event action regardless whether the first +// arg is a function or not... +// +// var Event = module.Event = function(name, func, options={}){ @@ -169,12 +181,14 @@ function(name, func, options={}){ method = Eventfull(name, function(handle, ...args){ // add handler... - // XXX handle handler tags... if(typeof(args[0]) == 'function'){ method.__event_handler_add__(this, args[0]) // call the action... } else { + // special case: force trigger -> remove TRIGGER from args... + args[0] === module.TRIGGER + && args.shift() func && func.call(this, handle, ...args) } @@ -240,7 +254,7 @@ module.EventHandlerMixin = { trigger: function(evt, ...args){ // local handler... evt in this - && this[evt](...args) + && this[evt](module.TRIGGER, ...args) // global events... this.__event_handlers__ && (this.__event_handlers__[evt] || []) diff --git a/package.json b/package.json index f6885be..856830a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "3.7.1", + "version": "3.7.2", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": { diff --git a/runner.js b/runner.js index 28bd904..54fd618 100644 --- a/runner.js +++ b/runner.js @@ -234,6 +234,7 @@ module.Queue = object.Constructor('Queue', Array, { && running.length < (this.pool_size || Infinity) ){ var task = this.shift() + // XXX BUG this executes the task for some reson... this.trigger('taskStarting', task) // run... @@ -254,11 +255,13 @@ module.Queue = object.Constructor('Queue', Array, { .filter(function(e){ return e !== res })) // finishup... that + // XXX BUG this executes the task for some reson... .trigger('taskCompleted', task, res) ._run() }) // completed sync task... } else { + // XXX BUG this executes the task for some reson... this.trigger('taskCompleted', task, res) } } // empty queue -> pole or stop... diff --git a/test.js b/test.js index a2c2450..2ef534b 100755 --- a/test.js +++ b/test.js @@ -472,6 +472,11 @@ Events.cases({ .event() .event() assert(called['event-one-time-handler'] == 1, '.one("event", ..) handler cleared.') + delete called['event-one-time-handler'] + + // special case... + obj.trigger('event', function(){ called['trigger-function-called'] = true }) + assert(called['trigger-function-called'] === undefined, '.trigger(..) should not call it\'s args') // XXX test passing args... @@ -483,6 +488,57 @@ Events.cases({ }) +//--------------------------------------------------------------------- +// events... + +var Runner = test.TestSet() +test.Case('Runner', Runner) + +// XXX test aborting handlers... +Runner.cases({ + base: function(assert){ + //var q = assert(runner.Queue({auto_stop: true}), 'Queue()') + var q = assert(runner.Queue(), 'Queue()') + + // empty states... + assert(q.state == 'stopped', '.state: stopped') + + q.start() + + assert(q.state == 'running', '.state: running') + + q.start() + + assert(q.state == 'running', '.state: running') + + q.stop() + + assert(q.state == 'stopped', '.state: stopped') + + var tasks_run = [] + var a = function(){ tasks_run.push('a') } + var b = function(){ tasks_run.push('b') } + var c = function(){ tasks_run.push('c') } + + q.push(a) + q.push(b) + q.push(c) + + assert(q.length == 3, '.length is 3') + + // for some reason this runs the tasks above multiple times... + q.start() + + assert.array(tasks_run, ['a', 'b', 'c'], 'all tasks run') + + + + + //console.log('\n>>>', q.state) + }, +}) + + //--------------------------------------------------------------------- typeof(__filename) != 'undefined'