@toa.io/extensions.realtime 1.0.0-alpha.201 → 1.0.0-alpha.202

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.
@@ -22,26 +22,32 @@ export class Effect implements Operation {
22
22
  public async execute (input: Input): Promise<Readable> {
23
23
  const key = input.key
24
24
 
25
- if (!this.streams.has(key))
26
- this.createStream(key)
25
+ let stream: Stream | undefined
27
26
 
28
- const stream = this.streams.get(key)!
27
+ if (!this.streams.has(key)) {
28
+ stream = this.createStream(key)
29
+
30
+ this.logs.debug('Stream created', { key })
31
+ } else
32
+ stream = this.streams.get(key)!
33
+
34
+ // welcome
35
+ setTimeout(() => stream?.heartbeat(), 1000)
29
36
 
30
- // return stream.fork()
31
37
  return stream
32
38
  }
33
39
 
34
- private createStream (key: string): void {
35
- const stream = new Stream(this.logs.fork({ key }))
40
+ private createStream (key: string): Stream {
41
+ const stream = new Stream()
36
42
 
37
43
  this.streams.set(key, stream)
38
44
 
39
- stream.once('close', () => {
40
- this.logs.debug('Stream closed', { key })
45
+ stream.events.once('destroy', () => {
46
+ this.logs.debug('Stream destroyed', { key })
41
47
  this.streams.delete(key)
42
48
  })
43
49
 
44
- this.logs.debug('Stream created', { key })
50
+ return stream
45
51
  }
46
52
  }
47
53
 
@@ -1,26 +1,12 @@
1
- import { PassThrough, Readable } from 'node:stream'
1
+ import { EventEmitter, Readable } from 'node:stream'
2
2
 
3
3
  export class Stream extends Readable {
4
- private forks: number = 0
4
+ public events = new EventEmitter()
5
+
5
6
  private interval: NodeJS.Timeout | null = null
6
- private readonly logs: any
7
7
 
8
- public constructor (logs: any) {
8
+ public constructor () {
9
9
  super(objectMode)
10
-
11
- this.logs = logs
12
- }
13
-
14
- public fork (): PassThrough {
15
- const through = new PassThrough(objectMode)
16
-
17
- through.once('close', this.decrement.bind(this))
18
-
19
- this.increment()
20
- // this.heartbeat(through)
21
- this.pipe(through)
22
-
23
- return through
24
10
  }
25
11
 
26
12
  // has to be here
@@ -33,12 +19,12 @@ export class Stream extends Readable {
33
19
  if (this.interval !== null)
34
20
  clearInterval(this.interval)
35
21
 
36
- this.logs.debug('Stream destroyed', { forks: this.forks })
22
+ this.events.emit('destroy')
37
23
 
38
24
  super._destroy(error, callback)
39
25
  }
40
26
 
41
- private heartbeat (stream: Readable = this): boolean {
27
+ public heartbeat (stream: Readable = this): boolean {
42
28
  const resume = stream.push('heartbeat ' + Date.now())
43
29
 
44
30
  if (!resume && this.interval !== null) {
@@ -48,21 +34,6 @@ export class Stream extends Readable {
48
34
 
49
35
  return resume
50
36
  }
51
-
52
- private increment (): void {
53
- this.forks++
54
-
55
- this.logs.debug('Stream forked', { forks: this.forks })
56
- }
57
-
58
- private decrement (): void {
59
- this.forks--
60
-
61
- this.logs.debug('Stream fork closed', { forks: this.forks })
62
-
63
- if (this.forks === 0)
64
- this.destroy()
65
- }
66
37
  }
67
38
 
68
39
  const HEARTBEAT_INTERVAL = 16_000 // why?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/extensions.realtime",
3
- "version": "1.0.0-alpha.201",
3
+ "version": "1.0.0-alpha.202",
4
4
  "description": "Toa Realtime",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -28,5 +28,5 @@
28
28
  "transpile": "npx tsc && npx tsc -p ./components/streams",
29
29
  "features": "npx cucumber-js"
30
30
  },
31
- "gitHead": "e4955f36b0f2519516bc4be7c71e55a0e7cfdd44"
31
+ "gitHead": "4d230bb899fee0cd49b099db09749a01d01cda7e"
32
32
  }