Skip to content

Event

Event — a class for publishing events and subscribing to Аспро.Cloud platform events from a mini‑application. Any number of instances can be created for different target events.

ts
import { Event } from '@aspro-cloud/miniapp-jssdk'

Constructor

Creates an instance of Event with the given parameters

ts
constructor(params: EventParams)
ParameterTypeDescription
paramsEventParamsEvent creation parameters

Properties

events

EventCallbacks<EventEventName> object with the onEmit event handler

ts
get events(): EventCallbacks
set events(value: EventCallbacks)

target

EventTarget object identifying the target event, set via the constructor

ts
target: EventTarget

Methods

destroy

Removes subscriptions and releases resources

ts
destroy(): void

emit

Publishes an event on the platform — all subscribers of the target EventTarget receive the EventInfo data

ts
static async emit(params: EventEmitParams): Promise<any>
ParameterTypeDescription
paramsEventEmitParamsEvent publication parameters

off

Removes a subscription previously established via on() or once()

ts
async off(): Promise<any>

on

Registers an onEmit event handler, triggered every time a matching target event occurs on the platform

ts
async on(callback: EventCallback): Promise<any>
ParameterTypeDescription
callbackEventCallbackonEmit event handler

once

Registers a one-shot onEmit event handler — automatically removed after the first trigger

ts
async once(callback: EventCallback): Promise<any>
ParameterTypeDescription
callbackEventCallbackonEmit event handler

Example

ts
import { App, Event } from '@aspro-cloud/miniapp-jssdk'

await App.initializeFrame()

// publish an arbitrary event and pass arbitrary data
await Event.emit({
  target: {
    component: 'demo',
    action: 'ping'
  },
  info: {
    hello: 'world'
  }
})

// subscribe to the sidepanel opening
const sidepanelOpened = new Event({
  target: {
    code: 'sidepanel_opened'
  }
})

await sidepanelOpened.on(async (info) => {
  console.log('Sidepanel opened', info)

  // unsubscribe from the opening
  await sidepanelOpened.off()

  // and a one-shot subscription to the closing
  new Event({
    target: {
      code: 'sidepanel_closed'
    }
  }).once((info) => {
    console.log('Sidepanel closed', info)
  })
})
js
const App = window.ACloudMiniApp;

await App.initializeFrame();

// publish an arbitrary event and pass arbitrary data
await App.Frame.Event.emit({
  target: {
    component: 'demo',
    action: 'ping'
  },
  info: {
    hello: 'world'
  }
});

// subscribe to the sidepanel opening
const sidepanelOpened = new App.Frame.Event({
  target: {
    code: 'sidepanel_opened'
  }
});

await sidepanelOpened.on(async (info) => {
  console.log('Sidepanel opened', info);

  // unsubscribe from the opening
  await sidepanelOpened.off();

  // and a one-shot subscription to the closing
  new App.Frame.Event({
    target: {
      code: 'sidepanel_closed'
    }
  }).once((info) => {
    console.log('Sidepanel closed', info);
  });
});

Published under the MIT license.