Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

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

Closed
wants to merge 1 commit into from

Conversation

PatrickJS
Copy link
Member

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

@gkalpak
Copy link
Member

gkalpak commented Jan 4, 2015

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

@PatrickJS
Copy link
Member Author

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

@gkalpak
Copy link
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).

@PatrickJS
Copy link
Member Author

I added docs

* @param {...*=} Pass additional parameters to the executed function.

deferred = (skipApply ? $$q : $q).defer(),
promise = deferred.promise,
timeoutId;

timeoutId = $browser.defer(function() {
try {
deferred.resolve(fn());
deferred.resolve(fn.apply(null, args));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@lgalfaso
Copy link
Contributor

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
Copy link
Member

LGTM

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

Feel free to merge

@PatrickJS
Copy link
Member Author

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

@PatrickJS PatrickJS force-pushed the timeout-args branch 2 times, most recently from ae4720e to 7476a02 Compare January 22, 2015 04:29
@PatrickJS
Copy link
Member Author

@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

@PatrickJS PatrickJS force-pushed the timeout-args branch 5 times, most recently from 4c4cfe1 to 1472151 Compare January 23, 2015 04:37
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
@petebacondarwin
Copy link
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

@PatrickJS PatrickJS deleted the timeout-args branch March 9, 2015 20:25
hansmaad pushed a commit to hansmaad/angular.js that referenced this pull request Mar 10, 2015
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 angular#10631
netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
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 angular#10631
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants