Skip to content

Commit

Permalink
v1.2.2, TS compatible trigger via array of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Ni55aN committed Apr 11, 2019
1 parent 6504479 commit 4b373be
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rete",
"version": "1.2.1",
"version": "1.2.2",
"description": "JavaScript framework",
"main": "build/rete.common.js",
"module": "build/rete.esm.js",
Expand Down
6 changes: 4 additions & 2 deletions src/core/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export class Emitter<EventTypes> {
this.events = events instanceof Emitter ? events.events : events.handlers;
}

on<K extends keyof EventTypes>(names: K, handler: (args: EventTypes[K]) => any) {
(names as string).split(' ').forEach(name => {
on<K extends keyof EventTypes>(names: K | K[], handler: (args: EventTypes[K]) => any) {
const events = names instanceof Array ? names : (names as string).split(' ');

(events as Array<string>).forEach(name => {
if (!this.events[name])
throw new Error(`The event ${name} does not exist`);
this.events[name].push(handler);
Expand Down
9 changes: 8 additions & 1 deletion test/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ describe('Editor', () => {

assert.doesNotThrow(() => editor.removeConnection(connection), Error, 'remove connection');
assert.equal((n1.outputs.get('name') as Output).connections.length, 0, 'no connections');

});

it('events', () => {
var editor = new Rete.NodeEditor('[email protected]', c);

assert.doesNotThrow(() => editor.trigger('nodecreate'), Error, 'nodecreate events exist');
assert.throws(() => editor.on('wrngevent' as any, () => {}), Error, 'throw exception on non-exist event');
assert.doesNotThrow(() => editor.on(['nodecreate'], () => {}), Error, 'on events array');
})
})

0 comments on commit 4b373be

Please sign in to comment.