Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@equinor/fusion-framework-module-event

Package Overview
Dependencies
Maintainers
4
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@equinor/fusion-framework-module-event

Fusion module for events

  • 5.0.0-next-663bed8344cc2ca0111705b05045173328b3104d
  • next
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.7K
increased by23.88%
Maintainers
4
Weekly downloads
 
Created
Source

@equinor/fusion-framework-module-event

This package is meant for dispatching events between modules (siblings) and cross instances (parent|adjunct)

Base on the native node/web js event system, but the dispatcher is async for easier handling of cancelable events.

NOTE that creating a cancelable event without awaiting resolution, will not respect the preventDefault behavior!

Configuration

const configurator = (config) => {
  /** disable propagation of all events */
  delete config.event.onBubble

  /** pre-handle all events before dispatch */
  config.event.onDispatch = (e: FrameworkEvent) => {
    if(!allow_event(e)){
      e.preventDefault();
    }
  } 
}

Declaring events

import { ModuleEvent } from '@equinor/fusion-framework-module-event';
/** declare event type for code completion */
declare module '@equinor/fusion-framework-module-event' {
    interface ModuleEventMap {
        'someEvent': FrameworkEvent<FrameworkEventInit<MyDataObject, MySource>>;
    }
}

Custom events

import { ModuleEvent } from '@equinor/fusion-framework-module-event';

type CustomFrameworkEventInit = FrameworkEventInit<MyDataObject, MySource>;

class MyCustomEvent extends FrameworkEvent<CustomFrameworkEventInit, 'myCustomEvent'> {
  constructor(init: CustomFrameworkEventInit) { /** logic */ }
}

/** declare event type for code completion */
declare module '@equinor/fusion-framework-module-event' {
    interface ModuleEventMap {
        'myCustomEvent': MyCustomEvent;
    }
}

Usage

Handle a single event type

const teardown = modules.event.addEventListener('someEvent', (event) => console.log(event));
// remove event listener
teardown();

Dispatch event

// simple
const event = await modules.event.dispatchEvent(
  'myEvent', 
  {
    detail: 'some detail', 
    canBubble: false, 
    cancelable: true
    }
);

// alternative
const event = new MyCustomFrameworkEvent(
  'myCustomEvent', 
  {
    detail: 'some detail', 
    canBubble: false, 
    cancelable: true
    }
);
await modules.event.dispatchEvent(myEvent);


if(!event.defaultPrevent){
  doSomeAction();
}

Subscribe to all events

note that when subscribing to events, it does not allow side-effects, like preventDefault and stopPropagation

const subscription = modules.event.subscribe(console.log);
subscription.add(
  modules.event.subscribe({
    next: (event) => console.log(event),
    error: (err) => console.error(err),
    complete: () => 'event provider disposed'
  })
);
// when unmount
subscription.unsubscribe();

Keywords

FAQs

Package last updated on 11 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc