UNPKG

4.71 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 */
9
10'use strict';
11
12function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13
14var ReactUpdateQueue = require('./ReactUpdateQueue');
15
16var warning = require('fbjs/lib/warning');
17
18function warnNoop(publicInstance, callerName) {
19 if (process.env.NODE_ENV !== 'production') {
20 var constructor = publicInstance.constructor;
21 process.env.NODE_ENV !== 'production' ? warning(false, '%s(...): Can only update a mounting component. ' + 'This usually means you called %s() outside componentWillMount() on the server. ' + 'This is a no-op. Please check the code for the %s component.', callerName, callerName, constructor && (constructor.displayName || constructor.name) || 'ReactClass') : void 0;
22 }
23}
24
25/**
26 * This is the update queue used for server rendering.
27 * It delegates to ReactUpdateQueue while server rendering is in progress and
28 * switches to ReactNoopUpdateQueue after the transaction has completed.
29 * @class ReactServerUpdateQueue
30 * @param {Transaction} transaction
31 */
32
33var ReactServerUpdateQueue = function () {
34 function ReactServerUpdateQueue(transaction) {
35 _classCallCheck(this, ReactServerUpdateQueue);
36
37 this.transaction = transaction;
38 }
39
40 /**
41 * Checks whether or not this composite component is mounted.
42 * @param {ReactClass} publicInstance The instance we want to test.
43 * @return {boolean} True if mounted, false otherwise.
44 * @protected
45 * @final
46 */
47
48
49 ReactServerUpdateQueue.prototype.isMounted = function isMounted(publicInstance) {
50 return false;
51 };
52
53 /**
54 * Enqueue a callback that will be executed after all the pending updates
55 * have processed.
56 *
57 * @param {ReactClass} publicInstance The instance to use as `this` context.
58 * @param {?function} callback Called after state is updated.
59 * @internal
60 */
61
62
63 ReactServerUpdateQueue.prototype.enqueueCallback = function enqueueCallback(publicInstance, callback, callerName) {
64 if (this.transaction.isInTransaction()) {
65 ReactUpdateQueue.enqueueCallback(publicInstance, callback, callerName);
66 }
67 };
68
69 /**
70 * Forces an update. This should only be invoked when it is known with
71 * certainty that we are **not** in a DOM transaction.
72 *
73 * You may want to call this when you know that some deeper aspect of the
74 * component's state has changed but `setState` was not called.
75 *
76 * This will not invoke `shouldComponentUpdate`, but it will invoke
77 * `componentWillUpdate` and `componentDidUpdate`.
78 *
79 * @param {ReactClass} publicInstance The instance that should rerender.
80 * @internal
81 */
82
83
84 ReactServerUpdateQueue.prototype.enqueueForceUpdate = function enqueueForceUpdate(publicInstance) {
85 if (this.transaction.isInTransaction()) {
86 ReactUpdateQueue.enqueueForceUpdate(publicInstance);
87 } else {
88 warnNoop(publicInstance, 'forceUpdate');
89 }
90 };
91
92 /**
93 * Replaces all of the state. Always use this or `setState` to mutate state.
94 * You should treat `this.state` as immutable.
95 *
96 * There is no guarantee that `this.state` will be immediately updated, so
97 * accessing `this.state` after calling this method may return the old value.
98 *
99 * @param {ReactClass} publicInstance The instance that should rerender.
100 * @param {object|function} completeState Next state.
101 * @internal
102 */
103
104
105 ReactServerUpdateQueue.prototype.enqueueReplaceState = function enqueueReplaceState(publicInstance, completeState) {
106 if (this.transaction.isInTransaction()) {
107 ReactUpdateQueue.enqueueReplaceState(publicInstance, completeState);
108 } else {
109 warnNoop(publicInstance, 'replaceState');
110 }
111 };
112
113 /**
114 * Sets a subset of the state. This only exists because _pendingState is
115 * internal. This provides a merging strategy that is not available to deep
116 * properties which is confusing. TODO: Expose pendingState or don't use it
117 * during the merge.
118 *
119 * @param {ReactClass} publicInstance The instance that should rerender.
120 * @param {object|function} partialState Next partial state to be merged with state.
121 * @internal
122 */
123
124
125 ReactServerUpdateQueue.prototype.enqueueSetState = function enqueueSetState(publicInstance, partialState) {
126 if (this.transaction.isInTransaction()) {
127 ReactUpdateQueue.enqueueSetState(publicInstance, partialState);
128 } else {
129 warnNoop(publicInstance, 'setState');
130 }
131 };
132
133 return ReactServerUpdateQueue;
134}();
135
136module.exports = ReactServerUpdateQueue;
\No newline at end of file