-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66d8e99
commit 302bf32
Showing
11 changed files
with
209 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"presets": [ | ||
["es2015", { | ||
"loose": true | ||
}], | ||
"react" | ||
], | ||
"plugins": [ | ||
"transform-class-properties" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
logs | ||
*.log | ||
|
||
node_modules | ||
|
||
.DS_Store | ||
|
||
/build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/.* | ||
|
||
/node_modules/ | ||
|
||
/src/ | ||
|
||
/webpack.* | ||
/server.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react' | ||
import { getDisplayName } from '../utils'; | ||
import Types from '../types'; | ||
|
||
const provideTrackEvent = (Component, mapTrackerEventToProps) => { | ||
const displayName = getDisplayName(Component); | ||
|
||
const trackEventProvider = (props, context) => { | ||
const trackEvent = context.tracker.dispatch; | ||
|
||
if (!trackEvent) { | ||
throw Error(`Could not find tracker in the context of ` + `"${displayName}"`); | ||
} | ||
|
||
const propsWithTrackEvent = Object.assign({}, props, { trackEvent }); | ||
|
||
return React.createElement(Component, propsWithTrackEvent) | ||
} | ||
|
||
trackEventProvider.displayName = displayName; | ||
trackEventProvider.contextTypes = { | ||
tracker: Types.tracker.isRequired | ||
}; | ||
|
||
return trackEventProvider; | ||
} | ||
|
||
export default provideTrackEvent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export default class Tracker { | ||
constructor(subscribers) { | ||
this.trackingHistory = []; | ||
this.subscribers = subscribers || []; | ||
} | ||
|
||
subscribe(eventType, callback) { | ||
this.subscribers = [...this.subscribers, Object.assign({}, { [eventType]: callback })]; | ||
} | ||
|
||
dispatch = (event) => { | ||
const { type, data } = event || {}; | ||
|
||
if (!type) { | ||
return null; | ||
} | ||
|
||
for (let subscriber in this.subscribers) { | ||
|
||
if (Object.prototype.hasOwnProperty.call(this.subscribers, subscriber)) { | ||
if (typeof this.subscribers[subscriber] === 'function' && subscriber === type) { | ||
const save = this.subscribers[subscriber](event, this.trackingHistory); | ||
|
||
if (save) { | ||
this.trackingHistory = [...this.trackingHistory, save]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
getHistory() { | ||
return this.trackingHistory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Component, Children } from 'react'; | ||
import Types from '../types'; | ||
|
||
export default class TrackerProvider extends Component { | ||
getChildContext() { | ||
console.log('from here: ', this.tracker); | ||
return { | ||
tracker: this.tracker | ||
} | ||
} | ||
|
||
constructor(props, context) { | ||
super(props, context) | ||
this.tracker = props.tracker; | ||
|
||
console.log('from here constrc: ', this.tracker); | ||
} | ||
|
||
render() { | ||
console.log('from here render: ', this.tracker); | ||
|
||
return Children.only(this.props.children) | ||
} | ||
} | ||
|
||
TrackerProvider.propTypes = { | ||
tracker: Types.tracker.isRequired, | ||
children: Types.element.isRequired, | ||
}; | ||
|
||
TrackerProvider.childContextTypes = { | ||
tracker: Types.tracker.isRequired, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import TrackerProvider from './components/TrackerProvider'; | ||
import provideTrackEvent from './HOCs/provideTrackEvent'; | ||
import Tracker from './Tracker'; | ||
|
||
export { Tracker, TrackerProvider, provideTrackEvent }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import propTypes from 'prop-types'; | ||
|
||
const Types = Object.assign({}, propTypes, { | ||
tracker: propTypes.shape({ | ||
subscribe: propTypes.func.isRequired, | ||
dispatch: propTypes.func.isRequired, | ||
}) | ||
}); | ||
|
||
export default Types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const getDisplayName = WrappedComponent => { | ||
const wrappedComponentName = WrappedComponent.displayName | ||
|| WrappedComponent.name | ||
|| 'Component'; | ||
|
||
|
||
return `TrackEventProvider(${wrappedComponentName})`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const { ReactLoadablePlugin } = require('webpack'); | ||
|
||
module.exports = { | ||
entry: { | ||
index: './src/index.js' | ||
}, | ||
output: { | ||
path: path.join(__dirname, 'build'), | ||
publicPath: 'build/', | ||
filename: 'index.js', | ||
chunkFilename: 'react-tracker.js', | ||
sourceMapFilename: 'react-tracker.map', | ||
library: 'ReactTracker', | ||
libraryTarget: 'umd' | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/, | ||
exclude: /node_modules/, | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
babelrc: false, | ||
presets: [ | ||
['es2015', { modules: false }], | ||
'react', | ||
], | ||
plugins: [ | ||
'transform-class-properties', | ||
'transform-object-assign', | ||
], | ||
} | ||
}, | ||
}, | ||
], | ||
}, | ||
devtool: 'inline-source-map' | ||
}; |