wordpress-like hooks in Haxe?

114 views
Skip to first unread message

misterpah pah

unread,
Jan 16, 2013, 1:19:28 PM1/16/13
to haxe...@googlegroups.com
hi.. i am curious about executing any public function (in any class) by only it's name just like the php's call_user_func()
if possible, i wants the code to be generic. The question is , is it even possible ?

--------------------code------------------------------
package ;
/*
 * Hook.hx - A wordpress-like hook implementation 
 * 
 * available for flash8, flash, neko, js, php, cpp, cs, java
 */

import Reflect;
class Hook
{
public static var hooks:Array<Array<String>>;
public function new()
{
hooks = [];
}
public static function registerHook(hookName:String, registerFunction:String)
{
return hooks.push([hookName, registerFunction]);
}
public static function applyHook(hookName:String)
{
var affectedHooks:Array<Array<String>> = getRegisterHook(hookName);
/*
* i can't understand how reflects works. Please enlight me
for (i in affectedHooks)
{
var a = new Main();
Reflect.callMethod(a, Reflect.field(a, i[1]), []);
}
*/
}

private static function getRegisterHook(hookName:String)
{
var temp_hook_list:Array<Array<String>> = [];
for (i in hooks)
{
if (i[0] == hookName)
{
temp_hook_list.push(i);
}
}
return temp_hook_list;
}
}
---------------------------usage----------------------------------------------

function new()
{
new Hook();
Hook.registerHook("test", "testfunc");
Hook.registerHook("test", "testfunc2");
trace(Hook.hooks);
Hook.applyHook("test");
}

public function testfunc()
{
trace("calling from testfunc");
}
public function testfunc2()
{
trace("calling from testfunc2");
}

------------------------------------------------------------------------

Franco Ponticelli

unread,
Jan 16, 2013, 1:29:36 PM1/16/13
to haxe...@googlegroups.com
You are several ways to deal with this situation:

1. Don't pass the function name but pass a function reference:
registerHook(hookName : String, handler : Void -> Void) { // you can pass arguments if you want/need specifying another signature
// here you store the handler in Hash<Array<Void -> Void>> object
// call using handler()
2. Pass an instance and specify the method name:
registerHook(hookName : String, instance : {}, functionName : String)
// store in Hash<Array<{ instance : {}, functionName : String }>>
// call by using Reflect.callMethod(instance, Reflect.field(instance, functionName))
3. use the observer pattern or an event system


Axel Huizinga

unread,
Jan 16, 2013, 1:44:46 PM1/16/13
to haxe...@googlegroups.com

Am 16.01.2013 19:29, schrieb Franco Ponticelli:
You are several ways to deal with this situation:

1. Don't pass the function name but pass a function reference:
registerHook(hookName : String, handler : Void -> Void) { // you can pass arguments if you want/need specifying another signature
// here you store the handler in Hash<Array<Void -> Void>> object
why we would need the array here? won't it be simpler to store the function reference directly in the hash?

Cordially,
Axel

E-Mail ist virenfrei.
Von AVG überprüft - www.avg.de
Version: 2013.0.2890 / Virendatenbank: 2638/6026 - Ausgabedatum: 11.01.2013


Franco Ponticelli

unread,
Jan 16, 2013, 1:47:23 PM1/16/13
to haxe...@googlegroups.com
Just to be able to store multiple handlers for each hook name, of course if only one hook is allowed (strange in my opinion) you can always discard the array.

Axel Huizinga

unread,
Jan 16, 2013, 1:55:36 PM1/16/13
to haxe...@googlegroups.com

Am 16.01.2013 19:47, schrieb Franco Ponticelli:
Just to be able to store multiple handlers for each hook name, of course if only one hook is allowed (strange in my opinion) you can always discard the array.
Makes sense - I felt b4 it was a stupid question but want to be sure why :)

Tarwin Stroh-Spijer

unread,
Jan 16, 2013, 2:09:59 PM1/16/13
to haxe...@googlegroups.com
Question about "observer pattern". Is this where you call the methods directly, as opposed to an "event system" where you dispatch an event, and it has to be passed around until someone wants to do something with it?



Tarwin Stroh-Spijer
_______________________

Touch My Pixel
http://www.touchmypixel.com/
cell: +1 650 842 0920
_______________________

Franco Ponticelli

unread,
Jan 16, 2013, 2:54:26 PM1/16/13
to haxe...@googlegroups.com
It is probably like you say, for me is more an implementation detail. Observers require an instance of a certain interface/typedef/class where events act on methods handlers with a certain signature.

Marc Weber

unread,
Jan 16, 2013, 3:45:53 PM1/16/13
to haxelang
> public static var hooks:Array<Array<String>>;
callbacks are typed in haxe. Thus forgett about string. See this
example:

var hooks: Array<Array<Void -> Void>> = [[function(){ trace("foo"); }]];
hooks[0][0]();

Marc Weber

Franco Ponticelli

unread,
Jan 16, 2013, 3:49:04 PM1/16/13
to haxe...@googlegroups.com
Agreed, but the first array should be an Hash.


misterpah pah

unread,
Jan 17, 2013, 1:21:11 AM1/17/13
to haxe...@googlegroups.com
great stuff guys! i try it now
Reply all
Reply to author
Forward
0 new messages