Package: node-events
node_events.pyA minor rewrite of the NodeJS EventEmitter in PythonInstallingViaPyPI:pipinstallnode_eventsUsagefromnode_eventsimportEventEmittermyEmitter=EventEmitter()deffn():print("An event occurred")myEmitter.on('event',fn)myEmitter.emit('event')# Prints#   An event occurredAPIClass:EventEmitterTheEventEmitterclass is defined and exposed publicly by the module:fromnode_eventsimportEventEmitterEventEmitter.addListener(eventName, listener)eventName: <string>listener: <function>Returns: <EventEmitter>Alias forself.on(eventName, listener)Emits the'addlistener:{eventName}'event when called.EventEmitter.on(eventName, listener)eventName: <string> The name of the event.listener: <function> The callback function.Returns: <EventEmitter>Appends thelistenerto the listeners array for the event namedeventName. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.Emits the'addlistener:{eventName}'event when called.By default, event listeners are invoked in the order they are added. Theemitter.prependListener()method can be used as an alternative to add the event listener to the beginning of the listeners array.Returns a reference to theEventEmitter, so that calls can be chained.emitter=EventEmitter()defappendListener():print('a')defprependListener():print('b')emitter.on('test',appendListener)emitter.prependListener('test',appendListener)emitter.emit('test')# Prints#   b#   aEventEmitter.once(eventName, listener)eventName: <string> The name of the event.listener: <function> The callback function.Returns: <EventEmitter>Adds aone-timelistenerfunction for the event namedeventName. The next timeeventNameis triggered, this listener is removed and then invoked.Emits the'addlistener:{eventName}'event when called.By default, event listeners are invoked in the order they are added. Theemitter.prependOnceListener()method can be used as an alternative to add the event listener to the beginning of the listeners array.Returns a reference to theEventEmitter, so that calls can be chained.emitter=EventEmitter()defappendListener():print('a')defprependListener():print('b')emitter.once('test',appendListener)emitter.prependOnceListener('test',appendListener)emitter.emit('test')# Prints#   b#   aEventEmitter.prependListener(eventName, listener)eventName: <string> The name of the event.listener: <function> The callback function.Returns: <EventEmitter>Adds thelistenerfunction to the beginning of the listeners array for the event namedeventName. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.Emits the'addlistener:{eventName}'event when called.emitter=EventEmitter()defnewConnection():print('someone connected!')emitter.prependListener('connection',newConnection)emitter.emit('connection')Returns a reference to theEventEmitter, so that calls can be chained.EventEmitter.prependOnceListener(eventName, listener)eventName: <string> The name of the event.listener: <function> The callback function.Returns: <EventEmitter>Adds aone-timelistenerfunction for the event namedeventNameto the beginning of the listeners array. The next timeeventNameis triggered, this listener is removed, and then invoked.Emits the'addlistener:{eventName}'event when called.emitter=EventEmitter()defnewConnection():print('someone connected!')emitter.prependOnceListener('connection',newConnection)emitter.emit('connection')Returns a reference to theEventEmitter, so that calls can be chained.EventEmitter.removeAllListeners([eventName])eventName: <string> The name of the event.Returns: <EventEmitter>Removes all listeners, or those of the specifiedeventName.Emits the'rmlistener:{eventName}'event when called.Returns a reference to the EventEmitter, so that calls can be chained.EventEmitter.removeListener(eventName, listener)eventName: <string>listener: <function>Returns: <EventEmitter>Removes the specifiedlistenerfrom the listener array for the event namedeventName.Emits the'rmlistener:{eventName}'event when called.EventEmitter.hasEvent(eventName, raiseException)eventName: <string>raiseException: <boolean> (Default:False)Returns: <EventEmitter>Check if the event emitter has within itself an event namedeventName, return a boolean for the operation.
ifraiseExceptionis True, raise an exception if the result of the check evaluates toFalse.EventEmitter.hasListeners(eventName)eventName: <string>raiseException: <boolean>Returns: <EventEmitter>Safely check that the coreEventListenerStackhas at least one listener.
Implements theEventListenerStack::hasListeners()inherently.Class:EventListener(listener)listener: <function>This class wraps thelistenerfunction with useful, sandboxed manipulative featuresTheEventListenerclass is defined and exposed publicly by the module:fromnode_eventsimportEventListenerdeffn():print("test_fn")EventListener(fn).respond()# Prints#   test_fnEventListener.listenerCount(getter)Number of times the function has been calledEventListener.respond(*data)*data: <any>Send thedataarguments to the encapsulated function in evaluationEventListener.verify(fn)fn: <function>Returns: <boolean>Check iffnmatches with the encapsulated function
Useful in finding the instance amongst others by matching its coreClass:EventListenerStack(eventName)eventName: <string>Stacking layer of listeners for an event defined namedeventNameServes as an interfacing remote for series of grouped listenersTheEventListenerStackclass is defined and exposed publicly by the module:fromnode_eventsimportEventListenerStackdeftest_fn():print("hi from test_fn")stack=EventListenerStack("event_name")stack.attachListener(test_fn,0)stack.respond()# Prints#   hi from test_fnEventListenerStack.listeners(getter)Return a copy of the private listeners array.EventListenerStack.listenerCount(getter)Return the number of listeners exist and are actively waiting for event firingsEventListenerStack.respond(*data)*data: <any>Returns: <boolean>Send thedataarguments to all the listeners within the stack in the order of which they appear
Returns True if the stack has any active listeners who read the data else FalseEventListenerStack.verifyHasListener(fn)fn: <function>Check if the stack has the listenerfnEventListenerStack.attachListener(fn, index)fn: <function>index: <number>Attach thefnlistener to the event stack.
Theindexparameter determines the index at which to place the function in the stack array
Note, function calls are based on orderly calls from the stack arrayEventListenerStack.detachListener(fn)fn: <function>Returns: <boolean>Detach thefnlistener from the stack if it exists returning True otherwise return FalseEventListenerStack.detachAllListeners()Detach all the listeners within the stackEventListenerStack.hasListeners()Check if the stack has any listeners withinEventListenerStack.extractInstanceOf(fn)fn: <function>Extract theEventListenerinstance encapsulating thefnlistener if it exists otherwise returnNoneDevelopmentBuildingFeel free to clone, use in adherance to thelicenseand perhaps send pull requestsgitclonehttps://github.com/miraclx/node_events.py.gitcdnode_events.py# hack on codepip3install.--userLicenseApache 2.0©Miraculous Owonubi(@miraclx) <omiraculous@gmail.com>