Download the PHP package colinhdev/libasyncevent without Composer
On this page you can find all versions of the php package colinhdev/libasyncevent. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download colinhdev/libasyncevent
More information about colinhdev/libasyncevent
Files in colinhdev/libasyncevent
Package libasyncevent
Short Description Simple implementation for creating asynchronous event execution for PocketMine-MP plugins.
License GPL-3.0-or-later
Informations about the package libasyncevent
libAsyncEvent
libAsyncEvent provides you with multiple implementations for creating asynchronous event execution for PocketMine-MP plugins.
Why should I use this library?
I came to the idea for this library while implementing libasynql and await-generator to my plugin CPlot. I then realised how annoying it is, to deal with PocketMine-MP's events in this case, since it is not possible e.g. to check if a player is allowed to build in a certain area with an asynchronous-run query.
So when deciding to implement custom events into the plugin, I wanted to make it as developer-friendly as possible. So when someone decides to work with the events, they are not forced to directly decide, how to react (e.g. cancelling the event) and be allowed e.g. to run asynchronous queries to validate their decision.
How to use this library in my plugin?
How should my event class look like?
Normally your event class looks like this: It extends PocketMine-MP's Event
class or one of its subclasses and maybe
also implements an interface like the Cancellable
one.
First, implement the AsyncEvent
interface, which this library provides, to your event class.
And second, use one of the EventHandlerExecutionTrait
s in your event class:
There are multiple EventHandlerExecutionTrait
s, which you can use and each of them has a different behaviour:
ConsecutiveEventHandlerExecutionTrait
: This trait will execute all event listeners one after another. If oneblock()
s the execution, the next listener will only be executed after the current one callsrelease()
.PriorityEventHandlerExecutionTrait
: This trait will execute all event listeners of the same priority at the same time. If one or more listenersblock()
the execution, the listeners of the higher priority will only be executed after all of theblock()
ing ones calledrelease()
.
How can I call my event?
You can simply call your event by creating a new event instance and using its call()
method.
But to get the result of the event, you need to provide a callback function which will be run when all listeners are finished.
How to improve my event class?
Unless both you and the person trying to register a listener for your async event use composer, they will not be able to
correctly see libAsyncEvent's declared methods like block()
or release()
in their IDE.
Although ideally composer should be used to develop plugins and to declare their dependencies, we can not force anyone to do so. So to make it easier for them, you can add following PHPDoc comments to your event class:
This way, the IDE will know that these methods exist and will not show any errors.
How to handle an event made with this library?
To register an event listener for an async event, you can do it the same way, you would normally do it for every other event listener:
In this class, you as well create a method which accepts the event, you want to listen to, as a parameter:
So basically there is no difference between a normal event listener and our async event listener.
But if you want to keep the event instance from continuing with its execution, you need to call the block()
method.
This way the event won't finish its execution until you call the release()
method.
If you accidentally forget to call the release()
method, an exception will be thrown once the event instance is
destroyed by the garbage collector.