Skip to content

Commit

Permalink
add "event" for any event. fixes chjj#171.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Sep 1, 2015
1 parent 7936469 commit e4900bb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
12 changes: 12 additions & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ EventEmitter.prototype._emit = function(type, args) {
var handler = this._events[type]
, ret;

// if (type !== 'event') {
// this._emit('event', [type.replace(/^element /, '')].concat(args));
// }

if (!handler) {
if (type === 'error') {
throw new args[0];
Expand All @@ -101,8 +105,11 @@ EventEmitter.prototype._emit = function(type, args) {

EventEmitter.prototype.emit = function(type) {
var args = slice.call(arguments, 1)
, params = slice.call(arguments)
, el = this;

this._emit('event', params);

if (this.type === 'screen') {
return this._emit(type, args);
}
Expand All @@ -113,8 +120,13 @@ EventEmitter.prototype.emit = function(type) {

type = 'element ' + type;
args.unshift(this);
// `element` prefix
// params = [type].concat(args);
// no `element` prefix
// params.splice(1, 0, this);

do {
// el._emit('event', params);
if (!el._events[type]) continue;
if (el._emit(type, args) === false) {
return false;
Expand Down
3 changes: 0 additions & 3 deletions lib/widgets/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,6 @@ Screen.prototype.destroy = function() {
};

Screen.prototype.log = function() {
if (this.debugLog) {
this.debugLog.log.apply(this.debugLog, arguments);
}
return this.program.log.apply(this.program, arguments);
};

Expand Down
5 changes: 5 additions & 0 deletions test/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ screen.debugLog.on('show', function() {
screen.render();
});

screen.on('event', function(event, el) {
var type = (el && el.type) || Object.prototype.toString.call(el).slice(8, -1);
screen.program.log('emit("%s", {%s})', event, type);
});

screen.append(blessed.text({
top: 0,
left: 2,
Expand Down

0 comments on commit e4900bb

Please sign in to comment.