-
-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@wxt-dev/alarms Proposal #1539
Comments
That's awesome, i'm looking forward to it! |
Nice, I like it! Couple of questions:
|
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@wxt-dev/alarms API Proposal
Overview
This proposal outlines a simple wrapper for Chrome’s native
browser.alarms
API, available as @wxt-dev/alarms. The goal is to provide an asynchronous, promise-based interface that aligns with our existing API design patterns. The new design centers on a single function,defineAlarm
, which returns an object with helper methods to manage an alarm instance.Objectives
Proposed API Design
1.
defineAlarm
This function defines an alarm, automatically initializing and registering it if it does not already exist. It returns an object with methods to retrieve, reschedule, clear, and watch the alarm. By handling the registration of the alarm listener internally, this design eliminates the need for manually adding a listener via
browser.alarms.onAlarm.addListener
for each alarm, thereby reducing boilerplate and potential errors.The returned
DefinedAlarm
object has the following interface:Example Usage
Alternative Scheduling Options
The API is designed with flexibility in mind. In addition to using
browser.alarms
for scheduling, it can be extended to support alternative scheduling mechanisms such as cron-based scheduling or evensetInterval
for simpler use cases. Developers may configure the scheduling method via options, enabling a more versatile approach to alarm management.Why is this API Beneficial?
Using the traditional approach, you would need to manually add an event listener for each alarm, as shown below:
This approach requires you to check the alarm name each time an alarm is triggered. With our proposed API, this boilerplate code is encapsulated within the
defineAlarm
method. The API automatically registers a listener for the specified alarm and provides a dedicatedwatch
method, simplifying your code to just:This not only improves code clarity and maintainability but also reduces the risk of errors from manual listener management.
Conclusion
This proposal outlines a straightforward and useful interface for working with Chrome alarms in an asynchronous and consistent manner. By returning an object from
defineAlarm
, developers can manage a specific alarm instance with its own methods, improving code clarity and encapsulation. This approach streamlines alarm management and reduces boilerplate code, making it easier to integrate and maintain alarm functionality within the extension.The text was updated successfully, but these errors were encountered: