starpc 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,10 @@ import type { TsProtoRpc } from './ts-proto-rpc.js';
3
3
  import type { OpenStreamFunc } from './stream.js';
4
4
  export declare class Client implements TsProtoRpc {
5
5
  private openConnFn;
6
- constructor(openConnFn: OpenStreamFunc);
7
- setOpenConnFn(openConnFn: OpenStreamFunc): void;
6
+ private _openConnFn?;
7
+ constructor(openConnFn?: OpenStreamFunc);
8
+ setOpenConnFn(openConnFn?: OpenStreamFunc): Promise<OpenStreamFunc>;
9
+ private initOpenConnFn;
8
10
  request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
9
11
  clientStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Promise<Uint8Array>;
10
12
  serverStreamingRequest(service: string, method: string, data: Uint8Array): Observable<Uint8Array>;
@@ -19,15 +19,45 @@ function writeClientStream(call, data) {
19
19
  }
20
20
  // Client implements the ts-proto Rpc interface with the drpcproto protocol.
21
21
  export class Client {
22
- // openConnFn is the open connection function.
23
- // called when starting RPC.
22
+ // openConnFn is a promise which contains the OpenStreamFunc.
24
23
  openConnFn;
24
+ // _openConnFn resolves openConnFn.
25
+ _openConnFn;
25
26
  constructor(openConnFn) {
26
- this.openConnFn = openConnFn;
27
+ this.openConnFn = this.setOpenConnFn(openConnFn);
27
28
  }
28
29
  // setOpenConnFn updates the openConnFn for the Client.
29
30
  setOpenConnFn(openConnFn) {
30
- this.openConnFn = openConnFn;
31
+ if (this._openConnFn) {
32
+ if (openConnFn) {
33
+ this._openConnFn(openConnFn);
34
+ this._openConnFn = undefined;
35
+ }
36
+ }
37
+ else {
38
+ if (openConnFn) {
39
+ this.openConnFn = Promise.resolve(openConnFn);
40
+ }
41
+ else {
42
+ this.initOpenConnFn();
43
+ }
44
+ }
45
+ return this.openConnFn;
46
+ }
47
+ // initOpenConnFn creates the empty Promise for openConnFn.
48
+ initOpenConnFn() {
49
+ const openPromise = new Promise((resolve, reject) => {
50
+ this._openConnFn = (conn, err) => {
51
+ if (err) {
52
+ reject(err);
53
+ }
54
+ else if (conn) {
55
+ resolve(conn);
56
+ }
57
+ };
58
+ });
59
+ this.openConnFn = openPromise;
60
+ return this.openConnFn;
31
61
  }
32
62
  // request starts a non-streaming request.
33
63
  async request(service, method, data) {
@@ -105,7 +135,8 @@ export class Client {
105
135
  // throws any error starting the rpc call
106
136
  // if data == null and data.length == 0, sends a separate data packet.
107
137
  async startRpc(rpcService, rpcMethod, data) {
108
- const conn = await this.openConnFn();
138
+ const openConnFn = await this.openConnFn;
139
+ const conn = await openConnFn();
109
140
  const call = new ClientRPC(rpcService, rpcMethod);
110
141
  pipe(conn, parseLengthPrefixTransform(), decodePacketSource, call, encodePacketSource, prependLengthPrefixTransform(), conn);
111
142
  await call.writeCallStart(data || undefined);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
package/srpc/client.ts CHANGED
@@ -29,17 +29,45 @@ function writeClientStream(call: ClientRPC, data: Observable<Uint8Array>) {
29
29
 
30
30
  // Client implements the ts-proto Rpc interface with the drpcproto protocol.
31
31
  export class Client implements TsProtoRpc {
32
- // openConnFn is the open connection function.
33
- // called when starting RPC.
34
- private openConnFn: OpenStreamFunc
32
+ // openConnFn is a promise which contains the OpenStreamFunc.
33
+ private openConnFn: Promise<OpenStreamFunc>
34
+ // _openConnFn resolves openConnFn.
35
+ private _openConnFn?: (conn?: OpenStreamFunc, err?: Error) => void
35
36
 
36
- constructor(openConnFn: OpenStreamFunc) {
37
- this.openConnFn = openConnFn
37
+ constructor(openConnFn?: OpenStreamFunc) {
38
+ this.openConnFn = this.setOpenConnFn(openConnFn)
38
39
  }
39
40
 
40
41
  // setOpenConnFn updates the openConnFn for the Client.
41
- public setOpenConnFn(openConnFn: OpenStreamFunc) {
42
- this.openConnFn = openConnFn
42
+ public setOpenConnFn(openConnFn?: OpenStreamFunc): Promise<OpenStreamFunc> {
43
+ if (this._openConnFn) {
44
+ if (openConnFn) {
45
+ this._openConnFn(openConnFn)
46
+ this._openConnFn = undefined
47
+ }
48
+ } else {
49
+ if (openConnFn) {
50
+ this.openConnFn = Promise.resolve(openConnFn)
51
+ } else {
52
+ this.initOpenConnFn()
53
+ }
54
+ }
55
+ return this.openConnFn
56
+ }
57
+
58
+ // initOpenConnFn creates the empty Promise for openConnFn.
59
+ private initOpenConnFn(): Promise<OpenStreamFunc> {
60
+ const openPromise = new Promise<OpenStreamFunc>((resolve, reject) => {
61
+ this._openConnFn = (conn?: OpenStreamFunc, err?: Error) => {
62
+ if (err) {
63
+ reject(err)
64
+ } else if (conn) {
65
+ resolve(conn)
66
+ }
67
+ }
68
+ })
69
+ this.openConnFn = openPromise
70
+ return this.openConnFn
43
71
  }
44
72
 
45
73
  // request starts a non-streaming request.
@@ -140,7 +168,8 @@ export class Client implements TsProtoRpc {
140
168
  rpcMethod: string,
141
169
  data: Uint8Array | null
142
170
  ): Promise<ClientRPC> {
143
- const conn = await this.openConnFn()
171
+ const openConnFn = await this.openConnFn
172
+ const conn = await openConnFn()
144
173
  const call = new ClientRPC(rpcService, rpcMethod)
145
174
  pipe(
146
175
  conn,