Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Migrate to DI
Browse files Browse the repository at this point in the history
  • Loading branch information
ganemone authored and fusion-bot[bot] committed Jan 12, 2018
1 parent 8606f5c commit 95b2fcd
Show file tree
Hide file tree
Showing 10 changed files with 867 additions and 645 deletions.
95 changes: 48 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# fusion-plugin-universal-events
# fusion-plugin-universal-events

[![Build status](https://badge.buildkite.com/de4e30ddb9d019f5a8e3a2519bc0a5cccab25247809cd10c99.svg?branch=master)](https://buildkite.com/uberopensource/fusion-plugin-universal-events)

Expand All @@ -24,47 +24,36 @@ yarn add fusion-plugin-universal-events
// main.js
import React from 'react';
import App from 'fusion-react';
import universalEvents from 'fusion-plugin-universal-events';
import Root from './components/root';
import analytics from './plugins/analytics';
import UniversalEvents, {UniversalEventsToken} from 'fusion-plugin-universal-events';
import {FetchToken} from 'fusion-tokens';

export default function() {
const app = new App(<Root />);
const UniversalEvents = app.plugin(universalEvents);
const Analytics = app.plugin(analytics, {UniversalEvents});
const app = new App();
app.register(UniversalEventsToken, UniversalEvents);
__BROWSER__ && app.configure(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;
}
```

// components/root.js
export default ({}, {universalEvents}) => {
function trackSignUp() {
universalEvents.emit('user-action', {
action: 'click',
target: 'sign up button',
});
}
return <button onClick={trackSignUp}>Sign up</button>;
}
### UniversalEvents vs Standard Event Emitter

// plugins/analytics.js
export default function({UniversalEvents}) => (ctx, next) => {
UniversalEvents.of(ctx).on('user-action', ({action, target}) => {
// logs `User did a click on sign up button` both in client and server
console.log(`User did a ${action} on ${target}!`);
if (__NODE__) {
// save data
}
});
return next();
}
```
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.

```js
const events = UniversalEvents.of();
events.map('user-action', payload => {
return {...payload, time: new Date().getTime()};
});
Expand All @@ -89,10 +78,13 @@ events.on('type', (payload, ctx) => {
This parameter will be present when events are emitted from the `ctx` scoped EventsEmitter instance. For example:

```js
const eventsWithoutCtx = EventEmitter.of();
(ctx, next) => {
const eventsWithCtx = EventEmitter.of(ctx);
}
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
Expand All @@ -109,25 +101,24 @@ events.map('*', payload => {

### API

#### `universalEvents`

`universalEvents` - A plugin that creates a UniversalEvents class when passed to `app.plugin`
#### Plugin registration

```js
import App from 'fusion-react';
import universalEvents from 'fusion-plugin-universal-events';

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

#### `UniversalEvents.of`
#### Dependencies

##### `FetchToken`

- `fetch` - UniversalEvents in the browser depends on an implementation of `fetch` registered on the standard `FetchToken` exported from `fusion-tokens`.

```js
const events = UniversalEvents.of(ctx)
```
Returns an event emitter
import {FetchToken} from 'fetch-tokens';
__BROWSER__ && app.configure(FetchToken, window.fetch);

- `ctx: Object` - A memoization key
#### Instance API

#### `events.on`

Expand Down Expand Up @@ -182,4 +173,14 @@ Sets the frequency at which data is flushed to the server. Resets the interval t
events.teardown()
```

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

#### `events.from`

```js
const scoped = events.from(ctx);
```

Returns a scoped version of the events api.

- `ctx: FusionContext` - Required. See [FusionContext](https://github.com/fusionjs/fusion-core#context)
43 changes: 22 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,47 @@
"keywords": [],
"license": "MIT",
"files": [
"dist"
"dist",
"src"
],
"main": "./dist/node.cjs.js",
"module": "./dist/node.es.js",
"main": "./dist/index.js",
"module": "./dist/index.es.js",
"browser": {
"./dist/node.cjs.js": "./dist/browser.es5.cjs.js",
"./dist/node.es.js": "./dist/browser.es5.es.js"
"./dist/index.js": "./dist/browser.es5.js",
"./dist/index.es.js": "./dist/browser.es5.es.js"
},
"es2015": {
"./dist/browser.es5.cjs.js": "./dist/browser.es2015.cjs.js",
"./dist/browser.es5.es.js": "./dist/browser.es2015.es.js"
},
"es2017": {
"./dist/browser.es5.cjs.js": "./dist/browser.es2017.cjs.js",
"./dist/browser.es5.es.js": "./dist/browser.es2017.es.js",
"./dist/browser.es2015.cjs.js": "./dist/browser.es2017.cjs.js",
"./dist/browser.es2015.es.js": "./dist/browser.es2017.es.js"
},
"dependencies": {
"koa-bodyparser": "4.2.0"
},
"devDependencies": {
"babel-eslint": "^8.0.2",
"create-universal-package": "^2.0.1",
"eslint": "^4.11.0",
"eslint-config-fusion": "^0.1.2",
"babel-eslint": "^8.2.1",
"create-universal-package": "^3.2.4",
"eslint": "^4.15.0",
"eslint-config-fusion": "^0.2.1",
"eslint-plugin-cup": "^1.0.0",
"eslint-plugin-flowtype": "^2.39.1",
"eslint-plugin-prettier": "^2.3.1",
"eslint-plugin-react": "^7.4.0",
"flow-bin": "^0.59.0",
"fusion-core": "^0.2.3",
"fusion-test-utils": "^0.2.1",
"nyc": "^11.3.0",
"prettier": "1.8.2",
"eslint-plugin-flowtype": "^2.41.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-prettier": "^2.4.0",
"eslint-plugin-react": "^7.5.1",
"flow-bin": "^0.63.1",
"fusion-core": "^0.3.0-2",
"fusion-test-utils": "^0.3.0",
"fusion-tokens": "^0.0.4",
"nyc": "^11.4.1",
"prettier": "1.10.2",
"tape-cup": "^4.7.1",
"unitest": "^2.1.1"
},
"peerDependencies": {
"fusion-core": "^0.2.3"
"fusion-core": "^0.3.0-2",
"fusion-tokens": "^0.0.4"
},
"scripts": {
"clean": "rm -rf dist",
Expand Down
Loading

0 comments on commit 95b2fcd

Please sign in to comment.