@dnax/core 0.15.1 → 0.15.2

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.
@@ -1,11 +1,15 @@
1
1
  import type { ServerWebSocket, WebSocketHandler, Server } from "bun";
2
2
  import { EventEmitter } from "events";
3
+ import { Cfg } from "../../config";
3
4
  import { v4 } from "uuid";
4
5
  const wsClients = new Map<string, ServerWebSocket>();
5
6
  const wsEvents: optionsIo[] = [];
6
7
 
7
8
  class Io {
8
- constructor() {}
9
+ id: string | null;
10
+ constructor() {
11
+ this.id = null;
12
+ }
9
13
  on(event: string, cb: Function) {
10
14
  wsEvents.push({
11
15
  type: "on",
@@ -13,16 +17,31 @@ class Io {
13
17
  cb: cb,
14
18
  });
15
19
  }
20
+ to(id: string) {
21
+ const newInstance = new Io();
22
+ newInstance.id = id;
23
+
24
+ return newInstance;
25
+ }
16
26
  emit(event: string, data: any) {
17
- wsEvents.push({
18
- type: "emit",
19
- event: event,
20
- data: data,
27
+ if (!this?.id || this.id == null) {
28
+ console.error("No id found");
29
+ }
30
+
31
+ if (this.id) {
32
+ let client = wsClients.get(this.id);
33
+ client?.send(JSON.stringify({ type: "emit", event, data }));
34
+ }
35
+ }
36
+ broadcast(event: string, data: any) {
37
+ wsClients.forEach((client) => {
38
+ client.send(JSON.stringify({ type: "emit", event, data }));
21
39
  });
22
40
  }
23
41
  }
24
42
 
25
43
  const io = new Io();
44
+ Cfg.io = io;
26
45
  type optionsIo = {
27
46
  type: "on" | "emit";
28
47
  event: string | "connection" | "close";
@@ -41,7 +60,7 @@ export type socketIoType = {
41
60
  emit: (event: string, data: any) => void;
42
61
  }) => void
43
62
  ) => void;
44
- emit: (event: string, data: any) => void;
63
+ emit: (event: string, id: string, data: any) => void;
45
64
  broadcast: (event: string, data: any) => void;
46
65
  };
47
66
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.15.1",
3
+ "version": "0.15.2",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/types/index.ts CHANGED
@@ -190,7 +190,7 @@ export type hooksCtx = (ctx: {
190
190
  c?: Context;
191
191
  rest: InstanceType<typeof useRest>;
192
192
  session?: sessionCtx;
193
- io: Io;
193
+ io: socketIoType;
194
194
  }) => any;
195
195
 
196
196
  export type ctxApi = {
@@ -438,7 +438,7 @@ export type routeOption = {
438
438
  */
439
439
  base: string; // base URL
440
440
  handler: (ctx: {
441
- io: Io;
441
+ io: socketIoType;
442
442
  router: {
443
443
  post: (path: string, clb: (c: Context) => void) => {};
444
444
  get: (path: string, clb: (c: Context) => void) => {};
@@ -450,7 +450,7 @@ export type endpointCtx = {
450
450
  enabled: boolean;
451
451
  handler: (ctx: {
452
452
  rest: InstanceType<typeof useRest>;
453
- io: Io;
453
+ io: socketIoType;
454
454
  router: {
455
455
  post: (path: string, clb: (c: Context) => void) => {};
456
456
  get: (path: string, clb: (c: Context) => void) => {};
@@ -462,7 +462,7 @@ export type routeCtx = {
462
462
  enabled: boolean;
463
463
  handler: (ctx: {
464
464
  rest: InstanceType<typeof useRest>;
465
- io: Io;
465
+ io: socketIoType;
466
466
  router: {
467
467
  post: (path: string, clb: (c: Context) => void) => {};
468
468
  get: (path: string, clb: (c: Context) => void) => {};
@@ -505,7 +505,7 @@ export type Service = {
505
505
  * authenticated user or no
506
506
  */
507
507
  isAuth: boolean;
508
- io: Io;
508
+ io: socketIoType;
509
509
  }) => any;
510
510
  exec?: (ctx: {
511
511
  /**
@@ -525,7 +525,7 @@ export type Service = {
525
525
  * authenticated user or no
526
526
  */
527
527
  isAuth: boolean;
528
- io: Io;
528
+ io: socketIoType;
529
529
  }) => any;
530
530
  };
531
531