Skip to main content

quickjs_jit_stdlib/llrt/llrt_events/
custom_event.rs

1// Vendored from LLRT d9fd873e793c (modules/llrt_events/src/custom_event.rs); module paths and
2// feature names adapted by scripts/import-stdlib.py.
3// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4// SPDX-License-Identifier: Apache-2.0
5use rquickjs::{prelude::Opt, Ctx, IntoJs, Null, Result, Value};
6
7use crate::utils::object::ObjectExt;
8
9#[rquickjs::class]
10#[derive(rquickjs::class::Trace, rquickjs::JsLifetime)]
11pub struct CustomEvent<'js> {
12    event_type: String,
13    detail: Option<Value<'js>>,
14}
15
16#[rquickjs::methods]
17impl<'js> CustomEvent<'js> {
18    #[qjs(constructor)]
19    pub fn new(event_type: String, options: Opt<Value<'js>>) -> Result<Self> {
20        let mut detail = None;
21        if let Some(options) = options.0 {
22            if let Some(opt) = options.get_optional("detail")? {
23                detail = opt;
24            }
25        }
26        Ok(Self { event_type, detail })
27    }
28
29    #[qjs(get)]
30    pub fn detail(&self, ctx: Ctx<'js>) -> Result<Value<'js>> {
31        if let Some(detail) = &self.detail {
32            return Ok(detail.clone());
33        }
34        Null.into_js(&ctx)
35    }
36
37    #[qjs(get, rename = "type")]
38    pub fn event_type(&self) -> String {
39        self.event_type.clone()
40    }
41}