-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Server notifs): Add server sent notifications
- Loading branch information
Showing
11 changed files
with
140 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import { Client } from '../base/Client' | ||
import { JsonRpcRequest } from '../base/JsonRpcRequest' | ||
import { Server } from '../base/Server' | ||
import { DirectAddress } from '../base/Transports' | ||
import { DirectServer } from './DirectServer' | ||
|
||
export class DirectClient extends Client { | ||
private server: Server | ||
private server: DirectServer | ||
|
||
public constructor(address: Omit<DirectAddress, 'type'>) { | ||
super() | ||
this.server = address.server | ||
this.server.client = this | ||
} | ||
|
||
protected send(request: JsonRpcRequest): void { | ||
// @ts-ignore server.receive is private | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
this.server.receive(request).then(response => { | ||
if (response !== undefined) this.receive(response) | ||
}) | ||
this.server | ||
// @ts-ignore server.receive is private | ||
.receive(request) | ||
.then(response => { | ||
if (response !== undefined) this.receive(response) | ||
}) | ||
.catch(error => { | ||
throw error | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
import { Server } from '../base/Server' | ||
import { DirectAddress, Transport } from '../base/Transports' | ||
import { Client } from '../base/Client' | ||
import { InternalError } from '../base/InternalError' | ||
import { JsonRpcRequest } from '../base/JsonRpcRequest' | ||
|
||
export class DirectServer extends Server { | ||
client?: Client | ||
|
||
public get address(): DirectAddress { | ||
return { | ||
type: Transport.direct, | ||
server: this | ||
} | ||
} | ||
|
||
public notify(subject: string, message: string): void { | ||
if (this.client === undefined) | ||
throw new InternalError('No client connected!') | ||
const notification = new JsonRpcRequest(subject, { message }, false) | ||
// @ts-ignore client.receive is private | ||
this.client.receive(notification) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const delay = async (milliseconds: number) => | ||
new Promise(resolve => setTimeout(resolve, milliseconds)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters