Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Suggested addition to Function Methods: .repeat(seconds[, arg...])
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 27 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Rick Waldron  
View profile  
 More options Jun 23 2009, 11:25 am
From: Rick Waldron <waldron.r...@gmail.com>
Date: Tue, 23 Jun 2009 11:25:28 -0400
Local: Tues, Jun 23 2009 11:25 am
Subject: Suggested addition to Function Methods: .repeat(seconds[, arg...])

I detest the way setInterval() looks, so I came up with this... have been
using it my personal JS for some time.

Object.extend(Function.prototype, {
  repeat: function() {
    var __method = this, args = $A(arguments), interval = args.shift() *
1000;
    return window.setInterval(function() {
      return __method.apply(__method, args);
    }, interval );
  }

});

// usage:
var _pollInt = 0;
function repetiousPollFn() {
 console.log(_pollInt++);

}

repetiousPollFn.repeat(.5);

Will, of course, repeat repetiousPollFn() every half second.

Almost identical to .delay(), except that it returns setInterval instead of
setTimeout. One thing I intend to add is support for clearInterval, however
I figured I'd at least bring it up here first. I've never
proposed/contributed here before (i'm a lurker of the list :D ) - any
guidance is appreciated.

Rick


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
joe t.  
View profile  
 More options Jun 24 2009, 8:55 am
From: "joe t." <thooke...@gmail.com>
Date: Wed, 24 Jun 2009 05:55:00 -0700 (PDT)
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
Only suggestion i can think of is that your method strongly implies a
requirement that "repetitionsPollFn.repeat()" get assigned to a
variable so it can be cleared later:

var interval = repetitiousPollFn.repeat(0.5);
// interval is the setInterval ID

then later
window.clearInterval(interval);

i could also see potential for more "scheduling" options
(repeatUntilTime, repeatXTimes, etc) built into your #repeat idea, but
don't really have the time to flesh them out.

i find it an interesting idea though. :)
-joe t.

On Jun 23, 11:25 am, Rick Waldron <waldron.r...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tobie Langel  
View profile  
 More options Jun 24 2009, 9:00 am
From: Tobie Langel <tobie.lan...@gmail.com>
Date: Wed, 24 Jun 2009 06:00:39 -0700 (PDT)
Local: Wed, Jun 24 2009 9:00 am
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
You might also want to look at the already existing
PeriodicalExecuter.

Any implementation of a Function#repeat API should take this in
account.

On Jun 24, 2:55 pm, "joe t." <thooke...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Kieffer  
View profile  
 More options Jun 24 2009, 9:55 am
From: Robert Kieffer <bro...@gmail.com>
Date: Wed, 24 Jun 2009 06:55:34 -0700 (PDT)
Local: Wed, Jun 24 2009 9:55 am
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
I can't say I'm a big fan of this.  For several reasons.

First, it's just a cosmetic replacement for setInterval(myfunction
(...).bind(), ...) which simply isn't all that bad.

Second, I'm not a fan of setInterval in general.  I've seen some
rather nasty behavior with calls queuing up if the invoked function
takes longer than the delay to execute.  In particular, this seems to
be an issue if you do something like put a laptop to sleep.  (But
maybe others haven't seen this problem???)  Thus, I prefer to use a
self-invoking timeout like so:

function myFunction() {
    // do stuff ...
    // call ourselves again
    if (/*we want to continue?*/) setTimeout(myFunction, 1000)

}

This doesn't call the function at exactly one second intervals, but
that type of accuracy is rarely important.  Instead, it guarantees you
have at least one second of delay between invocations, which for
distributing cpu load or polling (the more common cases where
setInterval might be used), is more desireable.

Finally, as Joe T. points out, there should be a way of cancelling the
interval that doesn't require the user to store the returned value
(*that* is what I find most annoying, not the syntax of
"setInterval").

Thus, I'd suggest this instead:

  Object.extend(Function.prototype, {
    repeat: function(delay) {
      // Reset state
      if (this._repeater) delete this._repeater;
      this._repeatTimeout = clearTimeout(this._repeatTimeout);

      if (!delay) return; // (stop repeating if no args or delay==0)

      // Create setTimeout-based invoker
      var _method = this;
      if (!this._repeater) this._repeater = function() {
        // Let _method cancel repeat by doing "return false;"
        if (_method() !== false) setTimeout(_method._repeater, delay);
      }

      // Start repeating
      this._repeatTimeout = setTimeout(this._repeater, delay);
    },

    stopRepeating: function() {
      this.repeat();
    }
  });

For example:

  var count = 0;
  function foo() {
    console.log(count++);
    return count < 10;  // Return "false" when count >= 10 to cancel
the repeat
  }

  // Start repeating 1/sec
  foo.repeat(1000);
  //... some time later change interval to 2/sec
  foo.repeat(500);
  // ... later still stop repeating.
  foo.stopRepeating();

As you can see, this implementation of repeat() does a lot more for
you than simply alias'ing "setInterval":
  - It guarantees your function is only invoked by one interval
  - It makes changing the interval or cancelling it altogether
trivial.
  - It allows you to conditionally cancel the repeat from w/in the
function itself.

The only thing missing is the bind() behavior but, well, that's what
bind is for.   If you need to bind arguments, just bind() your
arguments first.

On Jun 23, 8:25 am, Rick Waldron <waldron.r...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rick Waldron  
View profile  
 More options Jun 24 2009, 10:49 am
From: Rick Waldron <waldron.r...@gmail.com>
Date: Wed, 24 Jun 2009 10:49:09 -0400
Local: Wed, Jun 24 2009 10:49 am
Subject: Re: [Prototype-core] Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])

