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

feat($time): create time service #10525

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

marcin-wosinek
Copy link
Contributor

Add simple $time service to allow easier mocking dates in applications.

Closes #10402

@googlebot
Copy link

CLAs look good, thanks!

@lgalfaso
Copy link
Contributor

how is this different from https://docs.angularjs.org/api/ngMock/type/angular.mock.TzDate ?

@caitp
Copy link
Contributor

caitp commented Dec 19, 2014

TzDate is great for testing, but I think we could use $time to move some logic from the date filter and date input handlers into a more logical and re-usable place, which is why I said it would be nice to do it

src/ng/time.js Outdated
var self = this;

this.$get = ['$window', function($window) {
return {
Copy link
Contributor

Choose a reason for hiding this comment

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

do you think that there will be more properties added to $time ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that just returning a function would be better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually return function - the doc was wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

Returning $window.Date.now rather than an object would actually make mocking harder

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The options we have here:

// 1
this.now = $window.Date.now;

// 2
this.now = function() {
  return new Date();
};
  1. Was requested in $time service #10402 and is implemented in PR
  2. Is what what you @caitp call object? I've build something like this in my project, and it worked nice.

Copy link
Contributor

Choose a reason for hiding this comment

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

I still think that $time should be a function (that is a wrap that calls $window.Date()), just like what we have for $timeout. Mocks can be done in the same way

Copy link
Contributor

Choose a reason for hiding this comment

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

it's problematic if we do want to put functionality for date filter/date input on it, because those then become harder to mock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With $timeout we just wrap one function inside the other. Here I can imaging that later we will want to have other properties. $time.now() is easier to extend, $time() seems only to be a bit shorter. And it should be called $now then.

Copy link
Contributor

Choose a reason for hiding this comment

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

it's problematic if we do want to put functionality for date filter/date input on it, because those then become harder to mock.

why is this different than $timeout ?

Copy link
Contributor

Choose a reason for hiding this comment

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

$timeout only has one custom method to mock, we'd potentially have 3, possibly more

@lgalfaso
Copy link
Contributor

@caitp I think you are right, this would be a good addition

@lgalfaso lgalfaso self-assigned this Dec 19, 2014
@lgalfaso lgalfaso modified the milestones: 1.3.8, 1.3.x Dec 19, 2014
@marcin-wosinek marcin-wosinek force-pushed the time branch 3 times, most recently from 3959c66 to cc054db Compare December 19, 2014 15:44
@PatrickJS
Copy link
Contributor

why not $date?

@lgalfaso
Copy link
Contributor

@gdi2290 $date works for me

Add simple $time service to allow easier mocking dates in applications.

Closes angular#10402
@marcin-wosinek
Copy link
Contributor Author

@lgalfaso - do you have some sugestions about tests? I feel that the current one are not really valuable.

@lgalfaso
Copy link
Contributor

@manuel-woelker sorry for the late response. Even when the PR looks good if we are just looking at the core, I think it is important that there is a reasonable counterpart for ng-mock. This is going to take a little bit more time to get done right, but I would still would like to aim at 1.4

@marcin-wosinek
Copy link
Contributor Author

Could you provide example of what should this ng-mock counterpart do? I can think only of some $date.setTime(), but this can be acomplished by just mocking the normal fuction

@lgalfaso
Copy link
Contributor

Here is the first draft
https://docs.google.com/document/d/1nkTDEd6yaV9KWEqXEuqHjFzt3WtZ_WNVFC1vA1la0ts/edit#heading=h.csrxcq4wx70q

Note: This is still an early draft so it may change entirely or not be done
at all

@marcin-wosinek
Copy link
Contributor Author

Ah, that looks like a pretty big change. Is there separate ticket for that? There is #10402, but it's defined much simpler.

btw

Keep a unified queue of elements that are triggered thru $timeout and $interval

should this be something like
0 => now
+100ms => interval 1
+200ms => interval 2
+250ms => timeout

So the queue will know that there should be two intervals fired before timeout?

@lgalfaso
Copy link
Contributor

So the queue will know that there should be two intervals fired before timeout?

yes, and also that $time.now() (or however we call it) also moves forward the right number of ms

@petebacondarwin petebacondarwin modified the milestones: 1.5.x - migration-facilitation, 1.3.x - superluminal-nudge Sep 7, 2015
@petebacondarwin
Copy link
Contributor

This should land in 1.5 - @lgalfaso can you manage it?

@lgalfaso
Copy link
Contributor

lgalfaso commented Sep 7, 2015

This should land in 1.5 - @lgalfaso https://github.com/lgalfaso can you
manage it?

I will take care

@lgalfaso lgalfaso closed this in fa4c7b7 Sep 20, 2015
@@ -80,6 +80,7 @@
$TemplateCacheProvider,
$TemplateRequestProvider,
$$TestabilityProvider,
$TimeProvider,
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be $DateProvider ?

@gkalpak
Copy link
Member

gkalpak commented Sep 21, 2015

@lgalfaso, I am kind of confused. Is there some followup planned with more features ?

From the discussion above, I got the impression this would be more feature-rich/involved.
In the current state it's essentially a wrapper for Date(). What is the benefit of using $date over just using $window.Date ?

@petebacondarwin
Copy link
Contributor

When I said "this should land", I hadn't actually reviewed this, by the way ;-P

@lgalfaso
Copy link
Contributor

Let me revert this

@gkalpak
Copy link
Member

gkalpak commented Sep 21, 2015

When I said "this should land", I hadn't actually reviewed this, by the way ;-P

Wait...whaat ?! We should actually review the PRs before LGTM'ing ? 😛

@petebacondarwin
Copy link
Contributor

'This Should Land In One Point Five' (TSLIOPF) does not start with the letters L.G.T.M, as far as I can tell.
For the avoidance of doubt: where I said such a thing I really meant that we should work on the issue or pull request once we are in 1.5 mode.

@petebacondarwin
Copy link
Contributor

@lgalfaso - we probably don't need a revert just a bit of additional polish, right?

@lgalfaso
Copy link
Contributor

reverted this with 7dcfe5e

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

$time service
8 participants