Ver código fonte

:recycle: refactor notification dispatching to a util

tags/0.0.1^2
J 3 anos atrás
pai
commit
4fa4e87157

+ 2
- 2
backend/lib/plugins/notification.js Ver arquivo

@@ -1,3 +1,5 @@
1
+const { allStreams } = require('../utils')
2
+
1 3
 const NotificationRoute = require('../routes/notification')
2 4
 
3 5
 /** Heavily lifted from: https://github.com/mtharrison/susie/blob/master/lib/index.js */
@@ -8,8 +10,6 @@ const Transform = Stream.Transform
8 10
 
9 11
 const ENDER = { event: 'end', data: '' }
10 12
 
11
-const allStreams = {}
12
-
13 13
 /**
14 14
  * Stringify a stream
15 15
  * ?: I don't really get what this is doing

+ 11
- 17
backend/lib/routes/membership/active.js Ver arquivo

@@ -6,7 +6,7 @@ const errorSchema = require('../../schemas/errors')
6 6
 const groupingSchema = require('../../schemas/groupings')
7 7
 const params = require('../../schemas/params')
8 8
 
9
-const PassThrough = require('stream').PassThrough
9
+const { dispatchNotification } = require('../../utils')
10 10
 
11 11
 const pluginConfig = {
12 12
     handlerType: 'grouping',
@@ -36,19 +36,6 @@ const responseSchemas = {
36 36
     error: errorSchema.single,
37 37
 }
38 38
 
39
-const dispatch = (streamName, streams, h) => {
40
-    const stream = streams[streamName]
41
-    if (!stream || !streams) return
42
-    const msg = {
43
-        name: 'MSHRM',
44
-        price: (500 + Math.floor(Math.random() * 100)).toString(),
45
-        order: null,
46
-        type: 'info',
47
-    }
48
-    stream.write(msg)
49
-    h.event(stream, h, { event: streamName })
50
-}
51
-
52 39
 module.exports = {
53 40
     method: 'GET',
54 41
     path: '/{profile_id}',
@@ -96,9 +83,16 @@ module.exports = {
96 83
                 return g
97 84
             })
98 85
 
99
-            const allStreams =
100
-                request.server.plugins['notification-plugin']['streams']
101
-            dispatch(`${profileId}.stonk`, allStreams, h)
86
+            dispatchNotification(
87
+                {
88
+                    name: 'MSHRM',
89
+                    price: (500 + Math.floor(Math.random() * 100)).toString(),
90
+                    order: null,
91
+                    type: 'info',
92
+                },
93
+                `${profileId}.stonk`,
94
+                h,
95
+            )
102 96
 
103 97
             try {
104 98
                 return {

+ 6
- 21
backend/lib/routes/notification/index.js Ver arquivo

@@ -3,7 +3,7 @@ const apiSchema = require('../../schemas/api')
3 3
 const errorSchema = require('../../schemas/errors')
4 4
 const params = require('../../schemas/params')
5 5
 
6
-const PassThrough = require('stream').PassThrough
6
+const { intializeStream } = require('../../utils')
7 7
 
8 8
 const pluginConfig = {
9 9
     handlerType: 'notifictaion',
@@ -28,26 +28,11 @@ module.exports = {
28 28
         handler: async (request, h) => {
29 29
             const { profile_id } = request.params
30 30
 
31
-            const streamName = `${profile_id}.stonk`
32
-            const allStreams =
33
-                request.server.plugins['notification-plugin']['streams']
34
-            allStreams[streamName] = new PassThrough({ objectMode: true })
35
-
36
-            const msg = {
37
-                profile_id,
38
-                name: 'BDGRS',
39
-                price: (500 + Math.floor(Math.random() * 100)).toString(),
40
-                order: null,
41
-                type: 'info',
42
-            }
43
-
44
-            // Write to the input stream
45
-            msg.order = Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL'
46
-            allStreams[streamName].write(msg)
47
-
48
-            // h.event() Added at plugin registration
49
-            // h is the toolkit
50
-            return h.event(allStreams[streamName], h, { event: streamName })
31
+            /**
32
+             * Write the initial stream
33
+             * !: this must remain open for notifications to work
34
+             */
35
+            return intializeStream(`${profile_id}.stonk`, h)
51 36
         },
52 37
 
53 38
         /** Validate based on validators object */

+ 43
- 0
backend/lib/utils.js Ver arquivo

@@ -0,0 +1,43 @@
1
+const PassThrough = require('stream').PassThrough
2
+
3
+const allStreams = {}
4
+
5
+const activeStream = name => allStreams[name]
6
+
7
+/**
8
+ * Takes an open HTTP stream and writes
9
+ * a msg to it, then fires notification plugin's
10
+ * onEvent callback
11
+ * @param {object} msg you want to send
12
+ * @param {string} name <profileId>.<eventType>
13
+ * @param {object} req server request object
14
+ * @param {Toolkit} h hapi common response toolkit
15
+ */
16
+const dispatchNotification = (msg, name, h) => {
17
+    const stream = activeStream(name)
18
+    if (!stream) return
19
+    stream.write(msg)
20
+    return h.event(stream, h, { event: name })
21
+}
22
+
23
+const intializeStream = (name, h) => {
24
+    allStreams[name] = new PassThrough({ objectMode: true })
25
+
26
+    return dispatchNotification(
27
+        {
28
+            profile_id: name,
29
+            name: 'BDGRS',
30
+            price: (500 + Math.floor(Math.random() * 100)).toString(),
31
+            order: Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL',
32
+            type: 'info',
33
+        },
34
+        name,
35
+        h,
36
+    )
37
+}
38
+
39
+module.exports = {
40
+    dispatchNotification,
41
+    allStreams,
42
+    intializeStream,
43
+}

Carregando…
Cancelar
Salvar