npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@equinor/fusion-framework-module-event

v4.1.1

Published

Fusion module for events

Downloads

6,212

Readme

@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();