Skip to content

Commit

Permalink
Initial outline for new devtools api
Browse files Browse the repository at this point in the history
  • Loading branch information
jim committed Nov 12, 2015
1 parent fc24522 commit 0f114f5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/renderers/dom/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'use strict';

var ReactDOMComponentTree = require('ReactDOMComponentTree');
var ReactDOMDevtools = require('ReactDOMDevtools');
var ReactDefaultInjection = require('ReactDefaultInjection');
var ReactMount = require('ReactMount');
var ReactPerf = require('ReactPerf');
Expand All @@ -35,6 +36,8 @@ var React = {
render: render,
unmountComponentAtNode: ReactMount.unmountComponentAtNode,
version: ReactVersion,
addDevtool: addDevtool,
removeDevtool: removeDevtool,

/* eslint-disable camelcase */
unstable_batchedUpdates: ReactUpdates.batchedUpdates,
Expand Down
52 changes: 52 additions & 0 deletions src/renderers/dom/shared/ReactDOMDevtools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactDOMDevtools
*/

'use strict';

var warning = require('warning');

var eventHandlers = [];
var handlerDoesThrowForEvent = {};

var ReactDOMDevtools = {
addDevtool(devtool) {
eventHandlers.push(devtool);
},

removeDevtool(devtool) {
for (var i = 0; i < eventHandlers.length; i++) {
if (eventHandlers[i] === devtool) {
eventHandlers.splice(i, 1);
i--;
}
}
},

emitEvent(eventName, eventData) {
if (__DEV__) {
eventHandlers.forEach(function(handler) {
try {
handler.handleEvent(eventName, eventData);
} catch (e) {
warning(
!handlerDoesThrowForEvent[eventName],
'exception thrown by devtool while handling %s: %s',
eventName,
e.message
);
handlerDoesThrowForEvent[eventName] = true;
}
});
}
},
};

module.exports = ReactDOMDevtools;

0 comments on commit 0f114f5

Please sign in to comment.