UNPKG

diet-yadda

Version:

A true BDD framework for JavaScript - slimmed down

69 lines (58 loc) 2.08 kB
/* * Copyright 2010 Acuminous Ltd / Energized Work Ltd * * 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. */ /* jslint node: true */ "use strict"; var $ = require('./Array'); var fn = require('./fn'); var event_bus = new EventBus(); // A communication channel between event emitters and event listeners function EventBus() { var event_handlers = $(); var _this = this; this.send = function(event_name, event_data, next) { if (arguments.length == 1) return this.send(event_name, {}); if (arguments.length == 2 && fn.is_function(event_data)) return this.send(event_name, {}, event_data); notify_handlers(event_name, event_data); next && next(); return this; }; this.on = function(event_pattern, callback) { event_handlers.push({ pattern: event_pattern, callback: callback }); return this; }; var notify_handlers = function(event_name, event_data) { find_handlers(event_name).each(function(callback) { callback({ name: event_name, data: event_data }); }); }; var find_handlers = function(event_name) { return event_handlers.find_all(function(handler) { return new RegExp(handler.pattern).test(event_name); }).collect(function(handler) { return handler.callback; }); }; } function instance() { return event_bus; } module.exports = { instance: instance, ON_SCENARIO: '__ON_SCENARIO__', ON_STEP: '__ON_STEP__', ON_EXECUTE: '__ON_EXECUTE__', ON_DEFINE: '__ON_DEFINE__' };