Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80517 views
1
/**
2
* Copyright 2014-2015, 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 LocalEventTrapMixin
10
*/
11
12
'use strict';
13
14
var ReactBrowserEventEmitter = require("./ReactBrowserEventEmitter");
15
16
var accumulateInto = require("./accumulateInto");
17
var forEachAccumulated = require("./forEachAccumulated");
18
var invariant = require("./invariant");
19
20
function remove(event) {
21
event.remove();
22
}
23
24
var LocalEventTrapMixin = {
25
trapBubbledEvent:function(topLevelType, handlerBaseName) {
26
("production" !== process.env.NODE_ENV ? invariant(this.isMounted(), 'Must be mounted to trap events') : invariant(this.isMounted()));
27
// If a component renders to null or if another component fatals and causes
28
// the state of the tree to be corrupted, `node` here can be null.
29
var node = this.getDOMNode();
30
("production" !== process.env.NODE_ENV ? invariant(
31
node,
32
'LocalEventTrapMixin.trapBubbledEvent(...): Requires node to be rendered.'
33
) : invariant(node));
34
var listener = ReactBrowserEventEmitter.trapBubbledEvent(
35
topLevelType,
36
handlerBaseName,
37
node
38
);
39
this._localEventListeners =
40
accumulateInto(this._localEventListeners, listener);
41
},
42
43
// trapCapturedEvent would look nearly identical. We don't implement that
44
// method because it isn't currently needed.
45
46
componentWillUnmount:function() {
47
if (this._localEventListeners) {
48
forEachAccumulated(this._localEventListeners, remove);
49
}
50
}
51
};
52
53
module.exports = LocalEventTrapMixin;
54
55