Browse Source

Implemented a simple eventbus

css_transitions
josdejong 11 years ago
parent
commit
00054668b4
8 changed files with 205 additions and 7 deletions
  1. +21
    -1
      Jakefile.js
  2. +2
    -1
      package.json
  3. +64
    -0
      src/eventbus.js
  4. +1
    -0
      src/module/exports.js
  5. +5
    -1
      src/util.js
  6. +38
    -0
      test/eventbus.js
  7. +71
    -1
      vis.js
  8. +3
    -3
      vis.min.js

+ 21
- 1
Jakefile.js View File

@ -17,7 +17,7 @@ var VIS_MIN = './vis.min.js';
* default task * default task
*/ */
desc('Execute all tasks: build all libraries'); desc('Execute all tasks: build all libraries');
task('default', ['build', 'minify'], function () {
task('default', ['build', 'minify', 'test'], function () {
console.log('done'); console.log('done');
}); });
@ -50,6 +50,7 @@ task('build', {async: true}, function () {
'./src/dataset.js', './src/dataset.js',
'./src/stack.js', './src/stack.js',
'./src/range.js', './src/range.js',
'./src/eventbus.js',
'./src/controller.js', './src/controller.js',
'./src/component/component.js', './src/component/component.js',
@ -113,6 +114,25 @@ task('minify', function () {
console.log('created ' + VIS_MIN); console.log('created ' + VIS_MIN);
}); });
/**
* test task
*/
desc('Test the library');
task('test', ['build'], function () {
// TODO: use a testing suite for testing: nodeunit, mocha, tap, ...
var filelist = new jake.FileList();
filelist.include([
'./test/**/*.js'
]);
var files = filelist.toArray();
files.forEach(function (file) {
require('./' + file);
});
console.log('Executed ' + files.length + ' test files successfully');
});
/** /**
* replace version, date, and name placeholders in the provided file * replace version, date, and name placeholders in the provided file
* @param {String} filename * @param {String} filename

+ 2
- 1
package.json View File

@ -21,7 +21,8 @@
"browser" "browser"
], ],
"scripts": { "scripts": {
"prepublish": "jake"
"prepublish": "jake",
"test": "jake test --trace"
}, },
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {

+ 64
- 0
src/eventbus.js View File

@ -0,0 +1,64 @@
/**
* An event bus can be used to emit events, and to subscribe to events
* @constructor EventBus
*/
function EventBus() {
this.subscriptions = [];
}
/**
* Subscribe to an event
* @param {String | RegExp} event The event can be a regular expression, or
* a string with wildcards, like 'server.*'.
* @param {function} callback. Callback are called with three parameters:
* {String} event, {Object} [data],
* {*} [source]
* @param {*} [target]
* @returns {String} id A subscription id
*/
EventBus.prototype.on = function (event, callback, target) {
var subscription = {
id: util.randomUUID(),
event: event instanceof RegExp ? event : new RegExp(event.replace('*', '\\w+')),
callback: (typeof callback === 'function') ? callback : null,
target: target
};
this.subscriptions.push(subscription);
return subscription.id;
};
/**
* Unsubscribe from an event
* @param {String} id subscription id
*/
EventBus.prototype.off = function (id) {
var i = 0;
while (i < this.subscriptions.length) {
var subscription = this.subscriptions[i];
if (subscription.id == id) {
this.subscriptions.splice(i, 1);
}
else {
i++;
}
}
};
/**
* Emit an event
* @param {String} event
* @param {Object} [data]
* @param {*} [source]
*/
EventBus.prototype.emit = function (event, data, source) {
for (var i =0; i < this.subscriptions.length; i++) {
var subscription = this.subscriptions[i];
if (subscription.event.test(event)) {
if (subscription.callback) {
subscription.callback(event, data, source);
}
}
}
};

+ 1
- 0
src/module/exports.js View File

@ -10,6 +10,7 @@ var vis = {
Range: Range, Range: Range,
Stack: Stack, Stack: Stack,
TimeStep: TimeStep, TimeStep: TimeStep,
EventBus: EventBus,
components: { components: {
items: { items: {

+ 5
- 1
src/util.js View File

@ -597,9 +597,13 @@ util.option.asElement = function (value, defaultValue) {
* @param {String} css Text containing css * @param {String} css Text containing css
*/ */
util.loadCss = function (css) { util.loadCss = function (css) {
if (typeof document === 'undefined') {
return;
}
// get the script location, and built the css file name from the js file name // get the script location, and built the css file name from the js file name
// http://stackoverflow.com/a/2161748/1262753 // http://stackoverflow.com/a/2161748/1262753
var scripts = document.getElementsByTagName('script');
// var scripts = document.getElementsByTagName('script');
// var jsFile = scripts[scripts.length-1].src.split('?')[0]; // var jsFile = scripts[scripts.length-1].src.split('?')[0];
// var cssFile = jsFile.substring(0, jsFile.length - 2) + 'css'; // var cssFile = jsFile.substring(0, jsFile.length - 2) + 'css';

+ 38
- 0
test/eventbus.js View File

@ -0,0 +1,38 @@
// test vis.EventBus
var assert = require('assert'),
vis = require('../vis');
var bus = new vis.EventBus();
var received = [];
var id1 = '1';
bus.on('message', function (event, data, source) {
received.push({
event: event,
data: data,
source: source
});
}, id1);
var id2 = '2';
bus.emit('message', {text: 'hello world'}, id2);
bus.on('chat:*', function (event, data, source) {
received.push({
event: event,
data: data,
source: source
});
});
bus.emit('chat:1', null, id2);
bus.emit('chat:2', {text: 'hello world'}, id1);
// verify if the messages are received
assert.deepEqual(received, [
{event: 'message', data: {text: 'hello world'}, source: id2},
{event: 'chat:1', data: null, source: id2},
{event: 'chat:2', data: {text: 'hello world'}, source: id1}
]);

+ 71
- 1
vis.js View File

@ -628,9 +628,13 @@ util.option.asElement = function (value, defaultValue) {
* @param {String} css Text containing css * @param {String} css Text containing css
*/ */
util.loadCss = function (css) { util.loadCss = function (css) {
if (typeof document === 'undefined') {
return;
}
// get the script location, and built the css file name from the js file name // get the script location, and built the css file name from the js file name
// http://stackoverflow.com/a/2161748/1262753 // http://stackoverflow.com/a/2161748/1262753
var scripts = document.getElementsByTagName('script');
// var scripts = document.getElementsByTagName('script');
// var jsFile = scripts[scripts.length-1].src.split('?')[0]; // var jsFile = scripts[scripts.length-1].src.split('?')[0];
// var cssFile = jsFile.substring(0, jsFile.length - 2) + 'css'; // var cssFile = jsFile.substring(0, jsFile.length - 2) + 'css';
@ -2634,6 +2638,71 @@ Range.prototype.move = function(moveFactor) {
this.end = newEnd; this.end = newEnd;
}; };
/**
* An event bus can be used to emit events, and to subscribe to events
* @constructor EventBus
*/
function EventBus() {
this.subscriptions = [];
}
/**
* Subscribe to an event
* @param {String | RegExp} event The event can be a regular expression, or
* a string with wildcards, like 'server.*'.
* @param {function} callback. Callback are called with three parameters:
* {String} event, {Object} [data],
* {*} [source]
* @param {*} [target]
* @returns {String} id A subscription id
*/
EventBus.prototype.on = function (event, callback, target) {
var subscription = {
id: util.randomUUID(),
event: event instanceof RegExp ? event : new RegExp(event.replace('*', '\\w+')),
callback: (typeof callback === 'function') ? callback : null,
target: target
};
this.subscriptions.push(subscription);
return subscription.id;
};
/**
* Unsubscribe from an event
* @param {String} id subscription id
*/
EventBus.prototype.off = function (id) {
var i = 0;
while (i < this.subscriptions.length) {
var subscription = this.subscriptions[i];
if (subscription.id == id) {
this.subscriptions.splice(i, 1);
}
else {
i++;
}
}
};
/**
* Emit an event
* @param {String} event
* @param {Object} [data]
* @param {*} [source]
*/
EventBus.prototype.emit = function (event, data, source) {
for (var i =0; i < this.subscriptions.length; i++) {
var subscription = this.subscriptions[i];
if (subscription.event.test(event)) {
if (subscription.callback) {
subscription.callback(event, data, source);
}
}
}
};
/** /**
* @constructor Controller * @constructor Controller
* *
@ -5112,6 +5181,7 @@ var vis = {
Range: Range, Range: Range,
Stack: Stack, Stack: Stack,
TimeStep: TimeStep, TimeStep: TimeStep,
EventBus: EventBus,
components: { components: {
items: { items: {

+ 3
- 3
vis.min.js
File diff suppressed because it is too large
View File


Loading…
Cancel
Save