Repository navigation
Stabilize "now" time and "tick" relative times - #191
Conversation
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
|
I'm open to bikeshedding |
|
I'm fine with those names and the default. Looks good to me. 👍 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I might switch to this style, but will do so in another PR.
|
@ericf Haven't tested the code yet, but looks good to me apart from my |
|
about the ticking... ( 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 |
@johanneslumpe After thinking about this, I realized the idiomatic React solution would be to wrap
Yeah I'm thinking this for now. If it becomes a desired feature, then it can be added to |
@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.
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
So maybe |
|
@ericf maybe calling it |
|
Yeah |
|
@caridy After thinking through the naming of the prop more, I think 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 |
Stabilize "now" time and "tick" relative times
|
fair enough! |
Bumps [ts-node](https://github.com/TypeStrong/ts-node) from 8.3.0 to 8.4.1. - [Release notes](https://github.com/TypeStrong/ts-node/releases) - [Commits](TypeStrong/ts-node@v8.3.0...v8.4.1) Signed-off-by: dependabot-preview[bot] <[email protected]>
This adds a new
initialNowprop 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 appon the server and client so the initial client render will match the server's checksum.
On the server,
Date.now()should be captured before callingReactDOM.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 callsReact.render().Relatives times formatted via
<FormattedRelative>will now "tick" and stay up to date over time. The<FormattedRelative>component'snowprop has been renamed,initialNowto match and override the same prop on<IntlProvider>. It also has a newupdateIntervalprop 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
updateIntervalis set to 1 second.Fixes #186