Adding a object data to PeriodicalExecuter

9 views
Skip to first unread message

superruzafa

unread,
Jun 22, 2009, 12:45:28 PM6/22/09
to Prototype: Core
Hi, I needed access some object in each iteration of a
PeriodicalExecuter's function but I didn't want to use global
variables. So I modified the definition of initialize method of
PeriodicalExecuter class declaration adding a new parameter "data":

var PeriodicalExecuter = Class.create({
initialize: function(callback, frequency, data) {
this.callback = callback;
this.frequency = frequency;
this.currentlyExecuting = false;
this.data = data;
...

Then I could retrieve this object through the pe parameter of the
function called every iteration through pe.data. Would be useful to
implement officially this? There is any reason to don't do it?

Even, I think I could declare a subclass of PeriodicalExecuter like
PeriodicalExecuterParam or so.

What do you think?

superruzafa

unread,
Jun 23, 2009, 10:18:20 AM6/23/09
to Prototype: Core
I used a inherit class to implement that functionality:

var PeriodicalExecuterUserData = Class.create(PeriodicalExecuter,
{
initialize: function($super, callback, frequency, userdata)
{
this.userdata = userdata;
$super(callback, frequency);
}
});

then you can use this object calling

new PeriodicalExecuterUserData
(
function (pe)
{
// do something inteligent with pe.userdata;
pe.userdata.number++
pe.userdata.string = "string2"
pe.stop();
},
0.1,
{ number: 1, string: "string" }
);

Yaffle

unread,
Jun 23, 2009, 10:25:46 AM6/23/09
to Prototype: Core
Why you can't use closure or Function#curry based on it?

(new PeriodicalExecuter(function(someData){
var x = someData.num;
// ....
}.curry({ num: 1, text: "string" })));

Reply all
Reply to author
Forward
0 new messages