Skip to content

Commit

Permalink
Merge pull request #26 from vitaly-t/to-consumer
Browse files Browse the repository at this point in the history
adding method toConsumer
  • Loading branch information
vitaly-t committed Mar 12, 2020
2 parents 6d99b2b + 2ff3778 commit 5bc121f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Subscription} from './sub';
import {EventConsumer} from './consumer';

/**
* Schedule for emitting / broadcasting data to subscribers, to be used by method [[emit]].
Expand Down Expand Up @@ -246,6 +247,15 @@ export class SubEvent<T = unknown> {
this.options = options ?? {};
}

/**
* Returns a new [[EventConsumer]] for the event, which removes methods [[emit]] and [[cancelAll]].
*
* This method simplifies creation of a receive-only event object.
*/
public toConsumer<E extends SubEvent<T>>(): EventConsumer<T, E> {
return new EventConsumer(this);
}

/**
* Subscribes to the event.
*
Expand Down
13 changes: 12 additions & 1 deletion test/event.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {chai, dummy, expect} from './';
import {EmitSchedule, ISubContext, SubEvent} from '../src';
import {EmitSchedule, EventConsumer, ISubContext, SubEvent} from '../src';

const errInvalidOptions = `Invalid "options" parameter.`;

Expand Down Expand Up @@ -443,3 +443,14 @@ describe('once', () => {
expect(contextOut).to.equal(contextIn);
});
});

describe('toConsumer', () => {
it('must return new EventConsumer', () => {
const e = new SubEvent<number>();
const c = e.toConsumer();
expect(c).to.be.instanceOf(EventConsumer);
expect(typeof c.subscribe).to.equal('function');
expect((c as any).emit).to.be.undefined;
expect((c as any).cancelAll).to.be.undefined;
});
});

0 comments on commit 5bc121f

Please sign in to comment.