|
| 1 | +(function (global, factory) { |
| 2 | + typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('react')) : |
| 3 | + typeof define === 'function' && define.amd ? define(['react'], factory) : |
| 4 | + (global = global || self, factory(global.React)); |
| 5 | +}(this, function (React) { 'use strict'; |
| 6 | + |
| 7 | + /*! ***************************************************************************** |
| 8 | + Copyright (c) Microsoft Corporation. All rights reserved. |
| 9 | + Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 10 | + this file except in compliance with the License. You may obtain a copy of the |
| 11 | + License at http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | + THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED |
| 15 | + WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 16 | + MERCHANTABLITY OR NON-INFRINGEMENT. |
| 17 | +
|
| 18 | + See the Apache Version 2.0 License for specific language governing permissions |
| 19 | + and limitations under the License. |
| 20 | + ***************************************************************************** */ |
| 21 | + /* global Reflect, Promise */ |
| 22 | + |
| 23 | + var extendStatics = function(d, b) { |
| 24 | + extendStatics = Object.setPrototypeOf || |
| 25 | + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || |
| 26 | + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; |
| 27 | + return extendStatics(d, b); |
| 28 | + }; |
| 29 | + |
| 30 | + function __extends(d, b) { |
| 31 | + extendStatics(d, b); |
| 32 | + function __() { this.constructor = d; } |
| 33 | + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); |
| 34 | + } |
| 35 | + |
| 36 | + var __assign = function() { |
| 37 | + __assign = Object.assign || function __assign(t) { |
| 38 | + for (var s, i = 1, n = arguments.length; i < n; i++) { |
| 39 | + s = arguments[i]; |
| 40 | + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; |
| 41 | + } |
| 42 | + return t; |
| 43 | + }; |
| 44 | + return __assign.apply(this, arguments); |
| 45 | + }; |
| 46 | + |
| 47 | + var createContext = require('react-broadcast').createContext; |
| 48 | + var _a = createContext({ |
| 49 | + optimizely: null, |
| 50 | + timeout: 0, |
| 51 | + }), Consumer = _a.Consumer, Provider = _a.Provider; |
| 52 | + var OptimizelyContextConsumer = Consumer; |
| 53 | + var OptimizelyContextProvider = Provider; |
| 54 | + |
| 55 | + var OptimizelyProvider = /** @class */ (function (_super) { |
| 56 | + __extends(OptimizelyProvider, _super); |
| 57 | + function OptimizelyProvider(props) { |
| 58 | + var _this = _super.call(this, props) || this; |
| 59 | + var timeout = props.timeout, optimizely = props.optimizely; |
| 60 | + _this.sdkWrapper = optimizely; |
| 61 | + return _this; |
| 62 | + } |
| 63 | + OptimizelyProvider.prototype.render = function () { |
| 64 | + var _a = this.props, children = _a.children, timeout = _a.timeout; |
| 65 | + var value = { |
| 66 | + optimizely: this.sdkWrapper, |
| 67 | + timeout: timeout, |
| 68 | + }; |
| 69 | + return (React.createElement(OptimizelyContextProvider, { value: value }, children)); |
| 70 | + }; |
| 71 | + OptimizelyProvider.defaultProps = { |
| 72 | + timeout: 0, |
| 73 | + }; |
| 74 | + return OptimizelyProvider; |
| 75 | + }(React.Component)); |
| 76 | + |
| 77 | + function withOptimizely(Component) { |
| 78 | + return /** @class */ (function (_super) { |
| 79 | + __extends(WithOptimizely, _super); |
| 80 | + function WithOptimizely() { |
| 81 | + return _super !== null && _super.apply(this, arguments) || this; |
| 82 | + } |
| 83 | + WithOptimizely.prototype.render = function () { |
| 84 | + var _this = this; |
| 85 | + return (React.createElement(OptimizelyContextConsumer, null, function (value) { return (React.createElement(Component, __assign({}, _this.props, { optimizely: value.optimizely, optimizelyReadyTimeout: value.optimizelyReadyTimeout }))); })); |
| 86 | + }; |
| 87 | + return WithOptimizely; |
| 88 | + }(React.Component)); |
| 89 | + } |
| 90 | + |
| 91 | + var FeatureComponent = /** @class */ (function (_super) { |
| 92 | + __extends(FeatureComponent, _super); |
| 93 | + function FeatureComponent(props) { |
| 94 | + var _this = _super.call(this, props) || this; |
| 95 | + _this.state = { |
| 96 | + canRender: false, |
| 97 | + isEnabled: false, |
| 98 | + variables: {}, |
| 99 | + }; |
| 100 | + return _this; |
| 101 | + } |
| 102 | + FeatureComponent.prototype.componentDidMount = function () { |
| 103 | + var _this = this; |
| 104 | + var _a = this.props, feature = _a.feature, optimizely = _a.optimizely, optimizelyReadyTimeout = _a.optimizelyReadyTimeout; |
| 105 | + if (optimizely === null) { |
| 106 | + throw new Error('optimizely prop must be supplied'); |
| 107 | + } |
| 108 | + optimizely.onReady({ timeout: optimizelyReadyTimeout }).then(function () { |
| 109 | + var isEnabled = optimizely.isFeatureEnabled(feature); |
| 110 | + var variables = optimizely.getFeatureVariables(feature); |
| 111 | + _this.setState({ |
| 112 | + canRender: true, |
| 113 | + isEnabled: isEnabled, |
| 114 | + variables: variables, |
| 115 | + }); |
| 116 | + }); |
| 117 | + }; |
| 118 | + FeatureComponent.prototype.render = function () { |
| 119 | + var children = this.props.children; |
| 120 | + var _a = this.state, isEnabled = _a.isEnabled, variables = _a.variables, canRender = _a.canRender; |
| 121 | + if (!canRender) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + return children(isEnabled, variables); |
| 125 | + }; |
| 126 | + return FeatureComponent; |
| 127 | + }(React.Component)); |
| 128 | + var OptimizelyFeature = withOptimizely(FeatureComponent); |
| 129 | + |
| 130 | + var Experiment = /** @class */ (function (_super) { |
| 131 | + __extends(Experiment, _super); |
| 132 | + function Experiment(props) { |
| 133 | + var _this = _super.call(this, props) || this; |
| 134 | + _this.state = { |
| 135 | + canRender: false, |
| 136 | + variation: null, |
| 137 | + }; |
| 138 | + return _this; |
| 139 | + } |
| 140 | + Experiment.prototype.componentDidMount = function () { |
| 141 | + var _this = this; |
| 142 | + var _a = this.props, experiment = _a.experiment, optimizely = _a.optimizely, optimizelyReadyTimeout = _a.optimizelyReadyTimeout; |
| 143 | + if (optimizely === null) { |
| 144 | + throw new Error('optimizely prop must be supplied'); |
| 145 | + } |
| 146 | + optimizely.onReady({ timeout: optimizelyReadyTimeout }).then(function () { |
| 147 | + var variation = optimizely.activate(experiment); |
| 148 | + _this.setState({ |
| 149 | + canRender: true, |
| 150 | + variation: variation, |
| 151 | + }); |
| 152 | + }); |
| 153 | + }; |
| 154 | + Experiment.prototype.render = function () { |
| 155 | + var children = this.props.children; |
| 156 | + var _a = this.state, variation = _a.variation, canRender = _a.canRender; |
| 157 | + if (!canRender) { |
| 158 | + return null; |
| 159 | + } |
| 160 | + if (children != null && typeof children === 'function') { |
| 161 | + return children(variation); |
| 162 | + } |
| 163 | + var match = null; |
| 164 | + // We use React.Children.forEach instead of React.Children.toArray().find() |
| 165 | + // here because toArray adds keys to all child elements and we do not want |
| 166 | + // to trigger an unmount/remount |
| 167 | + React.Children.forEach(this.props.children, function (child) { |
| 168 | + if (match || !React.isValidElement(child)) { |
| 169 | + console.log('found', match, !React.isValidElement(child)); |
| 170 | + return; |
| 171 | + } |
| 172 | + console.log('child props', child.props); |
| 173 | + if (child.props.variation) { |
| 174 | + console.log('child variation', child.props.variation); |
| 175 | + if (variation === child.props.variation) { |
| 176 | + match = child; |
| 177 | + } |
| 178 | + } |
| 179 | + else if (child.props.default) { |
| 180 | + console.log('child default', child.props.default); |
| 181 | + match = child; |
| 182 | + } |
| 183 | + }); |
| 184 | + return match |
| 185 | + ? React.cloneElement(match, { variation: variation }) |
| 186 | + : null; |
| 187 | + }; |
| 188 | + return Experiment; |
| 189 | + }(React.Component)); |
| 190 | + var OptimizelyExperiment = withOptimizely(Experiment); |
| 191 | + |
| 192 | + var Variation = /** @class */ (function (_super) { |
| 193 | + __extends(Variation, _super); |
| 194 | + function Variation() { |
| 195 | + return _super !== null && _super.apply(this, arguments) || this; |
| 196 | + } |
| 197 | + Variation.prototype.render = function () { |
| 198 | + return this.props.children; |
| 199 | + }; |
| 200 | + return Variation; |
| 201 | + }(React.Component)); |
| 202 | + var OptimizelyVariation = Variation; |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + var optimizelyReactSDK = /*#__PURE__*/Object.freeze({ |
| 207 | + OptimizelyProvider: OptimizelyProvider, |
| 208 | + OptimizelyFeature: OptimizelyFeature, |
| 209 | + withOptimizely: withOptimizely, |
| 210 | + OptimizelyExperiment: OptimizelyExperiment, |
| 211 | + OptimizelyVariation: OptimizelyVariation |
| 212 | + }); |
| 213 | + |
| 214 | + module.exports = optimizelyReactSDK; |
| 215 | + |
| 216 | +})); |
0 commit comments