Permalink
Fetching contributors…
Cannot retrieve contributors at this time
246 lines (169 sloc) 7.08 KB

fusion-plugin-universal-events

Build status

The fusion-plugin-universal-events is commonly required by other Fusion.js plugins and is used as an event emitter for data such as statistics and analytics. This plugin captures events emitted from the client, sends them in batches to the server periodically, and allows the server to handle them. Note that due to the batched and fire-and-forget nature of the client-to-server event emission, this library is not suitable for timing-sensitive requests such as error logging or RPC calls.

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, this plugin automatically flushes its queue on page unload.

If you need to use the universal event emitter from React, use fusion-plugin-universal-events-react

Differences between UniversalEvents and other event emitter libraries

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.


Table of contents


Installation

yarn add fusion-plugin-universal-events

Usage

export default createPlugin({
  deps: {events: UniversalEventsToken},
  middleware: ({events}) => {
    events.on('some-event', (payload) => {
      console.log(payload)
    });
    events.on('some-scoped-event', (payload, ctx) => {
      console.log(payload)
    });
    events.emit('some-event', {some: 'payload'});
    return (ctx, next) => {
      const scoped = events.from(ctx);
      scoped.on('some-scoped-event', (payload, ctx) => {
        console.log(payload)
      });
      scoped.emit('some-scoped-event', {some: 'scoped-payload'});
    };
  },
});

Setup

// 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);
  return app;
}

API

Registration API

UniversalEvents
import UniversalEvents from 'fusion-plugin-universal-events';

The plugin. Provides the service API. Typically should be registered to UniversalEventsToken.

Dependencies

FetchToken
import {FetchToken} from 'fusion-tokens';
// ...
__BROWSER__ && app.register(FetchToken, window.fetch);

Required. Browser-only. See https://github.com/fusionjs/fusion-tokens#fetchtoken

UniversalEventsBatchStorageToken
import {UniversalEventsBatchStorageToken, localBatchStorage} from 'fusion-plugin-universal-events';
// ...
__BROWSER__ && app.register(UniversalEventsBatchStorageToken, inMemoryBatchStorage);

Optional. Browser-only.

By default events will be stored in browser local storage before they are sent to the server. If you wish to override this behavior you can supply your own batch storage providing it complies with the BatchStorage interface type.


Service API

events.on

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.

events.on(type: string, callback: (payload: Object, ctx: ?Context, type: string) => void)
  • type: string - Required. The type of event to listen on. The type * denotes all events.
  • callback: (mappedPayload: Object, ctx: ?Context) => 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:string, payload: Object)
  • type: string - Required. The type of event to emit. The type * denotes all events.
  • payload: Object - Optional. Data to be passed to event handlers
events.map

Mutates the payload. Useful if you need to modify the payload to include metrics or other meta data.

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

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

events.flush()
events.setFrequency
events.setFrequency(frequency: number)

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: Context);

Returns a scoped version of the events api.


Other examples

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 => {
  //
});