react / react-0.13.3 / examples / basic-commonjs / node_modules / react / lib / LocalEventTrapMixin.js
81144 views/**1* Copyright 2014-2015, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule LocalEventTrapMixin9*/1011'use strict';1213var ReactBrowserEventEmitter = require("./ReactBrowserEventEmitter");1415var accumulateInto = require("./accumulateInto");16var forEachAccumulated = require("./forEachAccumulated");17var invariant = require("./invariant");1819function remove(event) {20event.remove();21}2223var LocalEventTrapMixin = {24trapBubbledEvent:function(topLevelType, handlerBaseName) {25("production" !== process.env.NODE_ENV ? invariant(this.isMounted(), 'Must be mounted to trap events') : invariant(this.isMounted()));26// If a component renders to null or if another component fatals and causes27// the state of the tree to be corrupted, `node` here can be null.28var node = this.getDOMNode();29("production" !== process.env.NODE_ENV ? invariant(30node,31'LocalEventTrapMixin.trapBubbledEvent(...): Requires node to be rendered.'32) : invariant(node));33var listener = ReactBrowserEventEmitter.trapBubbledEvent(34topLevelType,35handlerBaseName,36node37);38this._localEventListeners =39accumulateInto(this._localEventListeners, listener);40},4142// trapCapturedEvent would look nearly identical. We don't implement that43// method because it isn't currently needed.4445componentWillUnmount:function() {46if (this._localEventListeners) {47forEachAccumulated(this._localEventListeners, remove);48}49}50};5152module.exports = LocalEventTrapMixin;535455