@toa.io/extensions.realtime 0.0.0 → 0.22.0

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,14 +1,31 @@
1
1
  import { type Readable } from 'node:stream'
2
+ import { type Operation } from '@toa.io/types'
2
3
  import { type Context } from './types'
3
- import { addStream } from './lib/streams'
4
+ import { Stream } from './lib/stream'
4
5
 
5
- export async function effect (input: Input, context: Context): Promise<Readable> {
6
- const key = input.key
6
+ export class Effect implements Operation {
7
+ private readonly streams = new Map<string, Stream>()
7
8
 
8
- if (!(key in context.state))
9
- addStream(key, context.state)
9
+ public mount (context: Context): void {
10
+ context.state.streams = this.streams
11
+ }
10
12
 
11
- return context.state[key].fork()
13
+ public async execute (input: Input, context: Context): Promise<Readable> {
14
+ const key = input.key
15
+
16
+ if (!this.streams.has(key))
17
+ this.createStream(key)
18
+
19
+ return (this.streams.get(key) as Stream).fork()
20
+ }
21
+
22
+ private createStream (key: string): void {
23
+ const stream = new Stream()
24
+
25
+ this.streams.set(key, stream)
26
+
27
+ stream.once('close', () => this.streams.delete(key))
28
+ }
12
29
  }
13
30
 
14
31
  interface Input {
@@ -1,13 +1,5 @@
1
1
  import { PassThrough, Readable } from 'node:stream'
2
2
 
3
- export function addStream (key: string, map: Record<string, Stream>): void {
4
- const stream = new Stream()
5
-
6
- map[key] = stream
7
-
8
- stream.once('close', () => delete map[key])
9
- }
10
-
11
3
  export class Stream extends Readable {
12
4
  private forks: number = 0
13
5
 
@@ -1,5 +1,5 @@
1
1
  import { type Context, type PushInput } from './types'
2
2
 
3
3
  export async function effect ({ key, event, data }: PushInput, context: Context): Promise<void> {
4
- context.state[key]?.push({ event, data })
4
+ context.state.streams.get(key)?.push({ event, data })
5
5
  }
@@ -1,7 +1,9 @@
1
- import { type Stream } from './lib/streams'
1
+ import { type Stream } from './lib/stream'
2
2
 
3
3
  export interface Context {
4
- state: Record<string, Stream>
4
+ state: {
5
+ streams: Map<string, Stream>
6
+ }
5
7
  }
6
8
 
7
9
  export interface PushInput {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/extensions.realtime",
3
- "version": "0.0.0",
3
+ "version": "0.22.0",
4
4
  "description": "Toa Realtime",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -24,8 +24,8 @@
24
24
  "testEnvironment": "node"
25
25
  },
26
26
  "scripts": {
27
- "transpile": "npx tsc && npx tsc -p ./components/streams",
27
+ "transpile": "rm -rf transpiled && npx tsc && rm -rf ./components/streams/operations && npx tsc -p ./components/streams",
28
28
  "features": "npx cucumber-js"
29
29
  },
30
- "gitHead": "28fc4b45c224c3683acaaf0e4abd1eb04e07b408"
30
+ "gitHead": "c463348c8eb54a43f7755ec8aeba9acafb56d53b"
31
31
  }