UNPKG

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