Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, 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 ReactReconcileTransaction
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
var CallbackQueue = require('CallbackQueue');
16
var PooledClass = require('PooledClass');
17
var ReactBrowserEventEmitter = require('ReactBrowserEventEmitter');
18
var ReactInputSelection = require('ReactInputSelection');
19
var ReactPutListenerQueue = require('ReactPutListenerQueue');
20
var Transaction = require('Transaction');
21
22
var assign = require('Object.assign');
23
24
/**
25
* Ensures that, when possible, the selection range (currently selected text
26
* input) is not disturbed by performing the transaction.
27
*/
28
var SELECTION_RESTORATION = {
29
/**
30
* @return {Selection} Selection information.
31
*/
32
initialize: ReactInputSelection.getSelectionInformation,
33
/**
34
* @param {Selection} sel Selection information returned from `initialize`.
35
*/
36
close: ReactInputSelection.restoreSelection
37
};
38
39
/**
40
* Suppresses events (blur/focus) that could be inadvertently dispatched due to
41
* high level DOM manipulations (like temporarily removing a text input from the
42
* DOM).
43
*/
44
var EVENT_SUPPRESSION = {
45
/**
46
* @return {boolean} The enabled status of `ReactBrowserEventEmitter` before
47
* the reconciliation.
48
*/
49
initialize: function() {
50
var currentlyEnabled = ReactBrowserEventEmitter.isEnabled();
51
ReactBrowserEventEmitter.setEnabled(false);
52
return currentlyEnabled;
53
},
54
55
/**
56
* @param {boolean} previouslyEnabled Enabled status of
57
* `ReactBrowserEventEmitter` before the reconciliation occured. `close`
58
* restores the previous value.
59
*/
60
close: function(previouslyEnabled) {
61
ReactBrowserEventEmitter.setEnabled(previouslyEnabled);
62
}
63
};
64
65
/**
66
* Provides a queue for collecting `componentDidMount` and
67
* `componentDidUpdate` callbacks during the the transaction.
68
*/
69
var ON_DOM_READY_QUEUEING = {
70
/**
71
* Initializes the internal `onDOMReady` queue.
72
*/
73
initialize: function() {
74
this.reactMountReady.reset();
75
},
76
77
/**
78
* After DOM is flushed, invoke all registered `onDOMReady` callbacks.
79
*/
80
close: function() {
81
this.reactMountReady.notifyAll();
82
}
83
};
84
85
var PUT_LISTENER_QUEUEING = {
86
initialize: function() {
87
this.putListenerQueue.reset();
88
},
89
90
close: function() {
91
this.putListenerQueue.putListeners();
92
}
93
};
94
95
/**
96
* Executed within the scope of the `Transaction` instance. Consider these as
97
* being member methods, but with an implied ordering while being isolated from
98
* each other.
99
*/
100
var TRANSACTION_WRAPPERS = [
101
PUT_LISTENER_QUEUEING,
102
SELECTION_RESTORATION,
103
EVENT_SUPPRESSION,
104
ON_DOM_READY_QUEUEING
105
];
106
107
/**
108
* Currently:
109
* - The order that these are listed in the transaction is critical:
110
* - Suppresses events.
111
* - Restores selection range.
112
*
113
* Future:
114
* - Restore document/overflow scroll positions that were unintentionally
115
* modified via DOM insertions above the top viewport boundary.
116
* - Implement/integrate with customized constraint based layout system and keep
117
* track of which dimensions must be remeasured.
118
*
119
* @class ReactReconcileTransaction
120
*/
121
function ReactReconcileTransaction() {
122
this.reinitializeTransaction();
123
// Only server-side rendering really needs this option (see
124
// `ReactServerRendering`), but server-side uses
125
// `ReactServerRenderingTransaction` instead. This option is here so that it's
126
// accessible and defaults to false when `ReactDOMComponent` and
127
// `ReactTextComponent` checks it in `mountComponent`.`
128
this.renderToStaticMarkup = false;
129
this.reactMountReady = CallbackQueue.getPooled(null);
130
this.putListenerQueue = ReactPutListenerQueue.getPooled();
131
}
132
133
var Mixin = {
134
/**
135
* @see Transaction
136
* @abstract
137
* @final
138
* @return {array<object>} List of operation wrap proceedures.
139
* TODO: convert to array<TransactionWrapper>
140
*/
141
getTransactionWrappers: function() {
142
return TRANSACTION_WRAPPERS;
143
},
144
145
/**
146
* @return {object} The queue to collect `onDOMReady` callbacks with.
147
*/
148
getReactMountReady: function() {
149
return this.reactMountReady;
150
},
151
152
getPutListenerQueue: function() {
153
return this.putListenerQueue;
154
},
155
156
/**
157
* `PooledClass` looks for this, and will invoke this before allowing this
158
* instance to be resused.
159
*/
160
destructor: function() {
161
CallbackQueue.release(this.reactMountReady);
162
this.reactMountReady = null;
163
164
ReactPutListenerQueue.release(this.putListenerQueue);
165
this.putListenerQueue = null;
166
}
167
};
168
169
170
assign(ReactReconcileTransaction.prototype, Transaction.Mixin, Mixin);
171
172
PooledClass.addPoolingTo(ReactReconcileTransaction);
173
174
module.exports = ReactReconcileTransaction;
175
176