-
Notifications
You must be signed in to change notification settings - Fork 154
Notifications API library #52
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
Comments
The existing
I see two basic designs for this future:
I don't have strong opinions between the two. Another question is whether we want to support the (deprecated) callback API for requesting permissions or not. If we did, then Rust users who aren't using Yet another, higher-level, alternative is to make creating notifications require a capability object that can only get obtained via requesting permissions and having it be granted. Something like this sketch: pub fn request_permission() -> impl Future<Item = NotificationCapability, Error = PermissionDenid> { ... }
pub struct NotificationCapability { ... }
impl NotificationCapability {
pub fn new_notification(&self) -> NotificationBuilder { ... }
} It is unclear to me whether this capabilities-based approach would benefit from being a layer on top of the others or not. It seems like it would be a very thin layer, and the whole point of the capabilities approach is to remove incorrect usage at compile time, so being able to get an almost-identical API that allows that incorrect usage seems strange. I guess that I've sort of convinced myself by writing all this out that:
|
Thanks for the feedback.
I'm in favor of option 2.
I think it would be easier if we use a Notification::request_permission()
.map(|builder| {
builder
.title("Foo")
.body("Bar")
.show();
}) This also works if permissions are revoked in the browser after they were granted.
I haven't thought about that. Since it's deprecated, maybe we could use a callback in Rust, and a javascript Notification::request_permission_map(|builder| {
builder
.title("Foo")
.body("Bar")
.show();
});
About that: |
Interesting point! If we wanted to guarantee that the builder is used on the same tick that the permissions request was granted, we could add a phantom
It means that the Web IDL interfaces that Assuming it is a standard option, then we would send a PR upstream to add it to that interface, and wait for a new version of web-sys to be published before we started using it here. |
Yeah, Firefox doesn't support all the options (but Chrome does). They aren't that new: For example, |
I wouldn't call that a priority. It doesn't happen too often that permissions are revoked, and even if a Notification is created without permission (provided that the programmer messed up), usually nothing bad happens. |
@fitzgen do you know what's the best way to test this? I can't grant permissions in |
Probably to include some JS that monkey patches / overrides the Here is an (untested) sketch: // notification-monkey-patch.js
let shouldGrantPermission = false;
window.Notification = class {
static requestPermission() {
return shouldGrantPermission ? Promise.resolve() : Promise.reject();
}
// ...
};
window.setShouldGrantPermission = should => shouldGrantPermission = should; // crates/notification/tests/web.rs
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = setShouldGrantPermission)]
fn set_should_grant_permission(should: bool);
} |
Summary
Make an API for displaying notifications (see MDN documentation).
Motivation
The event handlers in
web-sys
accept aOption<&Function>
, which is ugly to work with. Also, it is missing some options.Detailed Explanation
I propose to use Rust closures instead of
Function
. Also I'd like a function requesting permission to display notifications, that returns aFuture
.Notification::builder()
returns aNotificationBuilder
, whose build function is calledshow()
:Drawbacks, Rationale, and Alternatives
I don't know of any drawbacks. However, I'm not familiar with other frameworks, so please tell me if there are better alternatives.
Unresolved Questions
The syntax of the event listeners, which is discussed in #43
The text was updated successfully, but these errors were encountered: