feat($timeout): pass arguments in callback #10631

Closed
wants to merge 1 commit into
from

Projects

None yet

7 participants

@gdi2290
Member
gdi2290 commented Jan 4, 2015

PR has updated Docs and Tests
setTimeout allows you to pass arguments into the callback function in
the …3rd argument. Since that’s taken I added it to the …4th
http://mdn.io/setTimeout#Syntax

here is an example of why you would need to do this

import {$http} from 'angular.js/modules';
import {setTimeout, Promise} from 'facade/async';
var cache = {};

export class AwesomeModule {
  static getSwag(url:string):Promise {
    return new Promise(function(resolve, reject) {
      var complete = function(data) {
        cache[url] = data;
        resolve(data);
      };

      if (url in cache) {
        setTimeout(complete, 0, cache[url]);
      } else {
        $http.get(url).then(complete, reject);
      } // end url in cache
    });
  } // end getSwag
} // end AwesomeModule

similar pull request #10632

@googlebot googlebot added the cla: yes label Jan 4, 2015
@gkalpak
Member
gkalpak commented Jan 4, 2015

Note that according to MDN, IE9 does not support the additional arguments.

@gdi2290
Member
gdi2290 commented Jan 4, 2015

due to the way angular is handling setTimeout intervally this code works in IE9 even if setTimeout doesn't support it natively

@gkalpak
Member
gkalpak commented Jan 4, 2015

@gdi2290: Good point ;) BTW, if it gets merged, it would be good to have the docs updatd as well (imo).

@gdi2290
Member
gdi2290 commented Jan 4, 2015

I added docs

* @param {...*=} Pass additional parameters to the executed function.
@gkalpak gkalpak commented on the diff Jan 4, 2015
src/ng/timeout.js
deferred = (skipApply ? $$q : $q).defer(),
promise = deferred.promise,
timeoutId;
timeoutId = $browser.defer(function() {
try {
- deferred.resolve(fn());
+ deferred.resolve(fn.apply(null, args));
@gkalpak
gkalpak Jan 4, 2015 Member

I wonder if making the same optimisation as in $interval here would make any tangible difference. E.g.:

var hasArgs = arguments.length > 3;
...
deferred.resolve(hasArgs ? fn.apply(null, args) : fn());
@shahata
shahata Jan 4, 2015 Contributor

I'd go the other way and make this change a simple one liner:

deferred.resolve(fn.apply(null, sliceArgs(arguments, 3)));

there's no need for this hasArgs variable and no need to test the arguments.length on your own, sliceArgs will take care of it just fine.

@gdi2290
gdi2290 Jan 4, 2015 Member

I don't have reference to arguments in that scope so the best I can do is sliceArgs regardless of argument length

//...
var args = sliceArgs(arguments, 3)
//...
  //...
  deferred.resolve(fn.apply(null, args));
@shahata
shahata Jan 4, 2015 Contributor

👍

@lgalfaso
Member
lgalfaso commented Jan 4, 2015

this and #10632 look good, but I would like to see a few more tests that make sure that ngMock also works as expected

@petebacondarwin
Member

LGTM

@petebacondarwin petebacondarwin added this to the 1.4.0-beta.2 / 1.3.11 milestone Jan 21, 2015
@petebacondarwin
Member

Feel free to merge

@gdi2290
Member
gdi2290 commented Jan 22, 2015

I need to update this PR due to the API change of $timeout 5a60302 but I should be good to go soon

@gdi2290
Member
gdi2290 commented Jan 22, 2015

@petebacondarwin PR updated with rebase from master branch. Resolving merge conflicts are a pain I have to say.

Since the $timeout overload doesn't affect this feature too much I didn't account for it in the code (no callback so no point providing noop it arguments). I could check if they're using method overload and use $browser.defer(noop, delay) rather than having it run all the logic needed for the original api. The other PR (5a60302) didn't do this optimization so I can assume it doesn't matter

@gdi2290 gdi2290 feat($timeout): pass arguments in callback
setTimeout allows you to pass arguments into the callback function in
the …3rd argument. Since that’s taken I added it to the …4th
mdn.io/setTimeout#Syntax
76b7b48
@petebacondarwin petebacondarwin self-assigned this Mar 3, 2015
@petebacondarwin
Member

Welcome to our world (rebase hell)!

I think it is fine not to optimize the case where no handler function is passed for now.

Merging

@petebacondarwin petebacondarwin added a commit that closed this pull request Mar 9, 2015
@gdi2290 @petebacondarwin gdi2290 + petebacondarwin feat($timeout): pass additional arguments to the callback
Similar to how [`setTimeout`](mdn.io/setTimeout#Syntax) works, this commit
allows users of `$timeout` to add additional parameters to the call, which
will now be passed on to the callback function.

Closes #10631
3a4b6b8
@gdi2290 gdi2290 deleted the gdi2290:timeout-args branch Mar 9, 2015
@hansmaad hansmaad pushed a commit to hansmaad/angular.js that referenced this pull request Mar 10, 2015
@gdi2290 gdi2290 + hansmaad feat($timeout): pass additional arguments to the callback
Similar to how [`setTimeout`](mdn.io/setTimeout#Syntax) works, this commit
allows users of `$timeout` to add additional parameters to the call, which
will now be passed on to the callback function.

Closes #10631
9551af6
@netman92 netman92 added a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
@gdi2290 @netman92 gdi2290 + netman92 feat($timeout): pass additional arguments to the callback
Similar to how [`setTimeout`](mdn.io/setTimeout#Syntax) works, this commit
allows users of `$timeout` to add additional parameters to the call, which
will now be passed on to the callback function.

Closes #10631
6f68f45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment