Skip to content

Stabilize "now" time and "tick" relative times - #191

Merged
ericf merged 1 commit into
formatjs:masterfrom
ericf:stable-now-tick
Oct 25, 2015
Merged

ericf merged 1 commit into
formatjs:masterfrom
ericf:stable-now-tick

Conversation

@ericf

@ericf ericf commented Oct 21, 2015

Copy link
Copy Markdown
Collaborator

This adds a new initialNow prop to <IntlProvider> to stabilize the reference time when formatting relative times during the initial render. This prop should be set when rendering a universal React app
on the server and client so the initial client render will match the server's checksum.

On the server, Date.now() should be captured before calling ReactDOM.renderToString() and provided to <IntlProvider>. This "now" value needs to be serialized to the client so it can also pass the same value to <IntlProvider> when it calls React.render().

Relatives times formatted via <FormattedRelative> will now "tick" and stay up to date over time. The <FormattedRelative> component's now prop has been renamed, initialNow to match and override the same prop on <IntlProvider>. It also has a new updateInterval prop which accepts a number of milliseconds for the maximum speed at which relative times should be updated (defaults to 10 seconds).

Special care has been taken in the scheduling algorithm to display accurate information while reducing unnecessary re-renders. The algorithm will update the relative time at its next "interesting" moment; e.g., "1 minute ago" to "2 minutes ago" will use a delay of 60 seconds even if updateInterval is set to 1 second.

Fixes #186

This adds a new `initialNow` prop to `<IntlProvider>` to stabilize
the reference time when formatting relative times across the initial
render. This prop should be used when rendering a universal React app
on the server and client so the initial client render will match the
server's checksum.

On the server, `Date.now()` should be captured before calling
`ReactDOM.renderToString()` and provided to `<IntlProvider>`. This
"now" value needs to be serialized to the client so it can also pass
the same value to `<IntlProvider>` when it calls `React.render()`.

Relatives times formatted via `<FormattedRelative>` will now "tick"
and stay up to date over time. The `<FormattedRelative>` component's
`now` prop has been renamed, `initialNow` to match and override the
same prop on `<IntlProvider>`. It also has a new `updateInterval`
prop which accepts a number of milliseconds for the maximum speed at
which relative times should be updated (defaults to 10 seconds).

Special care has been taken in the scheduling algorithm to display
accurate information while reducing unnecessary re-renders. The
algorithm will update the relative time at its next "interesting"
moment; e.g., "1 minute ago" to "2 minutes ago" will use a delay of
60 seconds even if `updateInterval` is set to 1 second.

Fixes formatjs#186
@ericf

ericf commented Oct 21, 2015

Copy link
Copy Markdown
Collaborator Author

I'm open to bikeshedding initialNow and updateInterval prop names and the 10 second default update interval.

@mridgway

Copy link
Copy Markdown

I'm fine with those names and the default. Looks good to me. 👍

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nitpick: this should be const

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is my current coding style. I use const to represent constant bindings to constant values, generally which are static and not computed, or where the binding is to something the function doesn't own and should treat as immutable; e.g. const {foo} = options.

In places where I'm computing values, like this case, I've been using let. I may change my coding style in the future, but I'd want to apply it repo-wide.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I might switch to this style, but will do so in another PR.

@johanneslumpe

Copy link
Copy Markdown

@ericf Haven't tested the code yet, but looks good to me apart from my const nitpicking ;)The prop names are fine for me. What about the ability to specify a different default tick value globally for all relative components? Right now we would have to change the value for every single component if we wanted to tick every second. Should this be something that we can configure using the provider? Or would the solution here be to just create our own component which wraps FormattedRelative and sets the update interval?

@caridy

caridy commented Oct 21, 2015

Copy link
Copy Markdown
Collaborator

about the ticking... (updateInterval) I think that can be optimized based on the previous value, saying, if it renders as 1 week ago, there is probably not need to double check that in the next 24 hours. In other words, we can simply rely on the value to be render to predict when this value should be re-rendered. I know that react will opt out of the operation if we re-render it every 10 seconds, but still, we are carrying an operation that is not really needed.

