Skip to content

Commit 726a161

Browse files
committed
Add 'on' function to the tracker to listen to specific event.
1 parent 302bf32 commit 726a161

File tree

6 files changed

+76
-47
lines changed

6 files changed

+76
-47
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "react-tracker",
33
"version": "1.0.0",
44
"description": "Track actions within your React app with a tracker redux-like!",
5-
"main": "index.js",
5+
"main": "build/index.js",
66
"scripts": {
77
"build": "webpack"
88
},
@@ -34,7 +34,6 @@
3434
"webpack": "^3.6.0"
3535
},
3636
"dependencies": {
37-
"hoist-non-react-statics": "^2.2.1",
3837
"prop-types": "^15.5.10"
3938
},
4039
"peerDependencies": {

src/HOCs/provideTrackEvent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const provideTrackEvent = (Component, mapTrackerEventToProps) => {
66
const displayName = getDisplayName(Component);
77

88
const trackEventProvider = (props, context) => {
9-
const trackEvent = context.tracker.dispatch;
9+
const trackEvent = context.trackEvent;
1010

1111
if (!trackEvent) {
1212
throw Error(`Could not find tracker in the context of ` + `"${displayName}"`);
@@ -19,7 +19,7 @@ const provideTrackEvent = (Component, mapTrackerEventToProps) => {
1919

2020
trackEventProvider.displayName = displayName;
2121
trackEventProvider.contextTypes = {
22-
tracker: Types.tracker.isRequired
22+
trackEvent: Types.func.isRequired
2323
};
2424

2525
return trackEventProvider;

src/Tracker.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1+
import { assertSubscribers } from './utils';
2+
13
export default class Tracker {
24
constructor(subscribers) {
35
this.trackingHistory = [];
4-
this.subscribers = subscribers || [];
6+
this.subscribers = assertSubscribers(subscribers);
7+
8+
this.dispatch = this.dispatch.bind(this);
59
}
610

7-
subscribe(eventType, callback) {
8-
this.subscribers = [...this.subscribers, Object.assign({}, { [eventType]: callback })];
11+
on(eventType, callback) {
12+
if (typeof callback === 'function' && eventType && typeof eventType === 'string') {
13+
callback.eventType = eventType;
14+
15+
this.subscribers = [...this.subscribers, callback];
16+
}
917
}
1018

11-
dispatch = (event) => {
19+
dispatch(event) {
1220
const { type, data } = event || {};
1321

1422
if (!type) {
1523
return null;
1624
}
17-
18-
for (let subscriber in this.subscribers) {
1925

20-
if (Object.prototype.hasOwnProperty.call(this.subscribers, subscriber)) {
21-
if (typeof this.subscribers[subscriber] === 'function' && subscriber === type) {
22-
const save = this.subscribers[subscriber](event, this.trackingHistory);
26+
for (let i = 0; i < this.subscribers.length; i++) {
27+
if (typeof this.subscribers[i] === 'function' && this.subscribers[i].eventType === type) {
28+
const save = this.subscribers[i].call(null, event, this.trackingHistory);
2329

24-
if (save) {
25-
this.trackingHistory = [...this.trackingHistory, save];
26-
}
30+
if (save) {
31+
this.trackingHistory = [...this.trackingHistory, save];
2732
}
2833
}
2934
}

src/components/TrackerProvider.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,26 @@ import Types from '../types';
33

44
export default class TrackerProvider extends Component {
55
getChildContext() {
6-
console.log('from here: ', this.tracker);
76
return {
8-
tracker: this.tracker
7+
trackEvent: this.tracker.dispatch
8+
// We might need to extract other properties later.
99
}
1010
}
1111

1212
constructor(props, context) {
1313
super(props, context)
1414
this.tracker = props.tracker;
15-
16-
console.log('from here constrc: ', this.tracker);
1715
}
1816

1917
render() {
20-
console.log('from here render: ', this.tracker);
21-
2218
return Children.only(this.props.children)
2319
}
2420
}
2521

2622
TrackerProvider.propTypes = {
27-
tracker: Types.tracker.isRequired,
2823
children: Types.element.isRequired,
2924
};
3025

3126
TrackerProvider.childContextTypes = {
32-
tracker: Types.tracker.isRequired,
27+
trackEvent: Types.func.isRequired,
3328
};

src/utils/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ export const getDisplayName = WrappedComponent => {
22
const wrappedComponentName = WrappedComponent.displayName
33
|| WrappedComponent.name
44
|| 'Component';
5-
6-
5+
76
return `TrackEventProvider(${wrappedComponentName})`;
8-
}
7+
}
8+
9+
export const assertSubscribers = (subscribers = []) => {
10+
return subscribers.map(subscriber => {
11+
const eventType = Object.keys(subscriber)[0];
12+
13+
if ( eventType && typeof subscriber[eventType] === 'function') {
14+
subscriber[eventType].eventType = eventType;
15+
return subscriber[eventType];
16+
}
17+
})
18+
.filter(subscriber => Boolean(subscriber));
19+
}

webpack.config.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4-
const { ReactLoadablePlugin } = require('webpack');
4+
const webpack = require('webpack');
55

66
module.exports = {
77
entry: {
@@ -18,25 +18,44 @@ output: {
1818
},
1919
module: {
2020
rules: [
21-
{
22-
test: /\.js$/,
23-
exclude: /node_modules/,
24-
use: {
25-
loader: 'babel-loader',
26-
options: {
27-
babelrc: false,
28-
presets: [
29-
['es2015', { modules: false }],
30-
'react',
31-
],
32-
plugins: [
33-
'transform-class-properties',
34-
'transform-object-assign',
35-
],
36-
}
21+
{
22+
test: /\.js$/,
23+
exclude: /node_modules/,
24+
use: {
25+
loader: 'babel-loader',
26+
options: {
27+
babelrc: false,
28+
presets: [
29+
['es2015', { modules: false }],
30+
'react',
31+
],
32+
plugins: [
33+
'transform-class-properties',
34+
'transform-object-assign',
35+
],
36+
}
37+
},
3738
},
38-
},
39-
],
39+
]
4040
},
41-
devtool: 'inline-source-map'
41+
plugins: [
42+
new webpack.DefinePlugin({
43+
'process.env': {
44+
'NODE_ENV': JSON.stringify('production')
45+
}
46+
}),
47+
new webpack.optimize.UglifyJsPlugin({
48+
mangle: true,
49+
compress: {
50+
warnings: false,
51+
pure_getters: true,
52+
unsafe: true,
53+
unsafe_comps: true,
54+
screw_ie8: true
55+
},
56+
output: {
57+
comments: false,
58+
}
59+
}),
60+
]
4261
};

0 commit comments

Comments
 (0)