Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support React v16.0 #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/browser/app.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
var React = require('react');
var ReactDOM = require('react-dom');
var createReactClass = require('create-react-class');
var Experiment = require("../../lib/Experiment");
var Variant = require("../../lib/Variant");
var experimentDebugger = require("../../lib/debugger");

experimentDebugger.enable();

var App = React.createClass({
var App = createReactClass({
render() {
return <div>
<h1>Experiment 1</h1>
Expand Down
3 changes: 2 additions & 1 deletion examples/isomorphic/Component.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var React = require("react");
var createReactClass = require('create-react-class');
var Experiment = require("../../lib/Experiment");
var Variant = require("../../lib/Variant");

module.exports = React.createClass({
module.exports = createReactClass({
propTypes: {
userIdentifier: React.PropTypes.string.isRequired
},
Expand Down
1 change: 0 additions & 1 deletion lib/CoreExperiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ var CoreExperiment = function (_Component) {
var children = {};
_react2.default.Children.forEach(props.children, function (element) {
if (!_react2.default.isValidElement(element) || element.type.displayName !== "Pushtell.Variant") {
console.log(element.type);
var error = new Error("Pushtell Experiment children must be Pushtell Variant components.");
error.type = "PUSHTELL_INVALID_CHILD";
throw error;
Expand Down
35 changes: 28 additions & 7 deletions lib/Experiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,47 @@ var Experiment = function (_Component) {

return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Experiment.__proto__ || Object.getPrototypeOf(Experiment)).call.apply(_ref, [this].concat(args))), _this), _this.win = function () {
_emitter2.default.emitWin(_this.props.name);
}, _this.getRandomValue = function () {
}, _this.getSelectedVariant = function () {
/*
Choosing a weighted variant:
For C, A, B with weights 2, 4, 8
variants = A, B, C
weights = 4, 8, 2
weightSum = 14
weightedIndex = 9
AAAABBBBBBBBCC
========^
Select B
*/

// Sorted array of the variant names, example: ["A", "B", "C"]
var variants = _emitter2.default.getSortedVariants(_this.props.name);
// Array of the variant weights, also sorted by variant name. For example, if
// variant C had weight 2, variant A had weight 4, and variant B had weight 8
// return [4, 8, 2] to correspond with ["A", "B", "C"]
var weights = _emitter2.default.getSortedVariantWeights(_this.props.name);
// Sum the weights
var weightSum = weights.reduce(function (a, b) {
return a + b;
}, 0);
// A random number between 0 and weightSum
var weightedIndex = typeof _this.props.userIdentifier === 'string' ? Math.abs((0, _crc2.default)(_this.props.userIdentifier) % weightSum) : Math.floor(Math.random() * weightSum);
var randomValue = variants[variants.length - 1];
// Iterate through the sorted weights, and deduct each from the weightedIndex.
// If weightedIndex drops < 0, select the variant. If weightedIndex does not
// drop < 0, default to the last variant in the array that is initially assigned.
var selectedVariant = variants[variants.length - 1];
for (var index = 0; index < weights.length; index++) {
weightedIndex -= weights[index];
if (weightedIndex < 0) {
randomValue = variants[index];
selectedVariant = variants[index];
break;
}
}
_emitter2.default.setActiveVariant(_this.props.name, randomValue);
return randomValue;
_emitter2.default.setActiveVariant(_this.props.name, selectedVariant);
return selectedVariant;
}, _this.getLocalStorageValue = function () {
if (typeof _this.props.userIdentifier === "string") {
return _this.getRandomValue();
return _this.getSelectedVariant();
}
var activeValue = _emitter2.default.getActiveVariant(_this.props.name);
if (typeof activeValue === "string") {
Expand All @@ -117,7 +138,7 @@ var Experiment = function (_Component) {
_emitter2.default.setActiveVariant(_this.props.name, _this.props.defaultVariantName);
return _this.props.defaultVariantName;
}
return _this.getRandomValue();
return _this.getSelectedVariant();
}, _temp), _possibleConstructorReturn(_this, _ret);
}

Expand Down
6 changes: 5 additions & 1 deletion lib/debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _createReactClass = require('create-react-class');

var _createReactClass2 = _interopRequireDefault(_createReactClass);

var _emitter = require('./emitter');

var _emitter2 = _interopRequireDefault(_emitter);
Expand Down Expand Up @@ -94,7 +98,7 @@ if (process.env.NODE_ENV === "production" || !_ExecutionEnvironment.canUseDOM) {

var style = null;

var Debugger = _react2.default.createClass({
var Debugger = (0, _createReactClass2.default)({
displayName: "Pushtell.Debugger",
getInitialState: function getInitialState() {
return {
Expand Down
3 changes: 2 additions & 1 deletion src/debugger.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import ReactDOM from 'react-dom';
import createReactClass from 'create-react-class';
import emitter from "./emitter";
import {canUseDOM} from 'fbjs/lib/ExecutionEnvironment';

Expand Down Expand Up @@ -78,7 +79,7 @@ if(process.env.NODE_ENV === "production" || !canUseDOM) {
style = null;
}
}
const Debugger = React.createClass({
const Debugger = createReactClass({
displayName: "Pushtell.Debugger",
getInitialState(){
return {
Expand Down
Loading