-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use base event emitter for Message API (#782)
* Use base event emitter for Message API * emitter transform remove and changeset include * ApplyEventListeners comment update
- Loading branch information
Showing
7 changed files
with
67 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@signalwire/realtime-api': patch | ||
--- | ||
|
||
Base event emitter for Messaging API |
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 was deleted.
Oops, something went wrong.
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 @@ | ||
export * from './messagingWorker' |
53 changes: 53 additions & 0 deletions
53
packages/realtime-api/src/messaging/workers/messagingWorker.ts
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,53 @@ | ||
import { | ||
MessagingAction, | ||
SDKActions, | ||
SDKWorker, | ||
SagaIterator, | ||
getLogger, | ||
sagaEffects, | ||
} from '@signalwire/core' | ||
import type { Client } from '../../client/index' | ||
import { Message } from '../Messaging' | ||
|
||
export const messagingWorker: SDKWorker<Client> = function* ( | ||
options | ||
): SagaIterator { | ||
getLogger().trace('messagingWorker started') | ||
const { channels, instance: client } = options | ||
const { swEventChannel } = channels | ||
|
||
function* worker(action: MessagingAction) { | ||
const { payload, type } = action | ||
|
||
// @ts-expect-error | ||
const message = new Message(payload) | ||
|
||
switch (type) { | ||
case 'messaging.receive': | ||
// @ts-expect-error | ||
client.baseEmitter.emit('message.received', message) | ||
break | ||
case 'messaging.state': | ||
// @ts-expect-error | ||
client.baseEmitter.emit('message.updated', message) | ||
break | ||
default: | ||
getLogger().warn(`Unknown message event: "${action.type}"`) | ||
break | ||
} | ||
} | ||
|
||
const isMessagingEvent = (action: SDKActions) => | ||
action.type.startsWith('messaging.') | ||
|
||
while (true) { | ||
const action: MessagingAction = yield sagaEffects.take( | ||
swEventChannel, | ||
isMessagingEvent | ||
) | ||
|
||
yield sagaEffects.fork(worker, action) | ||
} | ||
|
||
getLogger().trace('messagingWorker ended') | ||
} |
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