Skip to content

Commit

Permalink
feat: add pusher/ws
Browse files Browse the repository at this point in the history
  • Loading branch information
zSoulweaver committed Jul 24, 2024
1 parent 67fa520 commit d2ed258
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 4 deletions.
Binary file modified bun.lockb
Binary file not shown.
15 changes: 15 additions & 0 deletions example/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Kient from 'Kient'
import { Events } from '../src/events'

// const kient = new Kient({
// connectToWebsocket: false,
// pusher: {
// appKey: '32cbd69e4b950bf97679',
// },
// })

const kient = new Kient()

kient.on(Events.Core.WebSocketConnected, async () => {
console.log('connected to ws')
})
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "src/index.ts",
"type": "module",
"scripts": {
"dev": "bun run --watch example/get-channel.ts"
"dev": "bun run --watch example/init.ts"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
Expand All @@ -16,6 +16,9 @@
},
"dependencies": {
"@deepkit/type": "^1.0.1-alpha.153",
"ofetch": "^1.3.4"
"defu": "^6.1.4",
"ofetch": "^1.3.4",
"pusher-js": "^8.4.0-rc2",
"tseep": "^1.2.2"
}
}
Empty file removed src/api-client.ts
Empty file.
15 changes: 15 additions & 0 deletions src/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const KientEvents = {
WebSocketConnected: 'KIENT_WEBSOCKET_CONNECTED',
WebSocketDisconnected: 'KIENT_WEBSOCKET_DISCONNECTED',
} as const

type CoreEvents = {
[KientEvents.WebSocketConnected]: () => void
[KientEvents.WebSocketDisconnected]: () => void
}

export const Events = {
Core: KientEvents,
}

export type KientEventEmitters = CoreEvents
45 changes: 44 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
import defu from 'defu'
import { getChannel } from './api/channels/get-channel'
import { WSClient } from './ws.client'
import { EventEmitter } from 'tseep'
import type { KientEventEmitters } from './events'

type DeepRequired<T> = {
[P in keyof T]-?: DeepRequired<NonNullable<T[P]>>
}

export interface KientOptions {
connectToWebsocket?: boolean
pusher?: {
appKey?: string
cluster?: string
}
}

const defaultKientOptions: DeepRequired<KientOptions> = {
connectToWebsocket: true,
pusher: {
appKey: '32cbd69e4b950bf97679',
cluster: 'us2',
},
}

export default class Kient extends EventEmitter<KientEventEmitters> {
private readonly kientOptions!: DeepRequired<KientOptions>
private wsClient?: WSClient

constructor(options?: KientOptions) {
super()
this.kientOptions = defu(options, defaultKientOptions)

this.kientOptions.connectToWebsocket ? this.connectWebsocket() : null
}

connectWebsocket() {
this.wsClient = new WSClient(this, {
pusher: {
appKey: this.kientOptions.pusher.appKey,
cluster: this.kientOptions.pusher.cluster,
},
})
}

export default class Kient {
api = {
getChannel: (slugOrId: string) => getChannel(this, slugOrId),
}
Expand Down
49 changes: 49 additions & 0 deletions src/ws.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type Kient from 'Kient'
import Pusher, { type Channel, type Options } from 'pusher-js'
import { Events } from './events'

interface WSClientOptions {
pusher: {
appKey: string
cluster: string
}
}

export class WSClient {
private readonly _kientInstance!: Kient
private readonly pusher!: Pusher

constructor(kientInstance: Kient, options: WSClientOptions) {
this._kientInstance = kientInstance

const pusherOptions: Options = {
cluster: options.pusher.cluster,
}
this.pusher = new Pusher(options.pusher.appKey, pusherOptions)
this.pusher.connection.bind('connected', () =>
this._kientInstance.emit(Events.Core.WebSocketConnected),
)
this.pusher.connection.bind('disconnected', () =>
this._kientInstance.emit(Events.Core.WebSocketDisconnected),
)
}

async subscribe(channel: string) {
return new Promise<Channel>((resolve, _) => {
const subscribedChannel = this.pusher.subscribe(channel)
subscribedChannel.bind(
'pusher:subscription_error',
(error: { type: string; error: string; status: number }) => {
throw new Error(`Unable to subscribe to channel, error: ${error}`)
},
)
subscribedChannel.bind('pusher:subscription_succeeded', () => {
resolve(subscribedChannel)
})
})
}

async unsubscribe(channel: string) {
await this.pusher.unsubscribe(channel)
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"Kient": [
"./src/index.ts"
]
}
},
"outDir": "dist"
},
"reflection": true
}

0 comments on commit d2ed258

Please sign in to comment.