added this.cancel() for canceling events inside an event handling function.

This commit is contained in:
walterhiggins 2015-02-14 11:50:20 +00:00
parent 6c95da14f8
commit d9e6cea844
4 changed files with 26 additions and 5 deletions

View file

@ -58,6 +58,7 @@
<target name="get-canary" depends="init" description="Downloads canarymod jar" unless="canary.present"> <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" <get src="https://ci.visualillusionsent.net/job/CanaryMod/lastStableBuild/artifact/*zip*/archive.zip"
maxtime="60"
dest="target/canarymod.zip" dest="target/canarymod.zip"
verbose="true"/> verbose="true"/>
<unzip src="target/canarymod.zip" <unzip src="target/canarymod.zip"

View file

@ -49,7 +49,16 @@ exports.on = function(
evt.setCancelled(true); 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 );
} }
} ); } );
/* /*

View file

@ -30,8 +30,17 @@ exports.on = function(
e.setCanceled(); 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 { try {
handler.call(result, e, cancel); handler.call(bound, e, cancel);
} catch ( error ){ } catch ( error ){
console.log('Error while executing handler:' + handler + console.log('Error while executing handler:' + handler +
' for event type:' + eventType + ' for event type:' + eventType +

View file

@ -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 `this` keyword when used inside the callback function refers to
the Listener object created by ScriptCraft. It has a single method the Listener object created by ScriptCraft. It has 2 methods
`unregister()` which can be used to stop listening. This is the same `unregister()` which can be used to stop listening and `cancel()`
object which is returned by the `events.on()` function. 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... To unregister a listener *outside* of the listener function...