-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #858 from TokenScript/feature/multi-hook
added multi-hook functionality to token negotiator
- Loading branch information
Showing
6 changed files
with
93 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// @ts-nocheck | ||
import { EventHookHandler } from '../eventHookHandler' | ||
|
||
const mockCallback = jest.fn(); | ||
|
||
describe('client spec', () => { | ||
test('eventHookHandler can subscribe and trigger events', async () => { | ||
const eventHookHandler = new EventHookHandler() | ||
eventHookHandler.subscribe('selected-tokens', mockCallback); | ||
eventHookHandler.trigger('selected-tokens', { data: "It's not a bug; it's an undocumented feature" }); | ||
expect(mockCallback).toHaveBeenCalled(); | ||
}) | ||
test('eventHookHandler can unsubscribe', async () => { | ||
const eventHookHandler = new EventHookHandler() | ||
const unsubscribe = eventHookHandler.subscribe('selected-tokens', mockCallback); | ||
unsubscribe(); | ||
eventHookHandler.trigger('selected-tokens', { data: "Make it work, make it right, make it fast." }); | ||
expect(mockCallback).toHaveLength(0); | ||
}) | ||
test('eventHookHandler can unsubscribe all subscriptions of event type', async () => { | ||
const eventHookHandler = new EventHookHandler() | ||
eventHookHandler.subscribe('selected-tokens', mockCallback); | ||
eventHookHandler.unsubscribe('selected-tokens'); | ||
eventHookHandler.trigger('selected-tokens', { data: "Make it work, make it right, make it fast." }); | ||
expect(mockCallback).toHaveLength(0); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export class EventHookHandler { | ||
subIds: number | ||
subscriptions: { [eventName: string]: { [token: number]: (data: any) => void } } | ||
|
||
constructor() { | ||
this.subIds = 0 | ||
this.subscriptions = {} | ||
} | ||
|
||
subscribe(eventName: string, fn: (data: any) => void) { | ||
if (!this.subscriptions[eventName]) this.subscriptions[eventName] = {} | ||
const token = ++this.subIds | ||
this.subscriptions[eventName][token] = fn | ||
return () => this.unsubscribe(eventName, token) | ||
} | ||
|
||
unsubscribe(eventName: string, token: number) { | ||
if (!token) delete this.subscriptions[eventName] | ||
this.subscriptions[eventName] && delete this.subscriptions[eventName][token] | ||
} | ||
|
||
trigger(eventName: string, data: any) { | ||
this.publish(eventName, data) | ||
return data | ||
} | ||
|
||
publish(eventName, data) { | ||
const subs = this.subscriptions[eventName] | ||
if (!subs) { | ||
return false | ||
} | ||
Object.values(subs).forEach((sub) => sub(data)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters