Skip to content

Commit

Permalink
feat(core/EventBus): add createEvent API
Browse files Browse the repository at this point in the history
Don't expose internal Event representation; instead provide
`EventBus#createEvent(payload)` API to create an internal event, where
needed.

BREAKING CHANGE:

* EventBus.Event constructor got removed. Create event using
`EventBus#createEvent`.
  • Loading branch information
nikku committed Apr 2, 2018
1 parent c6bbac7 commit 91899cf
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 42 deletions.
7 changes: 2 additions & 5 deletions lib/command/CommandStack.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict';

var uniqueBy = require('min-dash').uniqueBy,
isArray = require('min-dash').isArray,
assign = require('min-dash').assign;

var InternalEvent = require('../core/EventBus').Event;
isArray = require('min-dash').isArray;


/**
Expand Down Expand Up @@ -345,7 +342,7 @@ CommandStack.prototype._fire = function(command, qualifier, event) {
var names = qualifier ? [ command + '.' + qualifier, qualifier ] : [ command ],
i, name, result;

event = assign(new InternalEvent(), event);
event = this._eventBus.createEvent(event);

for (i = 0; (name = names[i]); i++) {
result = this._eventBus.fire('commandStack.' + name, event);
Expand Down
31 changes: 22 additions & 9 deletions lib/core/EventBus.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,22 @@ EventBus.prototype.off = function(event, callback) {
};


/**
* Create an EventBus event.
*
* @param {Object} data
*
* @return {Object} event, recognized by the eventBus
*/
EventBus.prototype.createEvent = function(data) {
var event = new InternalEvent();

event.init(data);

return event;
};


/**
* Fires a named event.
*
Expand Down Expand Up @@ -279,12 +295,11 @@ EventBus.prototype.fire = function(type, data) {

// we make sure we fire instances of our home made
// events here. We wrap them only once, though
if (data instanceof Event) {
if (data instanceof InternalEvent) {
// we are fine, we alread have an event
event = data;
} else {
event = new Event();
event.init(data);
event = this.createEvent(data);
}

// ensure we pass the event as the first parameter
Expand Down Expand Up @@ -426,19 +441,17 @@ EventBus.prototype._getListeners = function(name) {
/**
* A event that is emitted via the event bus.
*/
function Event() { }

module.exports.Event = Event;
function InternalEvent() { }

Event.prototype.stopPropagation = function() {
InternalEvent.prototype.stopPropagation = function() {
this.cancelBubble = true;
};

Event.prototype.preventDefault = function() {
InternalEvent.prototype.preventDefault = function() {
this.defaultPrevented = true;
};

Event.prototype.init = function(data) {
InternalEvent.prototype.init = function(data) {
assign(this, data || {});
};

Expand Down
11 changes: 8 additions & 3 deletions lib/features/dragging/Dragging.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ var domEvent = require('min-dom').event,
Cursor = require('../../util/Cursor'),
ClickTrap = require('../../util/ClickTrap');

var EventBusEvent = require('../../core/EventBus').Event;

var DRAG_ACTIVE_CLS = 'djs-drag-active';


Expand Down Expand Up @@ -150,7 +148,14 @@ function Dragging(eventBus, canvas, selection) {
function fire(type, dragContext) {
dragContext = dragContext || context;

var event = assign(new EventBusEvent(), dragContext.payload, dragContext.data, { isTouch: dragContext.isTouch });
var event = eventBus.createEvent(
assign(
{},
dragContext.payload,
dragContext.data,
{ isTouch: dragContext.isTouch }
)
);

// default integration
if (eventBus.fire('drag.' + type, event) === false) {
Expand Down
3 changes: 1 addition & 2 deletions test/spec/core/EventBusSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var EventBus = require('lib/core/EventBus');

var EventBusEvent = EventBus.Event;

/* global sinon */

Expand Down Expand Up @@ -83,7 +82,7 @@ describe('core/EventBus', function() {
it('should be undefined on event if no listeners', function() {

// given
var event = new EventBusEvent();
var event = eventBus.createEvent();

event.init({ type: 'foo' });

Expand Down
22 changes: 13 additions & 9 deletions test/spec/snapping/SnappingSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var canvasEvent = require('../../util/MockEvents').createCanvasEvent;

var SnapContext = require('lib/features/snapping/SnapContext');

var Event = require('lib/core/EventBus').Event;
var TestHelper = require('test/TestHelper');


describe('features/snapping - Snapping', function() {
Expand Down Expand Up @@ -98,28 +98,32 @@ describe('features/snapping - Snapping', function() {

var startEvent;

beforeEach(function() {
startEvent = assign(new Event(), {
beforeEach(inject(function(eventBus) {
startEvent = eventBus.createEvent({
x: 150,
y: 150,
context: {
shape: shape,
target: rootElement
}
});
});
}));


function moveTo(startEvent, newPosition) {

return assign(new Event(), startEvent, {
x: newPosition.x,
y: newPosition.y,
dx: newPosition.x - startEvent.x,
dy: newPosition.y - startEvent.y
return TestHelper.getDiagramJS().invoke(function(eventBus) {

return eventBus.createEvent(assign(startEvent, {
x: newPosition.x,
y: newPosition.y,
dx: newPosition.x - startEvent.x,
dy: newPosition.y - startEvent.y
}));
});
}


it('should init on shape.move.start', inject(function(eventBus) {

// when
Expand Down
24 changes: 10 additions & 14 deletions test/util/MockEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

var assign = require('min-dash').assign;

var EventBus = require('../../lib/core/EventBus');

var TestHelper = require('../helper');


Expand Down Expand Up @@ -39,19 +37,17 @@ module.exports.createCanvasEvent = createCanvasEvent;

function createEvent(target, position, data) {

data = assign({
target: target,
clientX: position.x,
clientY: position.y,
offsetX: position.x,
offsetY: position.y
}, data || {});

var event = new EventBus.Event();
return TestHelper.getDiagramJS().invoke(function(eventBus) {
data = assign({
target: target,
clientX: position.x,
clientY: position.y,
offsetX: position.x,
offsetY: position.y
}, data || {});

event.init(data);

return event;
return eventBus.createEvent(data);
});
}

module.exports.createEvent = createEvent;

0 comments on commit 91899cf

Please sign in to comment.