all known issues with event.js are fixed, no on to the unknown ones...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-11-15 02:27:20 +03:00
parent 5a7841423b
commit 5ee4b4dfd5
2 changed files with 32 additions and 13 deletions

View File

@ -41,15 +41,26 @@ function(name, func, options={}){
// NOTE: this will stop event handling if one of the handlers
// explicitly returns false...
var handle = function(){
return handlers
.reduce(function(res, handler){
return res === true
&& handler(...args) !== false }, true) }
var res
func ?
(res = func.call(this, handle, ...args))
: handle(...args)
// NOTE: to explicitly disable calling the handlers func must
// call handle(false)
// XXX should he user be able to control the args passed to
// the handlers???
var did_handle = false
var handle = function(skip=false){
did_handle = skip === true
return skip ?
undefined
: handlers
.reduce(function(res, handler){
return res === true
&& handler(name, ...args) !== false }, true) }
var res = func ?
func.call(this, handle, ...args)
: undefined
!did_handle
&& handle()
return res },
{

16
test.js
View File

@ -366,6 +366,7 @@ UniqueKeyMap.tests({
var Events = test.TestSet()
test.Case('Events', Events)
// XXX test aborting handlers...
Events.cases({
base: function(assert){
var called = {}
@ -374,6 +375,11 @@ Events.cases({
var ObjWithEvents =
assert(
object.mixinFlat({
// NOTE: we will also use virtual events later -- 'moo'
// and 'foo', these do not have to be defined to
// be usable...
// blank events...
bareEventBlank: assert(
events.bareEventMethod('bareEventBlank'),
'.bareEventMethod(..): blank'),
@ -381,20 +387,22 @@ Events.cases({
events.eventMethod('eventBlank'),
'.eventMethod(..): blank'),
// XXX test aborting handlers...
// normal events...
bareEvent: assert(events.bareEventMethod('bareEvent',
function(handle, ...args){
called['bareEvent-call'] = true
assert(handle(...args), '.bareEventMethod(..) -> handle(..)')
assert(handle(), '.bareEventMethod(..) -> handle(..)')
return 'bareEvent'
}), '.bareEventMethod(..)'),
event: assert(events.eventMethod('event',
function(handle, ...args){
called['event-call'] = true
assert(handle(...args), '.eventMethod(..) -> handle(..)')
assert(handle(), '.eventMethod(..) -> handle(..)')
}), '.eventMethod(..)'),
}, events.EventMixin),
'object with event mixin created.')
// create an "instance"...
var obj = Object.create(ObjWithEvents)
@ -453,7 +461,7 @@ Events.cases({
assert(call('event') === obj, '<obj-w-events>.event(..) return value.')
assert(call('bareEvent') == 'bareEvent', '<obj-w-events>.bareEvent(..) return value.')
obj.event(1,2,3)
// unbind...