Skip to content

Commit

Permalink
feat: Initial implementation of Event (#637)
Browse files Browse the repository at this point in the history
* feat: Initial implementation of Event

* Delete limetime

* Fix
  • Loading branch information
nabetti1720 authored Oct 21, 2024
1 parent 977ef7f commit 7f0ff52
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
61 changes: 61 additions & 0 deletions llrt_modules/src/modules/events/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use rquickjs::{prelude::Opt, Result, Value};

use llrt_utils::object::ObjectExt;

#[rquickjs::class]
#[derive(rquickjs::class::Trace)]
pub struct Event {
event_type: String,
bubbles: bool,
cancelable: bool,
composed: bool,
}

#[rquickjs::methods]
impl Event {
#[qjs(constructor)]
pub fn new(event_type: String, options: Opt<Value<'_>>) -> Result<Self> {
let mut bubbles = false;
let mut cancelable = false;
let mut composed = false;
if let Some(options) = options.0 {
if let Some(opt) = options.get_optional("bubbles")? {
bubbles = opt;
}
if let Some(opt) = options.get_optional("cancelable")? {
cancelable = opt;
}
if let Some(opt) = options.get_optional("composed")? {
composed = opt;
}
}
Ok(Self {
event_type,
bubbles,
cancelable,
composed,
})
}

#[qjs(get)]
pub fn bubbles(&self) -> bool {
self.bubbles
}

#[qjs(get)]
pub fn cancelable(&self) -> bool {
self.cancelable
}

#[qjs(get)]
pub fn composed(&self) -> bool {
self.composed
}

#[qjs(get, rename = "type")]
pub fn event_type(&self) -> String {
self.event_type.clone()
}
}
4 changes: 3 additions & 1 deletion llrt_modules/src/modules/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ use rquickjs::{
};
use tracing::trace;

use self::{custom_event::CustomEvent, event_target::EventTarget};
use self::{custom_event::CustomEvent, event::Event, event_target::EventTarget};
use crate::module_info::ModuleInfo;

pub mod custom_event;
pub mod event;
pub mod event_target;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -476,6 +477,7 @@ pub fn init(ctx: &Ctx<'_>) -> Result<()> {

Class::<EventTarget>::define(&globals)?;
Class::<CustomEvent>::define(&globals)?;
Class::<Event>::define(&globals)?;

EventTarget::add_event_target_prototype(ctx)?;

Expand Down
22 changes: 22 additions & 0 deletions tests/unit/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,25 @@ describe("EventTarget", () => {
expect(eventsArray).toEqual(["1st", "2nd", "1st"]);
});
});

describe("Event", () => {
it("globalThis should have a Event", () => {
const myEvent = new Event("test");

expect(myEvent.type).toEqual("test");
expect(myEvent.bubbles).toBeFalsy();
expect(myEvent.cancelable).toBeFalsy();
expect(myEvent.composed).toBeFalsy();
});
it("Event should have options", () => {
const myEvent = new Event("test", {
bubbles: true,
cancelable: true,
composed: true,
});

expect(myEvent.bubbles).toBeTruthy();
expect(myEvent.cancelable).toBeTruthy();
expect(myEvent.composed).toBeTruthy();
});
});

0 comments on commit 7f0ff52

Please sign in to comment.