This is fantastic feedback - thanks!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rick Waldron  
View profile  
 More options Jun 24 2009, 10:50 am
From: Rick Waldron <waldron.r...@gmail.com>
Date: Wed, 24 Jun 2009 10:50:10 -0400
Local: Wed, Jun 24 2009 10:50 am
Subject: Re: [Prototype-core] Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])

I've subbed my implementation with your to do some use-case testing. I'll
report back anything of interest as I go along.

Rick

On Wed, Jun 24, 2009 at 10:49 AM, Rick Waldron <waldron.r...@gmail.com>wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tobie Langel  
View profile  
 More options Jun 24 2009, 12:53 pm
From: Tobie Langel <tobie.lan...@gmail.com>
Date: Wed, 24 Jun 2009 09:53:33 -0700 (PDT)
Local: Wed, Jun 24 2009 12:53 pm
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
Just to clarify the above: Prototype Core already contains a similar
functionality: PeriodicalExecuter. The API is different but the
functionality is the same.

I'd strongly suggest looking into combining both approaches if you
want your suggestion to be included in core and not just stay a thread
in the mailing list. :)

Best,

Tobie

On Jun 24, 4:50 pm, Rick Waldron <waldron.r...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Yaffle  
View profile  
 More options Jun 24 2009, 1:28 pm
From: Yaffle <vic99...@yandex.ru>
Date: Wed, 24 Jun 2009 10:28:38 -0700 (PDT)
Local: Wed, Jun 24 2009 1:28 pm
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
I use Function.prototype.watch instead of Abstract.TimedObserver

Function.prototype.watch = function(delay,onChange){
  var __method = this,
      value    = __method();
  return setInterval(function(){
    var v = __method();
        if(v!==value){
          onChange(v);
          value = v;
        }
  },delay*1000);

};

May be you combine it too? =)

On Jun 24, 10:53 pm, Tobie Langel <tobie.lan...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Kieffer  
View profile  
 More options Jun 24 2009, 3:39 pm
From: Robert Kieffer <bro...@gmail.com>
Date: Wed, 24 Jun 2009 12:39:14 -0700 (PDT)
Local: Wed, Jun 24 2009 3:39 pm
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
FWIW, PeriodicalExecuter has always struck me as a bit of a wart on
the Prototype API. The name and usage are awkward, and it's
functionality really feels like something that should just be a
Function extension, as we're seeing in this thread.  I'm assuming it's
presence is mostly a legacy thing.

But... I'm not really arguing for it's removal.  Just pissing into the
wind.  :-)

On Jun 24, 9:53 am, Tobie Langel <tobie.lan...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rick Waldron  
View profile  
 More options Jun 24 2009, 5:28 pm
From: Rick Waldron <waldron.r...@gmail.com>
Date: Wed, 24 Jun 2009 17:28:02 -0400
Local: Wed, Jun 24 2009 5:28 pm
Subject: Re: [Prototype-core] Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])

Tobie,

I had in fact looked into PeriodicalExecuter and to be perfectly honest,
with no offense intended, i think it's usage syntax is hideous which is what
led me to writing my own Function.prototype method instead, taking
inspiration from .delay()

It doesnt matter to me whether or not .repeat() makes it into the prototype
core - I will always have it at my disposal, I simply wanted to share the
concept with the group.

I think everything I've said sounds like I'm in a bad mood, sorry about
that! :D

Rick

On Wed, Jun 24, 2009 at 12:53 PM, Tobie Langel <tobie.lan...@gmail.com>wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tobie Langel  
View profile  
 More options Jun 24 2009, 10:04 pm
From: Tobie Langel <tobie.lan...@gmail.com>
Date: Wed, 24 Jun 2009 19:04:45 -0700 (PDT)
Local: Wed, Jun 24 2009 10:04 pm
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
Hi Rick, hi Robert.

Fully agree regarding PE. It does however handle issues a regular
setInterval doesn't (as you mentioned).

It's clearly an area which would need refinement, but that's better
done in a backwards compatible way.

Personally, I'd love to see PE implemented as a method of Function
instances.

So since this is the core mailing list, I'm trying to give directions
on how it could be best implemented given legacy constraints and
consistency with the rest of the API.

Clearly, consistency with Function#defer and Function#delay would
imply returning a setInterval index. Unfortunately, since setTimeout
is used instead of setInterval, this isn't possible.

Maybe the simplest solution would be to make Function#repeat return an
instance of PE. Implementation would then be roughly:

Function.prototype.repeat = function(interval) {
  return new PeriodicalExecuter(this, interval);

}

Note that in that case, modifying Function#delay and Function#defer to
return a PE-like object instance with a stop method would make more
sense API wise, but at the expense of a larger memory footprint and
backwards compatibility.

As you can see, making the right choice isn't simple.

Best,

Tobie

On Jun 24, 11:28 pm, Rick Waldron <waldron.r...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rick Waldron  
View profile  
 More options Jun 25 2009, 1:23 pm
From: Rick Waldron <waldron.r...@gmail.com>
Date: Thu, 25 Jun 2009 13:23:25 -0400
Local: Thurs, Jun 25 2009 1:23 pm
Subject: Re: [Prototype-core] Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])

Thanks for the insight Tobie, definitely appreciated.

Function.prototype.repeat = function(interval) {
 return new PeriodicalExecuter(this, interval);

}

How to stop it? arguments?

This may come to you twice, but this is slightly updated:

http://jsbin.com/opimu

This repeat() method def is 775Bytes, accepts arguments like delay/defer,
uses setTimeout (returns initial setTimeout index) and has a stopping
mechanism.

Rick

On Wed, Jun 24, 2009 at 10:04 PM, Tobie Langel <tobie.lan...@gmail.com>wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tobie Langel  
View profile  
 More options Jun 26 2009, 6:02 am
From: Tobie Langel <tobie.lan...@gmail.com>
Date: Fri, 26 Jun 2009 03:02:06 -0700 (PDT)
Local: Fri, Jun 26 2009 6:02 am
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])

> How to stop it? arguments?

Stopping it is as easy as:

pe = foo.repeat();
pe.stop();

Passing arguments would require some simple currying:

Function.prototype.repeat = function(interval) {
 var fn = this;
 if (arguments.length > 1) {
   // not testsed but you get the idea
   fn = fn.curry.apply(fn, Array.prototype.slice.call(arguments, 1));
 }
 return new PeriodicalExecuter(fn, interval);

}
> This may come to you twice, but this is slightly updated:

> http://jsbin.com/opimu

> This repeat() method def is 775Bytes, accepts arguments like delay/defer,
> uses setTimeout (returns initial setTimeout index) and has a stopping
> mechanism.

What's the point in returning setTimeout index? That will set
expectations which can't be met: developers will expect to be able to
stop the functions calls by clearing it.

Are you sure your proposal fixes all of the small issues PE fixes? For
example, does it guarantee that the function will continue being
called if it happens to once throw an error. Does it avoid calling the
function again if a previous function is still executing, etc.?

I understand your eagerness to move away from a model you dislike, but
that shouldn't make you throw away all of the work that's been put
into previous solutions.

