Time as a dependency in JavaScript/TypeScript — inspired by .NET TimeProvider #204628
Replies: 2 comments
|
💬 Your Product Feedback Has Been Submitted 🎉 Thank you for taking the time to share your insights with us! Your feedback is invaluable as we build a better GitHub experience for all our users. Here's what you can expect moving forward ⏩
Where to look to see what's shipping 👀
What you can do in the meantime 💻
As a member of the GitHub community, your participation is essential. While we can't promise that every suggestion will be implemented, we want to emphasize that your feedback is instrumental in guiding our decisions and priorities. Thank you once again for your contribution to making GitHub even better! We're grateful for your ongoing support and collaboration in shaping the future of our platform. ⭐ |
|
The library was updated to v4.1.0 with a cleaner and netter API. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
🏷️ Discussion Type
Product Feedback
Body
I've been working on an open-source TypeScript library called Time-Provider:
https://github.com/jaenyf/time-provider
The idea started from a fairly simple observation:
Time is a dependency.
.NET has treated it as such since .NET 8 with
TimeProvider, but I felt the JavaScript/TypeScript/Node.js ecosystem was missing a similar abstraction.That's why I created Time-Provider.
It doesn't just mimic .NET's
TimeProvider. It goes a step further to fit the JavaScript ecosystem, where applications can use many different date/time libraries —Temporal, nativeDate, Day.js, Luxon, Moment, and others.You can choose the date library that fits your stack while keeping everything type-safe.
The architecture is also extensible because not every environment provides the same APIs. For example, the Animation Frame API is available in browsers but not in Node.js, so it shouldn't necessarily be part of every runtime's baseline.
The goal is to make time something you can inject, rather than something you have to monkey-patch in your tests.
In my opinion, this leads to a cleaner design and fits well with Dependency Injection and DDD principles.
As of writing,
time-providerprovides an application with a single time interface containing:The same interface can then be backed by different clock strategies:
For example, a test can create a manual clock:
Conceptually, this provides the same kind of deterministic control you get from Sinon/Jest fake timers, but with a narrower and more explicit scope.
No global monkey-patching, no waiting for real time to pass, and no need to couple your application to a particular testing framework.
Another important goal was not forcing applications to adopt a particular date library.
The project currently provides adapters for:
DateSo you can keep your existing stack while still injecting time in a type-safe way.
The project is also designed around:
I'd really like to get feedback from people who have dealt with time-dependent code and testing.
How do you currently handle time in your applications?
Do you prefer fake timers, dependency injection, clock abstractions, or something else?
Repository: https://github.com/jaenyf/time-provider
I'd especially appreciate feedback on the API design and whether this approach solves a problem you actually encounter.
Take care!
— Jaenyf
All reactions