@samitouri / QOS-React-2 / commits / 95cb79a93e

Remove CallbackRegistry

cpojer committed Jan 3, 2014 at 19:03 UTC 95cb79a93e0856a495e7991af0b6c2ffef0a9f93
7 files changed +121 -174
src/core/ReactEventEmitter.js
+10 -10
@@ -61,16 +61,16 @@ var merge = require('merge');
61 * | ReactEvent | . | | |TapEvent | |------------|
62 * | Emitter | . | |<---+|Plugin | |other plugin|
63 * | | . | | +-----------+ | utilities |
64 - * | +-----------.---------+ | +------------+
65 - * | | | . +----|---------+
66 - * +-----|------+ . | ^ +-----------+
67 - * | . | | |Enter/Leave|
68 - * + . | +-------+|Plugin |
69 - * +-------------+ . v +-----------+
70 - * | application | . +----------+
71 - * |-------------| . | callback |
72 - * | | . | registry |
73 - * | | . +----------+
64 + * | +-----------.--->| | +------------+
65 + * | | | . +--------------+
66 + * +-----|------+ . ^ +-----------+
67 + * | . | |Enter/Leave|
68 + * + . +-------+|Plugin |
69 + * +-------------+ . +-----------+
70 + * | application | .
71 + * |-------------| .
72 + * | | .
73 + * | | .
74 * +-------------+ .
75 * .
76 * React Core . General Purpose Event Plugin System
src/core/__tests__/ReactEventEmitter-test.js
-1
@@ -20,7 +20,6 @@
20
21 require('mock-modules')
22 .dontMock('BrowserScroll')
23 - .dontMock('CallbackRegistry')
23 .dontMock('EventPluginHub')
24 .dontMock('ReactMount')
25 .dontMock('ReactEventEmitter')
src/event/CallbackRegistry.js deleted
-101
@@ -1,101 +0,0 @@
1 -/**
2 - * Copyright 2013 Facebook, Inc.
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and
14 - * limitations under the License.
15 - *
16 - * @providesModule CallbackRegistry
17 - * @typechecks static-only
18 - */
19 -
20 -"use strict";
21 -
22 -var isEventSupported = require('isEventSupported');
23 -
24 -var listenerBank = {};
25 -
26 -/**
27 - * Stores "listeners" by `registrationName`/`id`. There should be at most one
28 - * "listener" per `registrationName`/`id` in the `listenerBank`.
29 - *
30 - * Access listeners via `listenerBank[registrationName][id]`.
31 - *
32 - * @class CallbackRegistry
33 - * @internal
34 - */
35 -var CallbackRegistry = {
36 -
37 - /**
38 - * Stores `listener` at `listenerBank[registrationName][id]`. Is idempotent.
39 - *
40 - * @param {string} id ID of the DOM element.
41 - * @param {string} registrationName Name of listener (e.g. `onClick`).
42 - * @param {?function} listener The callback to store.
43 - */
44 - putListener: function(id, registrationName, listener) {
45 - if (__DEV__) {
46 - // IE8 has no API for event capturing and the `onScroll` event doesn't
47 - // bubble.
48 - if (registrationName === 'onScroll' &&
49 - !isEventSupported('scroll', true)) {
50 - console.warn('This browser doesn\'t support the `onScroll` event');
51 - }
52 - }
53 - var bankForRegistrationName =
54 - listenerBank[registrationName] || (listenerBank[registrationName] = {});
55 - bankForRegistrationName[id] = listener;
56 - },
57 -
58 - /**
59 - * @param {string} id ID of the DOM element.
60 - * @param {string} registrationName Name of listener (e.g. `onClick`).
61 - * @return {?function} The stored callback.
62 - */
63 - getListener: function(id, registrationName) {
64 - var bankForRegistrationName = listenerBank[registrationName];
65 - return bankForRegistrationName && bankForRegistrationName[id];
66 - },
67 -
68 - /**
69 - * Deletes a listener from the registration bank.
70 - *
71 - * @param {string} id ID of the DOM element.
72 - * @param {string} registrationName Name of listener (e.g. `onClick`).
73 - */
74 - deleteListener: function(id, registrationName) {
75 - var bankForRegistrationName = listenerBank[registrationName];
76 - if (bankForRegistrationName) {
77 - delete bankForRegistrationName[id];
78 - }
79 - },
80 -
81 - /**
82 - * Deletes all listeners for the DOM element with the supplied ID.
83 - *
84 - * @param {string} id ID of the DOM element.
85 - */
86 - deleteAllListeners: function(id) {
87 - for (var registrationName in listenerBank) {
88 - delete listenerBank[registrationName][id];
89 - }
90 - },
91 -
92 - /**
93 - * This is needed for tests only. Do not use!
94 - */
95 - __purge: function() {
96 - listenerBank = {};
97 - }
98 -
99 -};
100 -
101 -module.exports = CallbackRegistry;
src/event/EventPluginHub.js
+92 -7
@@ -18,14 +18,18 @@
18
19 "use strict";
20
21 -var CallbackRegistry = require('CallbackRegistry');
21 var EventPluginRegistry = require('EventPluginRegistry');
22 var EventPluginUtils = require('EventPluginUtils');
24 -var EventPropagators = require('EventPropagators');
23
24 var accumulate = require('accumulate');
25 var forEachAccumulated = require('forEachAccumulated');
26 var invariant = require('invariant');
27 +var isEventSupported = require('isEventSupported');
28 +
29 +/**
30 + * Internal store for event listeners
31 + */
32 +var listenerBank = {};
33
34 /**
35 * Internal queue of events that have accumulated their dispatches and are
@@ -55,6 +59,21 @@ var executeDispatchesAndRelease = function(event) {
59 }
60 };
61
62 +/**
63 + * - `InstanceHandle`: [required] Module that performs logical traversals of DOM
64 + * hierarchy given ids of the logical DOM elements involved.
65 + */
66 +var InstanceHandle = null;
67 +
68 +function validateInstanceHandle() {
69 + var invalid = !InstanceHandle||
70 + !InstanceHandle.traverseTwoPhase ||
71 + !InstanceHandle.traverseEnterLeave;
72 + if (invalid) {
73 + throw new Error('InstanceHandle not injected before use!');
74 + }
75 +}
76 +
77 /**
78 * This is a unified interface for event plugins to be installed and configured.
79 *
@@ -94,7 +113,19 @@ var EventPluginHub = {
113 * @param {object} InjectedInstanceHandle
114 * @public
115 */
97 - injectInstanceHandle: EventPropagators.injection.injectInstanceHandle,
116 + injectInstanceHandle: function(InjectedInstanceHandle) {
117 + InstanceHandle = InjectedInstanceHandle;
118 + if (__DEV__) {
119 + validateInstanceHandle();
120 + }
121 + },
122 +
123 + getInstanceHandle: function() {
124 + if (__DEV__) {
125 + validateInstanceHandle();
126 + }
127 + return InstanceHandle;
128 + },
129
130 /**
131 * @param {array} InjectedEventPluginOrder
@@ -111,13 +142,60 @@ var EventPluginHub = {
142
143 registrationNameModules: EventPluginRegistry.registrationNameModules,
144
114 - putListener: CallbackRegistry.putListener,
145 + /**
146 + * Stores `listener` at `listenerBank[registrationName][id]`. Is idempotent.
147 + *
148 + * @param {string} id ID of the DOM element.
149 + * @param {string} registrationName Name of listener (e.g. `onClick`).
150 + * @param {?function} listener The callback to store.
151 + */
152 + putListener: function(id, registrationName, listener) {
153 + if (__DEV__) {
154 + // IE8 has no API for event capturing and the `onScroll` event doesn't
155 + // bubble.
156 + if (registrationName === 'onScroll' &&
157 + !isEventSupported('scroll', true)) {
158 + console.warn('This browser doesn\'t support the `onScroll` event');
159 + }
160 + }
161 + var bankForRegistrationName =
162 + listenerBank[registrationName] || (listenerBank[registrationName] = {});
163 + bankForRegistrationName[id] = listener;
164 + },
165
116 - getListener: CallbackRegistry.getListener,
166 + /**
167 + * @param {string} id ID of the DOM element.
168 + * @param {string} registrationName Name of listener (e.g. `onClick`).
169 + * @return {?function} The stored callback.
170 + */
171 + getListener: function(id, registrationName) {
172 + var bankForRegistrationName = listenerBank[registrationName];
173 + return bankForRegistrationName && bankForRegistrationName[id];
174 + },
175
118 - deleteListener: CallbackRegistry.deleteListener,
176 + /**
177 + * Deletes a listener from the registration bank.
178 + *
179 + * @param {string} id ID of the DOM element.
180 + * @param {string} registrationName Name of listener (e.g. `onClick`).
181 + */
182 + deleteListener: function(id, registrationName) {
183 + var bankForRegistrationName = listenerBank[registrationName];
184 + if (bankForRegistrationName) {
185 + delete bankForRegistrationName[id];
186 + }
187 + },
188
120 - deleteAllListeners: CallbackRegistry.deleteAllListeners,
189 + /**
190 + * Deletes all listeners for the DOM element with the supplied ID.
191 + *
192 + * @param {string} id ID of the DOM element.
193 + */
194 + deleteAllListeners: function(id) {
195 + for (var registrationName in listenerBank) {
196 + delete listenerBank[registrationName][id];
197 + }
198 + },
199
200 /**
201 * Allows registered plugins an opportunity to extract events from top-level
@@ -184,6 +262,13 @@ var EventPluginHub = {
262 'processEventQueue(): Additional events were enqueued while processing ' +
263 'an event queue. Support for this has not yet been implemented.'
264 );
265 + },
266 +
267 + /**
268 + * This is needed for tests only. Do not use!
269 + */
270 + __purge: function() {
271 + listenerBank = {};
272 }
273
274 };
src/event/EventPropagators.js
+6 -42
@@ -18,39 +18,14 @@
18
19 "use strict";
20
21 -var CallbackRegistry = require('CallbackRegistry');
21 var EventConstants = require('EventConstants');
22 +var EventPluginHub = require('EventPluginHub');
23
24 var accumulate = require('accumulate');
25 var forEachAccumulated = require('forEachAccumulated');
26 -var getListener = CallbackRegistry.getListener;
27 -var PropagationPhases = EventConstants.PropagationPhases;
26
29 -/**
30 - * Injected dependencies:
31 - */
32 -
33 -/**
34 - * - `InstanceHandle`: [required] Module that performs logical traversals of DOM
35 - * hierarchy given ids of the logical DOM elements involved.
36 - */
37 -var injection = {
38 - InstanceHandle: null,
39 - injectInstanceHandle: function(InjectedInstanceHandle) {
40 - injection.InstanceHandle = InjectedInstanceHandle;
41 - if (__DEV__) {
42 - injection.validate();
43 - }
44 - },
45 - validate: function() {
46 - var invalid = !injection.InstanceHandle||
47 - !injection.InstanceHandle.traverseTwoPhase ||
48 - !injection.InstanceHandle.traverseEnterLeave;
49 - if (invalid) {
50 - throw new Error('InstanceHandle not injected before use!');
51 - }
52 - }
53 -};
27 +var PropagationPhases = EventConstants.PropagationPhases;
28 +var getListener = EventPluginHub.getListener;
29
30 /**
31 * Some event types have a notion of different registration names for different
@@ -73,7 +48,6 @@ function accumulateDirectionalDispatches(domID, upwards, event) {
48 if (!domID) {
49 throw new Error('Dispatching id must not be null');
50 }
76 - injection.validate();
51 }
52 var phase = upwards ? PropagationPhases.bubbled : PropagationPhases.captured;
53 var listener = listenerAtPhase(domID, event, phase);
@@ -92,7 +66,7 @@ function accumulateDirectionalDispatches(domID, upwards, event) {
66 */
67 function accumulateTwoPhaseDispatchesSingle(event) {
68 if (event && event.dispatchConfig.phasedRegistrationNames) {
95 - injection.InstanceHandle.traverseTwoPhase(
69 + EventPluginHub.injection.getInstanceHandle().traverseTwoPhase(
70 event.dispatchMarker,
71 accumulateDirectionalDispatches,
72 event
@@ -129,17 +103,11 @@ function accumulateDirectDispatchesSingle(event) {
103 }
104
105 function accumulateTwoPhaseDispatches(events) {
132 - if (__DEV__) {
133 - injection.validate();
134 - }
106 forEachAccumulated(events, accumulateTwoPhaseDispatchesSingle);
107 }
108
109 function accumulateEnterLeaveDispatches(leave, enter, fromID, toID) {
139 - if (__DEV__) {
140 - injection.validate();
141 - }
142 - injection.InstanceHandle.traverseEnterLeave(
110 + EventPluginHub.injection.getInstanceHandle().traverseEnterLeave(
111 fromID,
112 toID,
113 accumulateDispatches,
@@ -150,9 +118,6 @@ function accumulateEnterLeaveDispatches(leave, enter, fromID, toID) {
118
119
120 function accumulateDirectDispatches(events) {
153 - if (__DEV__) {
154 - injection.validate();
155 - }
121 forEachAccumulated(events, accumulateDirectDispatchesSingle);
122 }
123
@@ -172,8 +137,7 @@ function accumulateDirectDispatches(events) {
137 var EventPropagators = {
138 accumulateTwoPhaseDispatches: accumulateTwoPhaseDispatches,
139 accumulateDirectDispatches: accumulateDirectDispatches,
175 - accumulateEnterLeaveDispatches: accumulateEnterLeaveDispatches,
176 - injection: injection
140 + accumulateEnterLeaveDispatches: accumulateEnterLeaveDispatches
141 };
142
143 module.exports = EventPropagators;
src/event/__tests__/EventPluginHub-test.js renamed
+5 -5
@@ -19,23 +19,23 @@
19 "use strict";
20
21 require('mock-modules')
22 - .dontMock('CallbackRegistry')
22 + .dontMock('EventPluginHub')
23 .mock('isEventSupported');
24
25 -describe('CallbackRegistry', function() {
26 - var CallbackRegistry;
25 +describe('EventPluginHub', function() {
26 + var EventPluginHub;
27 var isEventSupported;
28
29 beforeEach(function() {
30 require('mock-modules').dumpCache();
31 - CallbackRegistry = require('CallbackRegistry');
31 + EventPluginHub = require('EventPluginHub');
32 isEventSupported = require('isEventSupported');
33 isEventSupported.mockReturnValue(false);
34 });
35
36 it('should warn about the `onScroll` issue on IE8', function() {
37 spyOn(console, 'warn');
38 - CallbackRegistry.putListener(1, 'onScroll', function(){});
38 + EventPluginHub.putListener(1, 'onScroll', function(){});
39 expect(console.warn.callCount).toBe(1);
40 expect(console.warn.mostRecentCall.args[0]).toBe(
41 'This browser doesn\'t support the `onScroll` event'
src/eventPlugins/__tests__/ResponderEventPlugin-test.js
+8 -8
@@ -18,7 +18,7 @@
18
19 "use strict";
20
21 -var CallbackRegistry;
21 +var EventPluginHub;
22 var EventConstants;
23 var EventPropagators;
24 var ReactInstanceHandles;
@@ -40,7 +40,7 @@ var onStartShouldSetResponder = function(id, cb, capture) {
40 var registrationNames = responderEventTypes
41 .startShouldSetResponder
42 .phasedRegistrationNames;
43 - CallbackRegistry.putListener(
43 + EventPluginHub.putListener(
44 id,
45 capture ? registrationNames.captured : registrationNames.bubbled,
46 cb
@@ -51,7 +51,7 @@ var onScrollShouldSetResponder = function(id, cb, capture) {
51 var registrationNames = responderEventTypes
52 .scrollShouldSetResponder
53 .phasedRegistrationNames;
54 - CallbackRegistry.putListener(
54 + EventPluginHub.putListener(
55 id,
56 capture ? registrationNames.captured : registrationNames.bubbled,
57 cb
@@ -62,7 +62,7 @@ var onMoveShouldSetResponder = function(id, cb, capture) {
62 var registrationNames = responderEventTypes
63 .moveShouldSetResponder
64 .phasedRegistrationNames;
65 - CallbackRegistry.putListener(
65 + EventPluginHub.putListener(
66 id,
67 capture ? registrationNames.captured : registrationNames.bubbled,
68 cb
@@ -71,7 +71,7 @@ var onMoveShouldSetResponder = function(id, cb, capture) {
71
72
73 var onResponderGrant = function(id, cb) {
74 - CallbackRegistry.putListener(
74 + EventPluginHub.putListener(
75 id,
76 responderEventTypes.responderGrant.registrationName,
77 cb
@@ -222,18 +222,18 @@ describe('ResponderEventPlugin', function() {
222 beforeEach(function() {
223 require('mock-modules').dumpCache();
224
225 - CallbackRegistry = require('CallbackRegistry');
225 + EventPluginHub = require('EventPluginHub');
226 EventConstants = require('EventConstants');
227 EventPropagators = require('EventPropagators');
228 ReactInstanceHandles = require('ReactInstanceHandles');
229 ResponderEventPlugin = require('ResponderEventPlugin');
230 SyntheticEvent = require('SyntheticEvent');
231 - EventPropagators.injection.injectInstanceHandle(ReactInstanceHandles);
231 + EventPluginHub.injection.injectInstanceHandle(ReactInstanceHandles);
232
233 // dumpCache, in open-source tests, only resets existing mocks. It does not
234 // reset module-state though -- so we need to do this explicitly in the test
235 // for now. Once that's no longer the case, we can delete this line.
236 - CallbackRegistry.__purge();
236 + EventPluginHub.__purge();
237
238 topLevelTypes = EventConstants.topLevelTypes;
239 responderEventTypes = ResponderEventPlugin.eventTypes;