Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/event/CallbackRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"use strict";

var listenerBank = {};
var invariant = require('invariant');

/**
* Stores "listeners" by `registrationName`/`id`. There should be at most one
Expand All @@ -40,6 +41,11 @@ var CallbackRegistry = {
* @param {?function} listener The callback to store.
*/
putListener: function(id, registrationName, listener) {
invariant(
typeof listener === 'function',
'Trying to bind an event handler to a non-function for %s',
registrationName
);
var bankForRegistrationName =
listenerBank[registrationName] || (listenerBank[registrationName] = {});
bankForRegistrationName[id] = listener;
Expand Down Expand Up @@ -88,4 +94,4 @@ var CallbackRegistry = {

};

module.exports = CallbackRegistry;
module.exports = CallbackRegistry;
40 changes: 40 additions & 0 deletions src/event/__tests__/CallbackRegistry-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @emails react-core
*/

"use strict";

describe('CallbackRegistry', function() {
var CallbackRegistry;

beforeEach(function() {
CallbackRegistry = require('CallbackRegistry');
});

it('should bind to a function', function(){
var noop = function(){};
expect(function(){
CallbackRegistry.putListener('test', 'onClick', noop);
}).not.toThrow();
});

it('should throw when binding to a non-function', function(){
expect(function(){
CallbackRegistry.putListener('test', 'onClick', {});
}).toThrow();
});
});