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
+ * @providesModule ReactServerUpdateQueue
10
+ * @flow
11
+ */
12
+
13
+'use strict';
14
+
15
+var ReactUpdateQueue = require('ReactUpdateQueue');
16
+var Transaction = require('Transaction');
17
+var warning = require('warning');
18
+
19
+function warnNoop(publicInstance: ReactComponent<any, any, any>, callerName: string) {
20
+ if (__DEV__) {
21
+ var constructor = publicInstance.constructor;
22
+ warning(
23
+ false,
24
+ '%s(...): Can only update a mounting component. ' +
25
+ 'This usually means you called %s() outside componentWillMount() on the server. ' +
26
+ 'This is a no-op. Please check the code for the %s component.',
27
+ callerName,
28
+ callerName,
29
+ constructor && (constructor.displayName || constructor.name) || 'ReactClass'
30
+ );
31
+ }
32
+}
33
+
34
+/**
35
+ * This is the update queue used for server rendering.
36
+ * It delegates to ReactUpdateQueue while server rendering is in progress and
37
+ * switches to ReactNoopUpdateQueue after the transaction has completed.
38
+ * @class ReactServerUpdateQueue
39
+ * @param {Transaction} transaction
40
+ */
41
+class ReactServerUpdateQueue {
42
+ /* :: transaction: Transaction; */
43
+
44
+ constructor(transaction: Transaction) {
45
+ this.transaction = transaction;
46
+ }
47
+
48
+ /**
49
+ * Checks whether or not this composite component is mounted.
50
+ * @param {ReactClass} publicInstance The instance we want to test.
51
+ * @return {boolean} True if mounted, false otherwise.
52
+ * @protected
53
+ * @final
54
+ */
55
+ isMounted(publicInstance: ReactComponent<any, any, any>): boolean {
56
+ return false;
57
+ }
58
+
59
+ /**
60
+ * Enqueue a callback that will be executed after all the pending updates
61
+ * have processed.
62
+ *
63
+ * @param {ReactClass} publicInstance The instance to use as `this` context.
64
+ * @param {?function} callback Called after state is updated.
65
+ * @internal
66
+ */
67
+ enqueueCallback(publicInstance: ReactComponent<any, any, any>, callback?: Function, callerName?: string) {
68
+ if (this.transaction.isInTransaction()) {
69
+ ReactUpdateQueue.enqueueCallback(publicInstance, callback, callerName);
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Forces an update. This should only be invoked when it is known with
75
+ * certainty that we are **not** in a DOM transaction.
76
+ *
77
+ * You may want to call this when you know that some deeper aspect of the
78
+ * component's state has changed but `setState` was not called.
79
+ *
80
+ * This will not invoke `shouldComponentUpdate`, but it will invoke
81
+ * `componentWillUpdate` and `componentDidUpdate`.
82
+ *
83
+ * @param {ReactClass} publicInstance The instance that should rerender.
84
+ * @internal
85
+ */
86
+ enqueueForceUpdate(publicInstance: ReactComponent<any, any, any>) {
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
+ enqueueReplaceState(publicInstance: ReactComponent<any, any, any>, completeState: Object|Function) {
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
+ enqueueSetState(publicInstance: ReactComponent<any, any, any>, partialState: Object|Function) {
124
+ if (this.transaction.isInTransaction()) {
125
+ ReactUpdateQueue.enqueueSetState(publicInstance, partialState);
126
+ } else {
127
+ warnNoop(publicInstance, 'setState');
128
+ }
129
+ }
130
+}
131
+
132
+module.exports = ReactServerUpdateQueue;