Skip to content

Commit

Permalink
feat: allow to customize the ping system
Browse files Browse the repository at this point in the history
Closes #4
  • Loading branch information
RomainLanz committed Mar 4, 2024
1 parent 187c02e commit 6ae2584
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/transmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import Emittery from 'emittery'
import string from '@poppinss/utils/string'
import { Stream } from './stream.js'
import { StorageBag } from './storage_bag.js'
import { SecureChannelStore } from './secure_channel_store.js'
Expand Down Expand Up @@ -69,7 +70,14 @@ export class Transmit {
void this.#broadcastLocally(channel, payload)
})

setInterval(() => this.#ping(), 1000 * 60)
if (this.#config.pingInterval) {
const intervalValue =
typeof this.#config.pingInterval === 'number'
? this.#config.pingInterval
: string.milliseconds.parse(this.#config.pingInterval)

setInterval(() => this.#ping(), intervalValue)
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/types/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
* file that was distributed with this source code.
*/

/**
* A Duration can be a number in milliseconds or a string formatted as a duration
*
* Formats accepted are :
* - Simple number in milliseconds
* - String formatted as a duration. Uses https://github.com/lukeed/ms under the hood
*/
export type Duration = number | string

export interface Transport {
send(channel: string, payload: any): Promise<void>
subscribe(channel: string, handler: any): Promise<void>
unsubscribe(channel: string): Promise<void>
}

export interface TransmitConfig {
pingInterval?: Duration | false
transport: null | { driver: new (...args: any[]) => Transport; channel?: string }
}
1 change: 1 addition & 0 deletions stubs/config/transmit.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
import { defineConfig } from '@adonisjs/transmit'

export default defineConfig({
pingInterval: false,
transport: null
})

0 comments on commit 6ae2584

Please sign in to comment.