Skip to content

Commit

Permalink
feat: expose HttpContext in events
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
RomainLanz committed Mar 4, 2024
1 parent b19a871 commit 6e86dd2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
16 changes: 8 additions & 8 deletions providers/transmit_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export default class TransmitProvider {
const router = await this.app.container.make('router')
const transmit = await this.app.container.make('transmit')

router.get('__transmit/events', ({ request, response }) => {
transmit.$createStream(request, response)
router.get('__transmit/events', (ctx) => {
transmit.$createStream(ctx)
})

router.post('__transmit/subscribe', (ctx) => {
Expand All @@ -57,17 +57,17 @@ export default class TransmitProvider {
return ctx.response.noContent()
})

router.post('__transmit/unsubscribe', ({ request, response }) => {
const uid = request.input('uid')
const channel = request.input('channel')
router.post('__transmit/unsubscribe', (ctx) => {
const uid = ctx.request.input('uid')
const channel = ctx.request.input('channel')

const success = transmit.$unsubscribeFromChannel(uid, channel)
const success = transmit.$unsubscribeFromChannel(uid, channel, ctx)

if (!success) {
return response.badRequest()
return ctx.response.badRequest()
}

return response.noContent()
return ctx.response.noContent()
})
}
}
24 changes: 13 additions & 11 deletions src/transmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import Emittery from 'emittery'
import { Stream } from './stream.js'
import { StorageBag } from './storage_bag.js'
import { SecureChannelStore } from './secure_channel_store.js'
import type { HttpContext, Request, Response } from '@adonisjs/core/http'
import type { HttpContext } from '@adonisjs/core/http'
import type { TransmitConfig, Transport } from './types/main.js'

interface TransmitLifecycleHooks {
connect: { uid: string }
disconnect: { uid: string }
connect: { uid: string; ctx: HttpContext }
disconnect: { uid: string; ctx: HttpContext }
broadcast: { channel: string; payload: Record<string, unknown> }
subscribe: { uid: string; channel: string }
unsubscribe: { uid: string; channel: string }
subscribe: { uid: string; channel: string; ctx: HttpContext }
unsubscribe: { uid: string; channel: string; ctx: HttpContext }
}

export class Transmit {
Expand Down Expand Up @@ -73,16 +73,18 @@ export class Transmit {
/**
* Creates and register a new stream for the given request and pipes it to the response.
*/
$createStream(request: Request, response: Response): void {
$createStream(ctx: HttpContext): void {
const { request, response } = ctx

const stream = new Stream(request.input('uid'), request.request)
stream.pipe(response.response, undefined, response.getHeaders())

void this.#emittery.emit('connect', { uid: stream.getUid() })
void this.#emittery.emit('connect', { uid: stream.getUid(), ctx })

this.#storage.push(stream)

response.response.on('close', () => {
void this.#emittery.emit('disconnect', { uid: stream.getUid() })
void this.#emittery.emit('disconnect', { uid: stream.getUid(), ctx })
this.#storage.remove(stream)
})

Expand Down Expand Up @@ -132,13 +134,13 @@ export class Transmit {
}
}

void this.#emittery.emit('subscribe', { uid, channel })
void this.#emittery.emit('subscribe', { uid, channel, ctx })

return this.#storage.addChannelToStream(uid, channel)
}

$unsubscribeFromChannel(uid: string, channel: string): boolean {
void this.#emittery.emit('unsubscribe', { uid, channel })
$unsubscribeFromChannel(uid: string, channel: string, ctx: HttpContext): boolean {
void this.#emittery.emit('unsubscribe', { uid, channel, ctx })

return this.#storage.removeChannelFromStream(uid, channel)
}
Expand Down

0 comments on commit 6e86dd2

Please sign in to comment.