Skip to content

Commit

Permalink
event names: use dashes instead of periods
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbCaudill committed Dec 12, 2023
1 parent 89758f4 commit 310908f
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 53 deletions.
12 changes: 6 additions & 6 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
Message,
PeerSocketMap,
UserName,
} from "./lib/types.js"
} from "./types.js"

const { version } = pkg
const HEARTBEAT = pack({ type: "Heartbeat" })
Expand All @@ -36,7 +36,7 @@ export interface PeerEventPayload {
* ```ts
* client = new Client({ userName: 'my-peer-userName', url })
* .join('my-document-userName')
* .on('peer.connect', ({documentId, userName, socket}) => {
* .on('peer-connect', ({documentId, userName, socket}) => {
* // send a message
* socket.send('Hello!')
*
Expand Down Expand Up @@ -125,7 +125,7 @@ export class Client extends EventEmitter<ClientEvents> {
this.retryDelay = this.minRetryDelay
this.shouldReconnectIfClosed = true
this.drainQueue()
this.emit("server.connect")
this.emit("server-connect")
this.open = true

this.heartbeat = setInterval(
Expand Down Expand Up @@ -159,7 +159,7 @@ export class Client extends EventEmitter<ClientEvents> {

// add the socket to the map for this peer
peer.set(documentId, peerConnection)
this.emit("peer.connect", {
this.emit("peer-connect", {
userName,
documentId,
socket: peerConnection,
Expand All @@ -169,7 +169,7 @@ export class Client extends EventEmitter<ClientEvents> {
// if the other end disconnects, we disconnect
peerConnection.onclose = () => {
this.closeSocket(userName, documentId)
this.emit("peer.disconnect", {
this.emit("peer-disconnect", {
userName,
documentId,
socket: peerConnection,
Expand All @@ -183,7 +183,7 @@ export class Client extends EventEmitter<ClientEvents> {

.on("close", () => {
this.open = false
this.emit("server.disconnect")
this.emit("server-disconnect")

// stop heartbeat
clearInterval(this.heartbeat)
Expand Down
10 changes: 2 additions & 8 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ import {
ConnectRequestParams,
DocumentId,
Message,
ServerEvents,
UserName,
} from "./lib/types.js"

type ServerEvents = {
ready: () => void
close: () => void
error: (payload: { error: Error; data: Uint8Array }) => void
introduction: (userName: UserName) => void
}
} from "./types.js"

/**
* This server provides two services:
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Server } from "./Server.js"
export { Client } from "./Client.js"

export { Message, DocumentId, UserName } from "./lib/types.js"
export { Message, DocumentId, UserName } from "./types.js"
2 changes: 1 addition & 1 deletion src/lib/deduplicate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DocumentId } from "./types.js"
import type { DocumentId } from "../types.js"

export const deduplicate = (acc: DocumentId[], documentId: string) =>
acc.includes(documentId) ? acc : acc.concat(documentId)
2 changes: 1 addition & 1 deletion src/lib/intersection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DocumentId } from "./types.js"
import type { DocumentId } from "../types.js"

export const intersection = (a: DocumentId[] = [], b: DocumentId[] = []) =>
a.filter(documentId => b.includes(documentId))
35 changes: 4 additions & 31 deletions src/test/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, it } from "vitest"
import { Client } from "../Client.js"
import { eventPromise } from "../lib/eventPromise.js"
import { DocumentId, Server } from "../index.js"
import { PeerEventPayload } from "../lib/types.js"
import { PeerEventPayload } from "../types.js"
import { pack, unpack } from "../lib/msgpack.js"
import { WebSocket } from "isomorphic-ws"

Expand Down Expand Up @@ -60,33 +60,6 @@ it("both peers have the second document", async () => {
await teardown()
})

// it("emits peer.connect only once per peer connection", async () => {
// // Alice and Bob both join a documentId
// const documentId = `some-other-test-documentId-${testId}`

// const { url, teardown } = await setup()
// let connections = 0
// const alice = new Client({ userName: `alice-${testId}`, url })

// alice.on("peer.connect", () => {
// connections++
// if (connections > 1) throw new Error(`peer connect emitted ${connections}x`)
// })
// const bob = new Client({ userName: `bob-${testId}`, url })

// bob.join(documentId)
// alice.join(documentId)
// alice.join(documentId)

// await allConnected(alice, bob)

// expect(alice.has(bob.userName, documentId)).toBe(true)
// expect(bob.has(alice.userName, documentId)).toBe(true)

// await pause(500)
// await teardown()
// })

it("leaves a documentId", async () => {
// Alice and Bob both join a documentId
const { alice, bob, documentId, teardown } = await setup()
Expand Down Expand Up @@ -204,7 +177,7 @@ it("closes when server disconnects", async () => {
expect(bob.open).toBe(true)

teardown()
await eventPromise(alice, "server.disconnect")
await eventPromise(alice, "server-disconnect")
expect(alice.open).toBe(false)
})

Expand All @@ -216,7 +189,7 @@ const allDisconnected = (a: Client, b: Client) =>

const connection = (a: Client, b: Client, documentId?: DocumentId) =>
new Promise<WebSocket>(resolve =>
a.on("peer.connect", ({ userName, documentId: d, socket }) => {
a.on("peer-connect", ({ userName, documentId: d, socket }) => {
if (
userName === b.userName &&
// are we waiting to connect on a specific document ID?
Expand All @@ -228,7 +201,7 @@ const connection = (a: Client, b: Client, documentId?: DocumentId) =>

const disconnection = (a: Client, b: Client) =>
new Promise<void>(resolve =>
a.on("peer.disconnect", ({ userName = "" }) => {
a.on("peer-disconnect", ({ userName = "" }) => {
if (userName === b.userName) resolve()
})
)
Expand Down
2 changes: 1 addition & 1 deletion src/test/Server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { eventPromise } from "../lib/eventPromise.js"
import { isReady } from "../lib/isReady.js"
import { pack, unpack } from "../lib/msgpack.js"
import { pause } from "../lib/pause.js"
import type { Message } from "../lib/types.js"
import type { Message } from "../types.js"

// const log = debug('lf:relay:tests')

Expand Down
15 changes: 11 additions & 4 deletions src/lib/types.ts → src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export namespace Message {
}

export type ClientEvents = {
"server.connect": () => void
"server.disconnect": () => void
"server-connect": () => void
"server-disconnect": () => void
error: (err: Error) => void
"peer.connect": (payload: PeerEventPayload) => void
"peer.disconnect": (payload: PeerEventPayload) => void
"peer-connect": (payload: PeerEventPayload) => void
"peer-disconnect": (payload: PeerEventPayload) => void
}
export interface PeerEventPayload {
documentId: DocumentId
Expand All @@ -65,4 +65,11 @@ export interface ClientOptions {
backoffFactor?: number
}

export type ServerEvents = {
ready: () => void
close: () => void
error: (payload: { error: Error; data: Uint8Array }) => void
introduction: (userName: UserName) => void
}

export type PeerSocketMap = Map<DocumentId, WebSocket | null>

0 comments on commit 310908f

Please sign in to comment.