UNPKG

3.98 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react')) :
3 typeof define === 'function' && define.amd ? define(['react'], factory) :
4 (global = global || self, global.withSideEffect = factory(global.React));
5}(this, function (React) { 'use strict';
6
7 var React__default = 'default' in React ? React['default'] : React;
8
9 function _defineProperty(obj, key, value) {
10 if (key in obj) {
11 Object.defineProperty(obj, key, {
12 value: value,
13 enumerable: true,
14 configurable: true,
15 writable: true
16 });
17 } else {
18 obj[key] = value;
19 }
20
21 return obj;
22 }
23
24 function _inheritsLoose(subClass, superClass) {
25 subClass.prototype = Object.create(superClass.prototype);
26 subClass.prototype.constructor = subClass;
27 subClass.__proto__ = superClass;
28 }
29
30 var canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement);
31 function withSideEffect(reducePropsToState, handleStateChangeOnClient, mapStateOnServer) {
32 if (typeof reducePropsToState !== 'function') {
33 throw new Error('Expected reducePropsToState to be a function.');
34 }
35
36 if (typeof handleStateChangeOnClient !== 'function') {
37 throw new Error('Expected handleStateChangeOnClient to be a function.');
38 }
39
40 if (typeof mapStateOnServer !== 'undefined' && typeof mapStateOnServer !== 'function') {
41 throw new Error('Expected mapStateOnServer to either be undefined or a function.');
42 }
43
44 function getDisplayName(WrappedComponent) {
45 return WrappedComponent.displayName || WrappedComponent.name || 'Component';
46 }
47
48 return function wrap(WrappedComponent) {
49 if (typeof WrappedComponent !== 'function') {
50 throw new Error('Expected WrappedComponent to be a React component.');
51 }
52
53 var mountedInstances = [];
54 var state;
55
56 function emitChange() {
57 state = reducePropsToState(mountedInstances.map(function (instance) {
58 return instance.props;
59 }));
60
61 if (SideEffect.canUseDOM) {
62 handleStateChangeOnClient(state);
63 } else if (mapStateOnServer) {
64 state = mapStateOnServer(state);
65 }
66 }
67
68 var SideEffect =
69 /*#__PURE__*/
70 function (_PureComponent) {
71 _inheritsLoose(SideEffect, _PureComponent);
72
73 function SideEffect() {
74 return _PureComponent.apply(this, arguments) || this;
75 }
76
77 // Try to use displayName of wrapped component
78 // Expose canUseDOM so tests can monkeypatch it
79 SideEffect.peek = function peek() {
80 return state;
81 };
82
83 SideEffect.rewind = function rewind() {
84 if (SideEffect.canUseDOM) {
85 throw new Error('You may only call rewind() on the server. Call peek() to read the current state.');
86 }
87
88 var recordedState = state;
89 state = undefined;
90 mountedInstances = [];
91 return recordedState;
92 };
93
94 var _proto = SideEffect.prototype;
95
96 _proto.UNSAFE_componentWillMount = function UNSAFE_componentWillMount() {
97 mountedInstances.push(this);
98 emitChange();
99 };
100
101 _proto.componentDidUpdate = function componentDidUpdate() {
102 emitChange();
103 };
104
105 _proto.componentWillUnmount = function componentWillUnmount() {
106 var index = mountedInstances.indexOf(this);
107 mountedInstances.splice(index, 1);
108 emitChange();
109 };
110
111 _proto.render = function render() {
112 return React__default.createElement(WrappedComponent, this.props);
113 };
114
115 return SideEffect;
116 }(React.PureComponent);
117
118 _defineProperty(SideEffect, "displayName", "SideEffect(" + getDisplayName(WrappedComponent) + ")");
119
120 _defineProperty(SideEffect, "canUseDOM", canUseDOM);
121
122 return SideEffect;
123 };
124 }
125
126 return withSideEffect;
127
128}));