4.1.1 • Published 2 months ago

@equinor/fusion-framework-module-event v4.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

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

2 months ago

4.1.0

2 months ago

4.0.8

3 months ago

4.0.7

5 months ago

4.0.5

8 months ago

4.0.4

9 months ago

4.0.6

8 months ago

4.0.3

9 months ago

2.0.1

1 year ago

3.0.0

1 year ago

4.0.1

12 months ago

4.0.0

1 year ago

4.0.2

11 months ago

4.0.0-next.1

1 year ago

4.0.0-next.0

1 year ago

1.2.0

1 year ago

1.2.1

1 year ago

2.0.0

1 year ago

1.1.1

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.1.6

1 year ago

1.0.7

2 years ago

1.1.5

1 year ago

1.0.6

2 years ago

1.1.4

1 year ago

1.0.5

2 years ago

1.1.3

2 years ago

1.0.4

2 years ago

1.1.2

2 years ago

1.0.3

2 years ago

1.0.0-alpha.0

2 years ago

1.0.1-next.1

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago