Skip to content

Commit

Permalink
simplify unsubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpenner committed Apr 28, 2017
1 parent a283571 commit 92a92e0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
12 changes: 5 additions & 7 deletions src/nanosignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export interface Listener<PAYLOAD, RESULT = void> {
/**
* Cancels a listener's subscription to its signal.
*/
export interface Subscription {
unsubscribe(): void;
export interface Unsubscriber {
(): void;
}

export interface Signal<PAYLOAD, RESULT = void> {
Expand All @@ -16,7 +16,7 @@ export interface Signal<PAYLOAD, RESULT = void> {
* Starts a function listening to this signal.
* @param listener
*/
subscribe(listener: Listener<PAYLOAD, RESULT>): Subscription;
subscribe(listener: Listener<PAYLOAD, RESULT>): Unsubscriber;
}

/**
Expand All @@ -30,10 +30,8 @@ export default function createSignal<PAYLOAD, RESULT = void>(firstListener?: Lis

signal.subscribe = (listener) => {
listeners = listeners.concat(listener);
return {
unsubscribe() {
listeners = listeners.filter(fn => fn !== listener);
}
return () => {
listeners = listeners.filter(fn => fn !== listener);
};
};

Expand Down
12 changes: 5 additions & 7 deletions src/nanosignal0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export interface Listener0<RESULT = void> {
/**
* Cancels a listener's subscription to its signal.
*/
export interface Subscription {
unsubscribe(): void;
export interface Unsubscriber {
(): void;
}

export interface Signal0<RESULT = void> {
Expand All @@ -16,7 +16,7 @@ export interface Signal0<RESULT = void> {
* Starts a function listening to this signal.
* @param listener
*/
subscribe(listener: Listener0<RESULT>): Subscription;
subscribe(listener: Listener0<RESULT>): Unsubscriber;
}

/**
Expand All @@ -30,10 +30,8 @@ export default function createSignal0<RESULT = void>(firstListener?: Listener0<R

signal.subscribe = (listener) => {
listeners = listeners.concat(listener);
return {
unsubscribe() {
listeners = listeners.filter(fn => fn !== listener);
}
return () => {
listeners = listeners.filter(fn => fn !== listener);
};
};

Expand Down
4 changes: 2 additions & 2 deletions test/nanosignal-self-removal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('when a listener unsubscribes another listener during dispatch', () =>
beforeEach(() => {
happened = createSignal0();
listenerA = jest.fn();
const subscriptionA = happened.subscribe(listenerA);
listenerB = jest.fn(subscriptionA.unsubscribe);
const unsubscribeA = happened.subscribe(listenerA);
listenerB = jest.fn(unsubscribeA);
listenerC = jest.fn();
happened.subscribe(listenerB);
happened.subscribe(listenerC);
Expand Down
14 changes: 7 additions & 7 deletions test/nanosignal-signal0.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createSignal0, { Signal0, Subscription } from '../src/nanosignal0';
import createSignal0, { Signal0, Unsubscriber } from '../src/nanosignal0';

describe('signal with 0 arguments', () => {
let happened: Signal0;
Expand All @@ -9,11 +9,11 @@ describe('signal with 0 arguments', () => {

describe('when removing a listener before firing', () => {
let listenerA: jest.Mock<any>;
let subscriptionA: Subscription;
let removeA: Unsubscriber;
beforeEach(() => {
listenerA = jest.fn();
subscriptionA = happened.subscribe(listenerA);
subscriptionA.unsubscribe();
removeA = happened.subscribe(listenerA);
removeA();
happened();
});

Expand All @@ -24,10 +24,10 @@ describe('signal with 0 arguments', () => {

describe('when adding the same listener twice', () => {
let listenerA: jest.Mock<any>;
let subscriptionA: Subscription;
let removeA: () => void;
beforeEach(() => {
listenerA = jest.fn();
subscriptionA = happened.subscribe(listenerA);
removeA = happened.subscribe(listenerA);
happened.subscribe(listenerA);
});

Expand All @@ -42,7 +42,7 @@ describe('signal with 0 arguments', () => {

describe('then removing the listener once and firing', () => {
beforeEach(() => {
subscriptionA.unsubscribe();
removeA();
happened();
});
it('the listener is not called', () => {
Expand Down

0 comments on commit 92a92e0

Please sign in to comment.