FWIW, I just noticed a patch wasn't applied to PE in current trunk
(it's missing a throw statement).

Best,

Tobie


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
joe t.  
View profile  
 More options Jun 26 2009, 9:46 am
From: "joe t." <thooke...@gmail.com>
Date: Fri, 26 Jun 2009 06:46:54 -0700 (PDT)
Local: Fri, Jun 26 2009 9:46 am
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
Tobie,

> Function.prototype.repeat = function(interval) {
>  var fn = this;
>  if (arguments.length > 1) {
>    // not testsed but you get the idea
>    fn = fn.curry.apply(fn, Array.prototype.slice.call(arguments, 1));
>  }
>  return new PeriodicalExecuter(fn, interval);

> }

If sticking to the PE approach there's no internal self-stop mechanism
(is there?), which i see as a nice touch in the above proposal (for
what my opinion is worth).
What about (from 1.6.1_rc2):

onTimerEvent: function() {
  if (!this.currentlyExecuting) {
    try {
      this.currentlyExecuting = true;
      if (this.execute()===false) // MOD
        this.stop();              // NEW
    } catch(e) {
      /* empty catch for clients that don't support try/finally */
    }
    finally {
      this.currentlyExecuting = false;
    }
  }

}

Just tossing in my 2 cents because i'm intrigued by that feature.
-joe t.

On Jun 26, 6:02 am, Tobie Langel <tobie.lan...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Samuel Lebeau  
View profile  
 More options Aug 25 2009, 10:33 pm
From: Samuel Lebeau <samuel.leb...@gmail.com>
Date: Wed, 26 Aug 2009 04:33:05 +0200
Local: Tues, Aug 25 2009 10:33 pm
Subject: [Prototype-core] Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
Joe,

Callback function receives PE instance as first argument, so here  
would be the self-stop mechanism :

        function(executer) { executer.stop() }.repeat()

Best,
Samuel.

On 26 juin 09, at 15:46, joe t. wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
T.J. Crowder  
View profile  
 More options Aug 27 2009, 6:17 am
From: "T.J. Crowder" <t...@crowdersoftware.com>
Date: Thu, 27 Aug 2009 03:17:07 -0700 (PDT)
Local: Thurs, Aug 27 2009 6:17 am
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
Hi all,

Weighing in...  First off, love the idea of Function#repeat.
Wonderfully simple and expressive.

As Tobie says, the goals when doing it should be to handle all of the
issues PE already handles,  to reuse existing code, and to be
consistent with other API functions.

Some notes:

1. I'd avoid requiring #curry or #bind on top of #repeat; #repeat
should be able to handle arguments and context itself. Every curry at
least doubles the call overhead.

2. Optional context would be helpful:

    Function#delay(options[, args...]) -> Number

...with `options` being either a number (frequency) or an object with
`context` and `frequency` parameters; the latter allows repeating
method calls.

3. #1 suggests that rather than #repeat using PE, the guts of PE
should become #repeat and the PE should use it:

    var PeriodicalExecuter = Class.create({
      initialize: function(callback, frequency) {
        this.handle = callback.repeat(frequency, this);
      },
      stop: function() {
        if (this.handle) {
          clearInterval(this.handle);
          this.handle = 0;
        }
      }
    });

4. I'd like to be able to repeat functions not explicitly designed to
#repeat, which suggests not changing their signature. Perhaps the self-
stop mechanism could remain a feature that PE adds on top of #repeat
(at the cost that PE functions have to be explicitly intended to be
repeated [or must ignore their args], as is currently the case).

5. Like Robert, I prefer the self-repeat over setInterval (that's
always how I do it), but unless we've seen significant issues with
setInterval we should be wary of changing it, especially in light of
how doing so complicates the API (e.g., return value issues). Perhaps
this aspect is a separate question entirely.

6. If we do need to use something other than an interval handle as the
return value, I'd suggest providing a stop function that works with
both interval handles and whatever our new thing is, e.g.:

    Function.stop = function(handle) {
      if (typeof handle == 'number') {
        clearInterval(handle);
      }
      else {
        /* ...stop it the new way... */
      }
    };

@Rick: All of this discussion probably seems like nit-picking your
idea to death. In fact, I think it indicates that there's a lot of
support and appreciation for your idea, and we're all (well, nearly
all, there's a dissenter) just trying to make it fit in, and make it
as cool as the idea warrants.
--
T.J. Crowder
tj / crowder software / com

On Aug 26, 3:33 am, Samuel Lebeau <samuel.leb...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Kieffer  
View profile  
 More options Aug 27 2009, 11:13 am
From: Robert Kieffer <bro...@gmail.com>
Date: Thu, 27 Aug 2009 08:13:10 -0700
Local: Thurs, Aug 27 2009 11:13 am
Subject: Re: [Prototype-core] Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])

@TJ...

Your goals sound about right.  Implicit in there, I think, is "make it easy
to deprecate PE at some later date".  At least, that's how I read it. :)

Re: #2 - Why not just use bind() to provide context?   I've never been a fan
of overloading arguments with multiple interpretations.   'Gets too
confusing, and makes the implementation that much uglier.

Re: #4 - I feel the ability to self-stop is more important than being able
to repeat() functions that aren't designed for it.  I _really_ feel that
making the self-stop behavior an optional add-on feature of PE is a bad
idea.  self-stop is an elegant pattern (IMHO) so forcing users to go through
the awkward PE API to get at it doesn't make sense.  Also, it makes
deprecating PE harder.

***Hmm...*** How about this as a solution:  Instead of having a function
return false to stop, have it "throw $break"!  I think that addresses your
concern, and is consistent with how Prototype does this sort of thing
elsewhere (in Enumerable).


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
T.J. Crowder  
View profile  
 More options Aug 27 2009, 11:29 am
From: "T.J. Crowder" <t...@crowdersoftware.com>
Date: Thu, 27 Aug 2009 08:29:11 -0700 (PDT)
Local: Thurs, Aug 27 2009 11:29 am
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
Hey Robert,

It's true, I was thinking in terms of deprecating PE at some
stage... :-)

Re #2: Using #bind at least doubles the call overhead, which I'm not a
fan of generally.

> ...have it "throw $break"!

Now that is a very smart idea.
--
T.J. Crowder
tj / crowder software / com

On Aug 27, 4:13 pm, Robert Kieffer <bro...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tobie Langel  
View profile  
 More options Aug 27 2009, 11:51 am
From: Tobie Langel <tobie.lan...@gmail.com>
Date: Thu, 27 Aug 2009 08:51:50 -0700 (PDT)
Local: Thurs, Aug 27 2009 11:51 am
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
Food for thought:

1. We would like to completely decouple native and host objects in the
LANG section for version 1.7.

`setTimeout` and `setInterval` are host objects...

2. We're planning strict ES 5 compliance of enumerables for 1.7. that
implies removing $break.

Best,

Tobie

On Aug 27, 5:29 pm, "T.J. Crowder" <t...@crowdersoftware.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rick Waldron  
View profile  
 More options Aug 27 2009, 1:08 pm
From: Rick Waldron <waldron.r...@gmail.com>
Date: Thu, 27 Aug 2009 13:08:40 -0400
Local: Thurs, Aug 27 2009 1:08 pm
Subject: Re: [Prototype-core] Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])

@TJ

Re: @Rick: All of this discussion probably seems like nit-picking your
idea to death. In fact, I think it indicates that there's a lot of
support and appreciation for your idea, and we're all (well, nearly
all, there's a dissenter) just trying to make it fit in, and make it
as cool as the idea warrants.

I love it! How else are we going to make a good idea into an awesome idea?
Such is why i brought it to the group - I'm just pleased that it wasn't
discarded. I'm excited to see the process of it's evolution and hope it
continues to do so and eventually to see Function#repeat() make its way into
Prototype - but only the best possible version of the method!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
T.J. Crowder  
View profile  
 More options Aug 27 2009, 2:38 pm
From: "T.J. Crowder" <t...@crowdersoftware.com>
Date: Thu, 27 Aug 2009 11:38:05 -0700 (PDT)
Local: Thurs, Aug 27 2009 2:38 pm
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
@Rick:

Rock on!  That's the spirit!

@Tobie:

*blech* to ES5's enumerable stuff not having $break or similar
functionality.  I've just read the forEach section of the draft spec
from a while back, and I'm not seeing a discussion of exception
handling.  I haven't delved deep, though -- do you know offhand how
exceptions in the callback function are handled?  E.g, can one
implement one's own $break handling, or are exceptions eaten?

Re setInterval and setTimeout, how do you see implementing #delay or
similar without using them?  Or do you not see it, e.g., a pure LANG
version of Prototype wouldn't have Function#defer.