I see two options to solve this... to get the low level api to expose metadata about the formatted value (similar to what we are trying to do with datatimeformat and fieldPositions), or sort-of re-implement part of the logic in FormattedRelative> so it knows when to re-render itself.

@ericf

ericf commented Oct 21, 2015

Copy link
Copy Markdown
Collaborator Author

Right now we would have to change the value for every single component if we wanted to tick every second.

@johanneslumpe After thinking about this, I realized the idiomatic React solution would be to wrap <FormattedRelative> with your own component that has your customizations, or it would naturally exist inside of some custom component's render(). If your UI is displaying a stream (like Twitter), you'd likely have a <StreamItem> component. So there might be 100 instances on the page, but updateInterval={1000} would only be declared once.

Or would the solution here be to just create our own component which wraps FormattedRelative and sets the update interval?

Yeah I'm thinking this for now. If it becomes a desired feature, then it can be added to <IntlProvider> later.

@ericf

ericf commented Oct 21, 2015

Copy link
Copy Markdown
Collaborator Author

I think that can be optimized based on the previous value, saying, if it renders as 1 week ago, there is probably not need to double check that in the next 24 hours. In other words, we can simply rely on the value to be render to predict when this value should be re-rendered. I know that react will opt out of the operation if we re-render it every 10 seconds, but still, we are carrying an operation that is not really needed.

@caridy This is already the algorithm I've implemented in this PR, I tried to explain how it works in the description at the top.

I see two options to solve this... to get the low level api to expose metadata about the formatted value (similar to what we are trying to do with datatimeformat and fieldPositions), or sort-of re-implement part of the logic in FormattedRelative> so it knows when to re-render itself.

There's no need for this. Part of Intl RelativeFormat's API is the set of units it works with. I already have those encoded in <FormattedRelative>'s units propType definition. The one thing I'm ignoring is Intl RelativeFormat's thresholds, and instead compute the standard ms per second, minute, hour, and day.

updateInterval is essentially to set the maximum speed at which the relative time will be re-rendered. I added this configuration option for the following reasons:

  1. To be able to turn off auto-updating relative times, updateInterval={0}.
  2. To control how fast relative times under 1 minute "tick". For a real-time app, like Twitter, you'd likely want to see: "now"…"1 second ago"…"2 seconds ago"…etc. While an app like Mail might find this distracting and only want to update every 10 seconds: "now"…"10 seconds ago"…etc.

So maybe updateInterval isn't the best? But I feel it gives developers some good control over the maximum update speed to control how "real-time" the app appears.

@caridy

caridy commented Oct 22, 2015

Copy link
Copy Markdown
Collaborator

@ericf maybe calling it minInterval, which is really what it is.

@ericf

ericf commented Oct 22, 2015

Copy link
Copy Markdown
Collaborator Author

Yeah minUpdateInterval, but what's awkward is the value that represents the fastest something will update and the maximum frequency, but uses the term "min".

@ericf

ericf commented Oct 25, 2015

Copy link
Copy Markdown
Collaborator Author

@caridy After thinking through the naming of the prop more, I think updateInterval is superior. It matches the React model of being declarative, but having the framework do something smart under to the hood for performance.

I think the reason you were concern about this props name is because you were in the context of reviewing these changes and concerned about the implementation details. But for a user, they shouldn't be concerned about the implementation details; and I'll document that the scheduling algorithm schedules the updates based on the updateInterval and interesting moments.

ericf added a commit that referenced this pull request Oct 25, 2015
Stabilize "now" time and "tick" relative times
@ericf
ericf merged commit e5b4529 into formatjs:master Oct 25, 2015
@ericf
ericf deleted the stable-now-tick branch October 25, 2015 00:32
@caridy

caridy commented Oct 26, 2015

Copy link
Copy Markdown
Collaborator

fair enough!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants