JSPM

fusion-plugin-universal-events

0.3.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 734
  • Score
    100M100P100Q113322F
  • License MIT

Events Emitter

Package Exports

  • fusion-plugin-universal-events
  • fusion-plugin-universal-events/dist/browser.es5.es.js
  • fusion-plugin-universal-events/dist/browser.es5.js
  • fusion-plugin-universal-events/dist/index.es.js
  • fusion-plugin-universal-events/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fusion-plugin-universal-events) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

fusion-plugin-universal-events

Build status

This is a Fusion plugin that captures events emitted from the client, sends them in batches to the server periodically, and allows the server to handle them.

It's useful for when you want to collect data about user actions or other metrics, and send them in bulk to the server to minimize the number of HTTP requests.

For convenience, universal-events automatically flushes its queue on page unload.


Installation

yarn add fusion-plugin-universal-events

Example

// main.js
import React from 'react';
import App from 'fusion-react';
import UniversalEvents, {UniversalEventsToken} from 'fusion-plugin-universal-events';
import {FetchToken} from 'fusion-tokens';

export default function() {
  const app = new App();
  app.register(UniversalEventsToken, UniversalEvents);
  __BROWSER__ && app.register(FetchToken, window.fetch);
  app.middleware({events: UniversalEventsToken}, ({events}) => {
    events.on('some-event', (payload) => {});
    events.on('some-scoped-event', (payload, ctx) => {});
    events.emit('some-event', {some: 'payload'});
    return (ctx, next) => {
      const scoped = events.from(ctx);
      scoped.on('some-scoped-event', (payload, ctx) => {});
      scoped.emit('some-scoped-event', {some: 'payload'});
    };
  });
  return app;
}

UniversalEvents vs Standard Event Emitter

The UniversalEvents abstraction was designed to allow you to emit and react to events without worrying about whether you are on the server or the browser. It also provides the ability for observers to map event payloads to new ones. For example, you might want to add the some piece of context such as a session id to every event of a certain type. If you just want a standard event emitter, this might not be the package for you.

Event transformation

It's possible to transform event data with a mapping function, for example to attach a timestamp to all actions of a type.

events.map('user-action', payload => {
  return {...payload, time: new Date().getTime()};
});

events.on('user-action', payload => {
  console.log(payload); // logs {type: 'click', time: someTimestamp}
});

events.emit('user-action', {type: 'click'});

Accessing ctx

Event mappers and handlers take an optional second parameter ctx. For example:

events.on('type', (payload, ctx) => {
  //...
})

This parameter will be present when events are emitted from the ctx scoped EventsEmitter instance. For example:

app.middleware({events: UniversalEventsToken}, ({events}) => {
  events.on('some-scoped-event', (payload, ctx) => {});
  return (ctx, next) => {
    const scoped = events.from(ctx);
    scoped.emit('some-scoped-event', {some: 'payload'});
  };
});

* event type

* is a special event type which denotes all events. This allows you to add a mapper or handler to all events. For example:

events.map('*', payload => {
  //
});

API

Plugin registration

import UniversalEvents, {UniversalEventsToken} from 'fusion-plugin-universal-events';
app.register(UniversalEventsToken, UniversalEvents);

Dependencies

FetchToken
  • fetch - UniversalEvents in the browser depends on an implementation of fetch registered on the standard FetchToken exported from fusion-tokens.
import {FetchToken} from 'fetch-tokens';
__BROWSER__ && app.register(FetchToken, window.fetch);

#### Instance API 

#### `events.on`

```js
events.on(type, callback)

Registers a callback to be called when an event of a type is emitted. Note that the callback will not be called if the event is emitted before the callback is registered.

  • type: string - Required. The type of event to listen on
  • callback: (mappedPayload: Object, ctx?) => void - Required. Runs when an event of matching type occurs. Receives the payload after it has been transformed by mapper functions, as well an optional ctx object.

events.emit

events.emit(type, payload)
  • type: string - Required. The type of event to emit
  • payload: Object - Optional. Data to be passed to event handlers

events.map

events.map(type, callback)
  • type: string - Required. The type of event to listen on
  • callback: (payload: Object, ctx?) => Object - Required. Runs when an event of matching type occurs. Should return a modified payload

events.flush

events.flush()

Flushes the data queue to the server immediately. Does not affect flush frequency

events.setFrequency

events.setFrequency(frequency)

Sets the frequency at which data is flushed to the server. Resets the interval timer.

  • frequency: number - Required.

events.teardown

events.teardown()

Stops the interval timer, clears the data queue and prevents any further data from being flushed to the server. Useful for testing

events.from

const scoped = events.from(ctx);

Returns a scoped version of the events api.