Skip to content

Commit

Permalink
Merge branch 'jribbink/sdk-subscriptions' into jribbink/blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Dec 2, 2024
2 parents a9fd1c5 + 49cecf0 commit dead25f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions packages/transport-http/src/subscribe/handlers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ export interface SubscriptionHandler<
): DataSubscriber<T["Args"], T["ArgsDto"], T["DataDto"]>
}

export interface DataSubscriber<Args, ArgsModel, Data> {
export interface DataSubscriber<Args, ArgsDto, DataDto> {
/**
* The callback to call when a data is received
*/
sendData(data: Data): void
onData(data: DataDto): void

/**
* The callback to call when an error is received
*/
sendError(error: Error): void
onError(error: Error): void

/**
* The arguments to connect or reconnect to the subscription
*/
encodeArgs(args: Args): ArgsModel
argsToDto(args: Args): ArgsDto

/**
* Get the arguments to connect or reconnect to the subscription
Expand Down
22 changes: 11 additions & 11 deletions packages/transport-http/src/subscribe/subscription-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ describe("SubscriptionManager", () => {

mockWs = new WS("wss://localhost:8080")
mockSubscriber = {
sendData: jest.fn(),
sendError: jest.fn(),
encodeArgs: jest.fn().mockReturnValue(mockConnectionArgs),
onData: jest.fn(),
onError: jest.fn(),
argsToDto: jest.fn().mockReturnValue(mockConnectionArgs),
get connectionArgs() {
return mockConnectionArgs
},
Expand Down Expand Up @@ -156,9 +156,9 @@ describe("SubscriptionManager", () => {

await serverPromise

expect(mockSubscriber.sendData).toHaveBeenCalledTimes(1)
expect(mockSubscriber.sendData).toHaveBeenCalledWith({key: "value"})
expect(mockSubscriber.sendError).toHaveBeenCalledTimes(0)
expect(mockSubscriber.onData).toHaveBeenCalledTimes(1)
expect(mockSubscriber.onData).toHaveBeenCalledWith({key: "value"})
expect(mockSubscriber.onError).toHaveBeenCalledTimes(0)

serverPromise = (async () => {
const msg = (await mockWs.nextMessage) as string
Expand Down Expand Up @@ -232,9 +232,9 @@ describe("SubscriptionManager", () => {

await serverPromise

expect(mockSubscriber.sendData).toHaveBeenCalledTimes(1)
expect(mockSubscriber.sendData).toHaveBeenCalledWith({key: "value"})
expect(mockSubscriber.sendError).toHaveBeenCalledTimes(0)
expect(mockSubscriber.onData).toHaveBeenCalledTimes(1)
expect(mockSubscriber.onData).toHaveBeenCalledWith({key: "value"})
expect(mockSubscriber.onError).toHaveBeenCalledTimes(0)

// Close the connection and create a new one
mockWs.close()
Expand Down Expand Up @@ -275,7 +275,7 @@ describe("SubscriptionManager", () => {

await serverPromise

expect(mockSubscriber.sendData).toHaveBeenCalledTimes(2)
expect(mockSubscriber.sendData.mock.calls[1]).toEqual([{key: "value2"}])
expect(mockSubscriber.onData).toHaveBeenCalledTimes(2)
expect(mockSubscriber.onData.mock.calls[1]).toEqual([{key: "value2"}])
})
})
4 changes: 2 additions & 2 deletions packages/transport-http/src/subscribe/subscription-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class SubscriptionManager<
})

this.subscriptions.forEach(sub => {
sub.subscriber.sendError(
sub.subscriber.onError(
new Error(
`Failed to reconnect to the server after ${this.reconnectAttempts + 1} attempts: ${error}`
)
Expand Down Expand Up @@ -318,7 +318,7 @@ export class SubscriptionManager<
}

// Send data to the subscriber
sub.subscriber.sendData(message.data)
sub.subscriber.onData(message.data)
}

private getHandler(topic: string) {
Expand Down

0 comments on commit dead25f

Please sign in to comment.