--
T.J. Crowder
tj / crowder software / com

On Aug 27, 6:08 pm, Rick Waldron <waldron.r...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tobie Langel  
View profile  
 More options Aug 27 2009, 5:49 pm
From: Tobie Langel <tobie.lan...@gmail.com>
Date: Thu, 27 Aug 2009 14:49:10 -0700 (PDT)
Local: Thurs, Aug 27 2009 5:49 pm
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])

> *blech* to ES5's enumerable stuff not having $break or similar
> functionality.  I've just read the forEach section of the draft spec
> from a while back, and I'm not seeing a discussion of exception
> handling.  I haven't delved deep, though -- do you know offhand how
> exceptions in the callback function are handled?  E.g, can one
> implement one's own $break handling, or are exceptions eaten?

Exceptions aren't eaten.

> Re setInterval and setTimeout, how do you see implementing #delay or
> similar without using them?

You can't.

> Or do you not see it, e.g., a pure LANG
> version of Prototype wouldn't have Function#defer.

Maybe Function#defer would be defined only if window.setTimeout was
present.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
T.J. Crowder  
View profile  
 More options Aug 27 2009, 9:25 pm
From: "T.J. Crowder" <t...@crowdersoftware.com>
Date: Thu, 27 Aug 2009 18:25:31 -0700 (PDT)
Local: Thurs, Aug 27 2009 9:25 pm
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])

> Exceptions aren't eaten.

Good, so a $break-like mechanism is possible then, just moved out a
level.

-- T.J.

On Aug 27, 10:49 pm, Tobie Langel <tobie.lan...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
joe t.  
View profile  
 More options Aug 28 2009, 10:59 am
From: "joe t." <thooke...@gmail.com>
Date: Fri, 28 Aug 2009 07:59:11 -0700 (PDT)
Local: Fri, Aug 28 2009 10:59 am
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
Unfortunately for me, i don't offer much on the deeply technical side,
but want to offer my vote to explore this idea. The self-stop is
definitely an attractive feature, and while the examples that i've
seen in this thread show a stop-on-return-false, it would probably be
more correct to stop-on-provided-condition:

[snipped from Robert's post]
  Object.extend(Function.prototype, {
    repeat: function(delay, stopCondition) {
      this.stopCondition = typeof stopCondition == "function" ?
        stopCondition : function(test){return test ===
stopCondition;}; // or $break-like mechanism?
...
      if (!this._repeater) this._repeater = function() {
        // Let _method cancel repeat by testing against stopCondition
        if (!this.stopCondition(_method())) setTimeout
(_method._repeater, delay);
      }
...
[/snip]

i'm obviously no expert. Robert mentions not being a fan of argument
interpretation, which makes sense. But in this case, the only
"exception" is for a function, and only occurs when the repeat is
created. And one strike against the above is if the user provides a
time-consuming stopCondition function, it increases the time to the
next repeat. Unless some timing math is done on either side of the
stopCondition call to balance the setTimeout delay:

setTimeout(_method._repeater, delay - (timeAfterStopCondition -
timeBeforeStopCondition));

...which is bad when the time differential is > delay. :(

For what it's worth, i'd like to see allowance of any object type to
be the stop condition, but if there are greater issues preventing
that, i defer to the better judgment of the developers. :)

-joe t.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Kieffer  
View profile  
 More options Aug 28 2009, 12:30 pm
From: Robert Kieffer <bro...@gmail.com>
Date: Fri, 28 Aug 2009 09:30:08 -0700 (PDT)
Local: Fri, Aug 28 2009 12:30 pm
Subject: Re: Suggested addition to Function Methods: .repeat(seconds[, arg...])
@tobie: I'm a little unclear how the ES5 implementation of forEach()
impacts the idea of using $break to terminate a repeat() call.  Can
you elaborate?

Unless $break is removed from Prototype altogether, I don't see how
this is an issue.  And the only reason to remove $break is if all the
Enumerable methods (that depend on it) are also going away.  But given
that  a) ES5 doesn't support a mechanism for early termination of
forEach(), b) the "throw $break" pattern is both useful and c) ES5
doesn't support many of the Enumerable methods, it seems like those
methods will stick around and continue to recognize $break as a way of
terminating an enumeration.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 27   Newer >
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google