event notification module.
luarocks install notifier --from=http://mah0x211.github.io/rocks/
Returns
notifier:table
: an instance of notifier.
Parameters
name:string
: event name.callback:function
: callback function.ctx:any
: any context value that passing to first argument of callback function on invocation. (default:nil
)count:uint
: number of notification. if notification count reached to this value, an observer will be removed automatically. (default:0
)
Returns
notifier:table
: an instance of notifier.
Parameters
name:string
: event name.
Returns
nobs:uint
: number of notification observers
unregister an event notification observer.
Parameters
name:string
: event name.callback:function
: callback function.
Returns
notifier:table
: an instance of notifier.
Parameters
name:string
: event name....
: passed arguments of observers.
Returns
notified:number
: number of notified observer.removed:number
: number of removed observer.
local Notifier = require('notifier');
local notifier = Notifier.new();
local function cbHello( ctx, ... )
print( ctx, ... );
end
local function cbWorld( ctx, ... )
print( ctx, ... );
end
local notified, removed;
-- register
notifier
:on( 'hello', cbHello, 'hello context' )
:on( 'world', cbWorld, 'world context', 1 );
-- notify
notified, removed = notifier:notify('hello');
print( notified, removed );
-- notify with arguments
notified, removed = notifier:notify('world', 'a', 'b', 1, 2 );
print( notified, removed );
-- unregister
notifier:off( 'hello', cbHello );