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

feat($interval): pass arguments in callback #10632

Closed
wants to merge 1 commit into from

Conversation

PatrickJS
Copy link
Member

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

similar pull request #10631

@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 setInterval intervally this code works in IE9 even if setInterval doesn't support it natively. I can optimise the code a bit more by checking if there are arguments before wrapping a function around it.

promise.then(null, null, function() {
  fn.apply(null, args);
});
promise.then(null, null, args.length === 0 ? fn : function() {
  fn.apply(null, args);
});

@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 and optimise the code a bit to check args once

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

expect(task2).toHaveBeenCalledWith('Task2');
$window.flush(1000);
expect(task3).toHaveBeenCalledWith('I', 'am', 'a', 'Task3', 'spy');
}));
Copy link
Member

Choose a reason for hiding this comment

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

This sequence of expectations seems not quite right. Since we are using $interval then each of the spies should have been called once per flush.

How about something more like:

$window.flush(1000);
expect(task1).toHaveBeenCalledWith('Task1');
expect(task2).toHaveBeenCalledWith('Task2');
expect(task3).toHaveBeenCalledWith('I', 'am', 'a', 'Task3', 'spy');
task1.reset();
task2.reset();
task3.reset();
$window.flush(1000);
expect(task1).toHaveBeenCalledWith('Task1');
expect(task2).toHaveBeenCalledWith('Task2');
expect(task3).toHaveBeenCalledWith('I', 'am', 'a', 'Task3', 'spy');

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 updated the tests (and rebase master) with an $interval count of 2 otherwise the second part of this won't work since the .reset() only resets the state of the spy and not $interval.

@PatrickJS
Copy link
Member Author

I have a question about the mocks. https://github.com/angular/angular.js/blob/master/src/ngMock/angular-mocks.js#L456 I'm assuming I need to update the mocks with the same logic but it seems a bit crazy that I can add a feature to $interval without this showing up as an error. Then again I didn't run any of the e2e tests since my computer is throwing Java errors (gg Java) if it did account for it I assumed Travis would catch it

@petebacondarwin
Copy link
Member

@gdi2290 - the tests of the $interval service should not be relying on the mock version of $interval; so we ought to be able to check that the $interval service is correct.
But I agree that keeping mocks in sync with their real life counterparts. One approach, that perhaps you might like to investigate, is to see if we can run the $interval unit tests against the mock version of $interval.

@PatrickJS
Copy link
Member Author

I pushed up changes to mock to reflect the $interval in order to get this merged. There are a few ways this can be solved

  • Refactoring to expose private methods setInterval and clearInterval so we can use a decorator similar to how ngMocks/$timeout#L1713 is handled in Angular 1.x or Angular 2 with facade/async.es6#L39
  • Hack Jasmine/tests to repeat the tests for mock/real
  • Update the build system to run tests for mocks/real (not sure if mocks are included into docs tests but again I have Java errors)

either one that's chosen I think a new PR would make sense for that change while this one should be good

@petebacondarwin
Copy link
Member

Actually setInterval and clearInterval are already exposed because $interval gets them from $window, which is a service that we can adapt. So perhaps instead of decorating $interval what we need to do is decorate or replace $window with new versions of these methods?

@petebacondarwin
Copy link
Member

In the meantime, this PR looks fine from the point of view of this feature, so let's land it too.

@PatrickJS PatrickJS deleted the interval-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 [`setInterval`](http://mdn.io/setInterval#Syntax) works, this
commit allows users of `$interval` to add additional parameters to the call,
which will now be passed on to the callback function.

Closes angular#10632
netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
Similar to how [`setInterval`](http://mdn.io/setInterval#Syntax) works, this
commit allows users of `$interval` to add additional parameters to the call,
which will now be passed on to the callback function.

Closes angular#10632
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

6 participants