1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var _assign = require('object-assign');
|
12 |
|
13 | var PooledClass = require('./PooledClass');
|
14 |
|
15 | var emptyFunction = require('fbjs/lib/emptyFunction');
|
16 | var warning = require('fbjs/lib/warning');
|
17 |
|
18 | var didWarnForAddedNewProperty = false;
|
19 | var isProxySupported = typeof Proxy === 'function';
|
20 |
|
21 | var shouldBeReleasedProperties = ['dispatchConfig', '_targetInst', 'nativeEvent', 'isDefaultPrevented', 'isPropagationStopped', '_dispatchListeners', '_dispatchInstances'];
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var EventInterface = {
|
28 | type: null,
|
29 | target: null,
|
30 |
|
31 | currentTarget: emptyFunction.thatReturnsNull,
|
32 | eventPhase: null,
|
33 | bubbles: null,
|
34 | cancelable: null,
|
35 | timeStamp: function (event) {
|
36 | return event.timeStamp || Date.now();
|
37 | },
|
38 | defaultPrevented: null,
|
39 | isTrusted: null
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function SyntheticEvent(dispatchConfig, targetInst, nativeEvent, nativeEventTarget) {
|
61 | if (process.env.NODE_ENV !== 'production') {
|
62 |
|
63 | delete this.nativeEvent;
|
64 | delete this.preventDefault;
|
65 | delete this.stopPropagation;
|
66 | }
|
67 |
|
68 | this.dispatchConfig = dispatchConfig;
|
69 | this._targetInst = targetInst;
|
70 | this.nativeEvent = nativeEvent;
|
71 |
|
72 | var Interface = this.constructor.Interface;
|
73 | for (var propName in Interface) {
|
74 | if (!Interface.hasOwnProperty(propName)) {
|
75 | continue;
|
76 | }
|
77 | if (process.env.NODE_ENV !== 'production') {
|
78 | delete this[propName];
|
79 | }
|
80 | var normalize = Interface[propName];
|
81 | if (normalize) {
|
82 | this[propName] = normalize(nativeEvent);
|
83 | } else {
|
84 | if (propName === 'target') {
|
85 | this.target = nativeEventTarget;
|
86 | } else {
|
87 | this[propName] = nativeEvent[propName];
|
88 | }
|
89 | }
|
90 | }
|
91 |
|
92 | var defaultPrevented = nativeEvent.defaultPrevented != null ? nativeEvent.defaultPrevented : nativeEvent.returnValue === false;
|
93 | if (defaultPrevented) {
|
94 | this.isDefaultPrevented = emptyFunction.thatReturnsTrue;
|
95 | } else {
|
96 | this.isDefaultPrevented = emptyFunction.thatReturnsFalse;
|
97 | }
|
98 | this.isPropagationStopped = emptyFunction.thatReturnsFalse;
|
99 | return this;
|
100 | }
|
101 |
|
102 | _assign(SyntheticEvent.prototype, {
|
103 | preventDefault: function () {
|
104 | this.defaultPrevented = true;
|
105 | var event = this.nativeEvent;
|
106 | if (!event) {
|
107 | return;
|
108 | }
|
109 |
|
110 | if (event.preventDefault) {
|
111 | event.preventDefault();
|
112 |
|
113 | } else if (typeof event.returnValue !== 'unknown') {
|
114 | event.returnValue = false;
|
115 | }
|
116 | this.isDefaultPrevented = emptyFunction.thatReturnsTrue;
|
117 | },
|
118 |
|
119 | stopPropagation: function () {
|
120 | var event = this.nativeEvent;
|
121 | if (!event) {
|
122 | return;
|
123 | }
|
124 |
|
125 | if (event.stopPropagation) {
|
126 | event.stopPropagation();
|
127 |
|
128 | } else if (typeof event.cancelBubble !== 'unknown') {
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | event.cancelBubble = true;
|
135 | }
|
136 |
|
137 | this.isPropagationStopped = emptyFunction.thatReturnsTrue;
|
138 | },
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 | persist: function () {
|
146 | this.isPersistent = emptyFunction.thatReturnsTrue;
|
147 | },
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 |
|
154 | isPersistent: emptyFunction.thatReturnsFalse,
|
155 |
|
156 | |
157 |
|
158 |
|
159 | destructor: function () {
|
160 | var Interface = this.constructor.Interface;
|
161 | for (var propName in Interface) {
|
162 | if (process.env.NODE_ENV !== 'production') {
|
163 | Object.defineProperty(this, propName, getPooledWarningPropertyDefinition(propName, Interface[propName]));
|
164 | } else {
|
165 | this[propName] = null;
|
166 | }
|
167 | }
|
168 | for (var i = 0; i < shouldBeReleasedProperties.length; i++) {
|
169 | this[shouldBeReleasedProperties[i]] = null;
|
170 | }
|
171 | if (process.env.NODE_ENV !== 'production') {
|
172 | Object.defineProperty(this, 'nativeEvent', getPooledWarningPropertyDefinition('nativeEvent', null));
|
173 | Object.defineProperty(this, 'preventDefault', getPooledWarningPropertyDefinition('preventDefault', emptyFunction));
|
174 | Object.defineProperty(this, 'stopPropagation', getPooledWarningPropertyDefinition('stopPropagation', emptyFunction));
|
175 | }
|
176 | }
|
177 | });
|
178 |
|
179 | SyntheticEvent.Interface = EventInterface;
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | SyntheticEvent.augmentClass = function (Class, Interface) {
|
188 | var Super = this;
|
189 |
|
190 | var E = function () {};
|
191 | E.prototype = Super.prototype;
|
192 | var prototype = new E();
|
193 |
|
194 | _assign(prototype, Class.prototype);
|
195 | Class.prototype = prototype;
|
196 | Class.prototype.constructor = Class;
|
197 |
|
198 | Class.Interface = _assign({}, Super.Interface, Interface);
|
199 | Class.augmentClass = Super.augmentClass;
|
200 |
|
201 | PooledClass.addPoolingTo(Class, PooledClass.fourArgumentPooler);
|
202 | };
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | if (process.env.NODE_ENV !== 'production') {
|
209 | if (isProxySupported) {
|
210 |
|
211 | SyntheticEvent = new Proxy(SyntheticEvent, {
|
212 | construct: function (target, args) {
|
213 | return this.apply(target, Object.create(target.prototype), args);
|
214 | },
|
215 | apply: function (constructor, that, args) {
|
216 | return new Proxy(constructor.apply(that, args), {
|
217 | set: function (target, prop, value) {
|
218 | if (prop !== 'isPersistent' && !target.constructor.Interface.hasOwnProperty(prop) && shouldBeReleasedProperties.indexOf(prop) === -1) {
|
219 | process.env.NODE_ENV !== 'production' ? warning(didWarnForAddedNewProperty || target.isPersistent(), "This synthetic event is reused for performance reasons. If you're " + "seeing this, you're adding a new property in the synthetic event object. " + 'The property is never released. See ' + 'https://fb.me/react-event-pooling for more information.') : void 0;
|
220 | didWarnForAddedNewProperty = true;
|
221 | }
|
222 | target[prop] = value;
|
223 | return true;
|
224 | }
|
225 | });
|
226 | }
|
227 | });
|
228 |
|
229 | }
|
230 | }
|
231 |
|
232 | PooledClass.addPoolingTo(SyntheticEvent, PooledClass.fourArgumentPooler);
|
233 |
|
234 | module.exports = SyntheticEvent;
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | function getPooledWarningPropertyDefinition(propName, getVal) {
|
244 | var isFunction = typeof getVal === 'function';
|
245 | return {
|
246 | configurable: true,
|
247 | set: set,
|
248 | get: get
|
249 | };
|
250 |
|
251 | function set(val) {
|
252 | var action = isFunction ? 'setting the method' : 'setting the property';
|
253 | warn(action, 'This is effectively a no-op');
|
254 | return val;
|
255 | }
|
256 |
|
257 | function get() {
|
258 | var action = isFunction ? 'accessing the method' : 'accessing the property';
|
259 | var result = isFunction ? 'This is a no-op function' : 'This is set to null';
|
260 | warn(action, result);
|
261 | return getVal;
|
262 | }
|
263 |
|
264 | function warn(action, result) {
|
265 | var warningCondition = false;
|
266 | process.env.NODE_ENV !== 'production' ? warning(warningCondition, "This synthetic event is reused for performance reasons. If you're seeing this, " + "you're %s `%s` on a released/nullified synthetic event. %s. " + 'If you must keep the original synthetic event around, use event.persist(). ' + 'See https://fb.me/react-event-pooling for more information.', action, propName, result) : void 0;
|
267 | }
|
268 | } |
\ | No newline at end of file |