@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.
- package/components/streams/operations/create.js +13 -8
- package/components/streams/operations/create.js.map +1 -1
- package/components/streams/operations/lib/Stream.d.ts +5 -8
- package/components/streams/operations/lib/Stream.js +3 -23
- package/components/streams/operations/lib/Stream.js.map +1 -1
- package/components/streams/operations/tsconfig.tsbuildinfo +1 -1
- package/components/streams/source/create.ts +15 -9
- package/components/streams/source/lib/Stream.ts +6 -35
- package/package.json +2 -2
- package/transpiled/tsconfig.tsbuildinfo +1 -1
|
@@ -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
|
-
|
|
26
|
-
this.createStream(key)
|
|
25
|
+
let stream: Stream | undefined
|
|
27
26
|
|
|
28
|
-
|
|
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):
|
|
35
|
-
const stream = new Stream(
|
|
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('
|
|
40
|
-
this.logs.debug('Stream
|
|
45
|
+
stream.events.once('destroy', () => {
|
|
46
|
+
this.logs.debug('Stream destroyed', { key })
|
|
41
47
|
this.streams.delete(key)
|
|
42
48
|
})
|
|
43
49
|
|
|
44
|
-
|
|
50
|
+
return stream
|
|
45
51
|
}
|
|
46
52
|
}
|
|
47
53
|
|
|
@@ -1,26 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EventEmitter, Readable } from 'node:stream'
|
|
2
2
|
|
|
3
3
|
export class Stream extends Readable {
|
|
4
|
-
|
|
4
|
+
public events = new EventEmitter()
|
|
5
|
+
|
|
5
6
|
private interval: NodeJS.Timeout | null = null
|
|
6
|
-
private readonly logs: any
|
|
7
7
|
|
|
8
|
-
public constructor (
|
|
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.
|
|
22
|
+
this.events.emit('destroy')
|
|
37
23
|
|
|
38
24
|
super._destroy(error, callback)
|
|
39
25
|
}
|
|
40
26
|
|
|
41
|
-
|
|
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.
|
|
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": "
|
|
31
|
+
"gitHead": "4d230bb899fee0cd49b099db09749a01d01cda7e"
|
|
32
32
|
}
|