diff --git a/src/api/events.ts b/src/api/events.ts new file mode 100644 index 0000000..a07d49b --- /dev/null +++ b/src/api/events.ts @@ -0,0 +1,23 @@ +import { request, RequestType } from '../http/request'; + +export function on(event: string, handler: any): Promise { + return new Promise((resolve: any, reject: any) => { + window.addEventListener(event, handler); + resolve(); + }); +}; + +export function off(event: string, handler: any): Promise { + return new Promise((resolve: any, reject: any) => { + window.removeEventListener(event, handler); + resolve(); + }); +}; + +export function dispatch(event: string, data: any): Promise { + return new Promise((resolve: any, reject: any) => { + let customEvent = new CustomEvent(event, {detail: data}); + window.dispatchEvent(customEvent); + resolve(); + }); +}; diff --git a/src/index.ts b/src/index.ts index 0278ace..e793d14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ export * as storage from './api/storage'; export * as debug from './api/debug'; export * as app from './api/app'; export * as window from './api/window'; +export * as events from './api/events'; export { init } from './api/init';