@polytric/openws-sdkgen 0.0.16 → 0.0.18

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.
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "files": ["dist"],
25
25
  "dependencies": {
26
- "@polytric/openws": "^0.0.9"
26
+ "@polytric/openws": "^0.0.10"
27
27
  },
28
28
  "scripts": {
29
29
  "build": "tsup",
@@ -57,11 +57,11 @@ export type <%= ctx.className %>ErrorHandler = (error: unknown) => void | Promis
57
57
  /**
58
58
  * Application callback for connection lifecycle events.
59
59
  */
60
- export type <%= ctx.className %>LifecycleHandler = (roleName: string) => void | Promise<void>
60
+ export type <%= ctx.className %>LifecycleHandler = (roleName: string, peer: <%= ctx.className %>Peer) => void | Promise<void>
61
61
  /**
62
62
  * Application callback for connection lifecycle errors.
63
63
  */
64
- export type <%= ctx.className %>LifecycleErrorHandler = (roleName: string, error: Error) => void | Promise<void>
64
+ export type <%= ctx.className %>LifecycleErrorHandler = (roleName: string, peer: <%= ctx.className %>Peer, error: Error) => void | Promise<void>
65
65
 
66
66
  type <%= ctx.className %>Connection = {
67
67
  roleName: string
@@ -132,14 +132,14 @@ export class <%= ctx.className %> {
132
132
  <% } -%>
133
133
  <% } -%>
134
134
  <% for (const remoteRole of ctx.remoteRoles) { -%>
135
- this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onOpen(async fromRole => {
136
- await this.handleOpen(fromRole)
135
+ this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onOpen(async (fromRole, peer) => {
136
+ await this.handleOpen(fromRole, peer as unknown as <%= ctx.className %>Peer)
137
137
  })
138
- this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onClose(async fromRole => {
139
- await this.handleClose(fromRole)
138
+ this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onClose(async (fromRole, peer) => {
139
+ await this.handleClose(fromRole, peer as unknown as <%= ctx.className %>Peer)
140
140
  })
141
- this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onError(async (fromRole, error) => {
142
- await this.handleError(fromRole, error)
141
+ this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onError(async (fromRole, peer, error) => {
142
+ await this.handleError(fromRole, peer as unknown as <%= ctx.className %>Peer, error)
143
143
  })
144
144
  <% } -%>
145
145
  if (canBindTransport(transport)) {
@@ -159,13 +159,21 @@ export class <%= ctx.className %> {
159
159
  case <%- JSON.stringify(remoteRole.roleName) %>: {
160
160
  const remoteEndpoint = endpoint ?? (<%- remoteRole.endpoints.length > 0 ? JSON.stringify(remoteRole.endpoints[0]) : 'undefined' %> as OpenWsEndpoint | undefined)
161
161
  await this.transport.connect?.(roleName, remoteEndpoint)
162
- const session = this.runtime.newSession(this.sendEnvelope)
162
+ let connection: <%= ctx.className %>Connection | undefined
163
+ const session = this.runtime.newSession(this.sendEnvelope, async () => {
164
+ if (!connection) {
165
+ await session.close()
166
+ return
167
+ }
168
+ await this.closeConnection(connection)
169
+ })
163
170
  const <%= remoteRole.varName %>Peer = await session.open(<%- JSON.stringify(remoteRole.roleName) %>)
164
- this.connections.add({
171
+ connection = {
165
172
  roleName: <%- JSON.stringify(remoteRole.roleName) %>,
166
173
  session,
167
174
  peer: <%= remoteRole.varName %>Peer,
168
- })
175
+ }
176
+ this.connections.add(connection)
169
177
  this.<%= remoteRole.peerVarName %> = <%= remoteRole.varName %>Peer as unknown as <%= remoteRole.scopedPeerName %>
170
178
  <% for (const handler of ctx.handlers) { -%>
171
179
  <% const handlerDefaultPeer = handler.bindFromRoles.find(fromRole => ctx.remoteRoles.some(remoteRole => remoteRole.roleName === fromRole.roleName)) ?? ctx.remoteRoles[0] -%>
@@ -198,11 +206,11 @@ export class <%= ctx.className %> {
198
206
  await this.closeSessions(peer)
199
207
  return
200
208
  }
201
- const connection = this.findConnectionByPeer(peer as PeerProto)
209
+ const connection = this.findConnectionByPeer(peer as unknown as PeerProto)
202
210
  if (!connection) {
203
211
  throw new Error('Peer is not connected')
204
212
  }
205
- await this.closeConnection(connection)
213
+ await this.runtime.disconnect(connection.peer)
206
214
  }
207
215
 
208
216
  /**
@@ -279,9 +287,9 @@ export class <%= ctx.className %> {
279
287
  /**
280
288
  * Framework entrypoint for a peer session opening.
281
289
  */
282
- async handleOpen(roleName: string): Promise<void> {
290
+ async handleOpen(roleName: string, peer: <%= ctx.className %>Peer): Promise<void> {
283
291
  for (const handler of this.openHandlers) {
284
- await handler(roleName)
292
+ await handler(roleName, peer)
285
293
  }
286
294
  }
287
295
 
@@ -298,9 +306,9 @@ export class <%= ctx.className %> {
298
306
  /**
299
307
  * Framework entrypoint for a peer session closing.
300
308
  */
301
- async handleClose(roleName: string): Promise<void> {
309
+ async handleClose(roleName: string, peer: <%= ctx.className %>Peer): Promise<void> {
302
310
  for (const handler of this.closeHandlers) {
303
- await handler(roleName)
311
+ await handler(roleName, peer)
304
312
  }
305
313
  }
306
314
 
@@ -317,9 +325,9 @@ export class <%= ctx.className %> {
317
325
  /**
318
326
  * Framework entrypoint for a peer session error.
319
327
  */
320
- async handleError(roleName: string, error: Error): Promise<void> {
328
+ async handleError(roleName: string, peer: <%= ctx.className %>Peer, error: Error): Promise<void> {
321
329
  for (const handler of this.lifecycleErrorHandlers) {
322
- await handler(roleName, error)
330
+ await handler(roleName, peer, error)
323
331
  }
324
332
  }
325
333
 
@@ -494,14 +502,14 @@ export class <%= ctx.className %> {
494
502
  <% } -%>
495
503
  <% } -%>
496
504
  <% for (const remoteRole of ctx.remoteRoles) { -%>
497
- this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onOpen(async fromRole => {
498
- await this.handleOpen(fromRole)
505
+ this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onOpen(async (fromRole, peer) => {
506
+ await this.handleOpen(fromRole, peer)
499
507
  })
500
- this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onClose(async fromRole => {
501
- await this.handleClose(fromRole)
508
+ this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onClose(async (fromRole, peer) => {
509
+ await this.handleClose(fromRole, peer)
502
510
  })
503
- this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onError(async (fromRole, error) => {
504
- await this.handleError(fromRole, error)
511
+ this.binder.fromRoles[<%- JSON.stringify(remoteRole.roleName) %>].onError(async (fromRole, peer, error) => {
512
+ await this.handleError(fromRole, peer, error)
505
513
  })
506
514
  <% } -%>
507
515
  if (canBindTransport(transport)) {
@@ -518,13 +526,21 @@ export class <%= ctx.className %> {
518
526
  case <%- JSON.stringify(remoteRole.roleName) %>: {
519
527
  const remoteEndpoint = endpoint ?? <%- remoteRole.endpoints.length > 0 ? JSON.stringify(remoteRole.endpoints[0]) : 'undefined' %>
520
528
  await this.transport.connect?.(roleName, remoteEndpoint)
521
- const session = this.runtime.newSession(this.sendEnvelope)
529
+ let connection
530
+ const session = this.runtime.newSession(this.sendEnvelope, async () => {
531
+ if (!connection) {
532
+ await session.close()
533
+ return
534
+ }
535
+ await this.#closeConnection(connection)
536
+ })
522
537
  this.<%= remoteRole.peerVarName %> = await session.open(<%- JSON.stringify(remoteRole.roleName) %>)
523
- this.#connections.add({
538
+ connection = {
524
539
  roleName: <%- JSON.stringify(remoteRole.roleName) %>,
525
540
  session,
526
541
  peer: this.<%= remoteRole.peerVarName %>,
527
- })
542
+ }
543
+ this.#connections.add(connection)
528
544
  <% for (const handler of ctx.handlers) { -%>
529
545
  <% const handlerDefaultPeer = handler.bindFromRoles.find(fromRole => ctx.remoteRoles.some(remoteRole => remoteRole.roleName === fromRole.roleName)) ?? ctx.remoteRoles[0] -%>
530
546
  <% if (handlerDefaultPeer?.roleName === remoteRole.roleName) { -%>
@@ -556,7 +572,7 @@ export class <%= ctx.className %> {
556
572
  if (!connection) {
557
573
  throw new Error('Peer is not connected')
558
574
  }
559
- await this.#closeConnection(connection)
575
+ await this.runtime.disconnect(connection.peer)
560
576
  }
561
577
 
562
578
  /**
@@ -633,9 +649,9 @@ export class <%= ctx.className %> {
633
649
  /**
634
650
  * Framework entrypoint for a peer session opening.
635
651
  */
636
- async handleOpen(roleName) {
652
+ async handleOpen(roleName, peer) {
637
653
  for (const handler of this.#openHandlers) {
638
- await handler(roleName)
654
+ await handler(roleName, peer)
639
655
  }
640
656
  }
641
657
 
@@ -652,9 +668,9 @@ export class <%= ctx.className %> {
652
668
  /**
653
669
  * Framework entrypoint for a peer session closing.
654
670
  */
655
- async handleClose(roleName) {
671
+ async handleClose(roleName, peer) {
656
672
  for (const handler of this.#closeHandlers) {
657
- await handler(roleName)
673
+ await handler(roleName, peer)
658
674
  }
659
675
  }
660
676
 
@@ -671,9 +687,9 @@ export class <%= ctx.className %> {
671
687
  /**
672
688
  * Framework entrypoint for a peer session error.
673
689
  */
674
- async handleError(roleName, error) {
690
+ async handleError(roleName, peer, error) {
675
691
  for (const handler of this.#lifecycleErrorHandlers) {
676
- await handler(roleName, error)
692
+ await handler(roleName, peer, error)
677
693
  }
678
694
  }
679
695
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polytric/openws-sdkgen",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "OpenWS SDK generator CLI",
5
5
  "type": "module",
6
6
  "bin": {
@@ -50,7 +50,7 @@
50
50
  "tsx": "^4.21.0",
51
51
  "typescript": "^5.9.3",
52
52
  "ws": "^8.18.3",
53
- "@polytric/openws": "^0.0.9"
53
+ "@polytric/openws": "^0.0.11"
54
54
  },
55
55
  "scripts": {
56
56
  "build": "tsup",