added this.cancel() for canceling events inside an event handling function.
This commit is contained in:
parent
6c95da14f8
commit
d9e6cea844
4 changed files with 26 additions and 5 deletions
|
@ -58,6 +58,7 @@
|
|||
|
||||
<target name="get-canary" depends="init" description="Downloads canarymod jar" unless="canary.present">
|
||||
<get src="https://ci.visualillusionsent.net/job/CanaryMod/lastStableBuild/artifact/*zip*/archive.zip"
|
||||
maxtime="60"
|
||||
dest="target/canarymod.zip"
|
||||
verbose="true"/>
|
||||
<unzip src="target/canarymod.zip"
|
||||
|
|
|
@ -49,7 +49,16 @@ exports.on = function(
|
|||
evt.setCancelled(true);
|
||||
}
|
||||
}
|
||||
handler.call( result, evt, cancel );
|
||||
/*
|
||||
let handlers use this.cancel() to cancel the current event
|
||||
or this.unregister() to unregister from future events.
|
||||
*/
|
||||
var bound = {};
|
||||
for (var i in result){
|
||||
bound[i] = result[i];
|
||||
}
|
||||
bound.cancel = cancel;
|
||||
handler.call( bound, evt, cancel );
|
||||
}
|
||||
} );
|
||||
/*
|
||||
|
|
|
@ -30,8 +30,17 @@ exports.on = function(
|
|||
e.setCanceled();
|
||||
}
|
||||
}
|
||||
/*
|
||||
let handlers use this.cancel() to cancel the current event
|
||||
or this.unregister() to unregister from future events.
|
||||
*/
|
||||
var bound = {};
|
||||
for (var i in result){
|
||||
bound[i] = result[i];
|
||||
}
|
||||
bound.cancel = cancel;
|
||||
try {
|
||||
handler.call(result, e, cancel);
|
||||
handler.call(bound, e, cancel);
|
||||
} catch ( error ){
|
||||
console.log('Error while executing handler:' + handler +
|
||||
' for event type:' + eventType +
|
||||
|
|
|
@ -74,9 +74,11 @@ events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, c
|
|||
```
|
||||
|
||||
The `this` keyword when used inside the callback function refers to
|
||||
the Listener object created by ScriptCraft. It has a single method
|
||||
`unregister()` which can be used to stop listening. This is the same
|
||||
object which is returned by the `events.on()` function.
|
||||
the Listener object created by ScriptCraft. It has 2 methods
|
||||
`unregister()` which can be used to stop listening and `cancel()`
|
||||
which can be used to cancel the current event. The object returned by
|
||||
`events.on()` only has the `unregister()` method, the `cancel()`
|
||||
method is only available from within the event handling function.
|
||||
|
||||
To unregister a listener *outside* of the listener function...
|
||||
|
||||
|
|
Reference in a new issue