Bladeren bron

:recycle: further refactor notifications into server methods

tags/0.0.1^2
J 3 jaren geleden
bovenliggende
commit
a01a599d16

+ 25
- 7
backend/lib/plugins/notification.js Bestand weergeven

@@ -1,4 +1,4 @@
1
-const { allStreams } = require('../utils')
1
+const _allStreams = {}
2 2
 
3 3
 const NotificationRoute = require('../routes/notification')
4 4
 
@@ -16,7 +16,7 @@ const ENDER = { event: 'end', data: '' }
16 16
  * @param {Stream} event
17 17
  * @returns {string}
18 18
  */
19
-const stringifyEvent = function (event) {
19
+const _stringifyEvent = function (event) {
20 20
     let str = ''
21 21
     const endl = '\r\n'
22 22
     for (const i in event) {
@@ -57,11 +57,11 @@ class Transformer extends Transform {
57 57
         if (this.event) {
58 58
             event.event = this.event
59 59
         }
60
-        this.push(stringifyEvent(event))
60
+        this.push(_stringifyEvent(event))
61 61
         callback()
62 62
     }
63 63
     _flush(callback) {
64
-        this.push(stringifyEvent(ENDER))
64
+        this.push(_stringifyEvent(ENDER))
65 65
         callback()
66 66
     }
67 67
 }
@@ -74,7 +74,7 @@ class Transformer extends Transform {
74 74
  * @param {Toolkit} h hapi common response toolkit
75 75
  * @param {object} streamOptions
76 76
  */
77
-const onEvent = (event, h, streamOptions) => {
77
+const _event = (event, h, streamOptions) => {
78 78
     let active
79 79
     if (event instanceof Stream.Readable) {
80 80
         if (event._readableState.objectMode) {
@@ -90,12 +90,30 @@ const onEvent = (event, h, streamOptions) => {
90 90
     }
91 91
 }
92 92
 
93
+/**
94
+ * Takes an open HTTP stream and writes
95
+ * a msg to it, then fires notification plugin's
96
+ * _event callback
97
+ * @param {object} msg you want to send
98
+ * @param {string} name <profileId>.<eventType>
99
+ * @param {boolean} shouldInitialize
100
+ * @param {Toolkit} h hapi common response toolkit
101
+ */
102
+const onNotify = (name, msg, h, shouldInitialize = false) => {
103
+    if (shouldInitialize) {
104
+        _allStreams[name] = new PassThrough({ objectMode: true })
105
+    }
106
+    if (!_allStreams[name]) return
107
+    _allStreams[name].write(msg)
108
+
109
+    return _event(_allStreams[name], h, { event: name })
110
+}
111
+
93 112
 module.exports = {
94 113
     name: 'notification-plugin',
95 114
     version: '1.0.0',
96 115
     register: async server => {
97 116
         await server.route(NotificationRoute)
98
-        server.decorate('toolkit', 'event', onEvent)
99
-        server.expose('streams', allStreams)
117
+        server.method('notify', onNotify)
100 118
     },
101 119
 }

+ 2
- 4
backend/lib/routes/membership/active.js Bestand weergeven

@@ -6,8 +6,6 @@ const errorSchema = require('../../schemas/errors')
6 6
 const groupingSchema = require('../../schemas/groupings')
7 7
 const params = require('../../schemas/params')
8 8
 
9
-const { dispatchNotification } = require('../../utils')
10
-
11 9
 const pluginConfig = {
12 10
     handlerType: 'grouping',
13 11
     docs: {
@@ -83,14 +81,14 @@ module.exports = {
83 81
                 return g
84 82
             })
85 83
 
86
-            dispatchNotification(
84
+            request.server.methods.notify(
85
+                `${profileId}.stonk`,
87 86
                 {
88 87
                     name: 'MSHRM',
89 88
                     price: (500 + Math.floor(Math.random() * 100)).toString(),
90 89
                     order: null,
91 90
                     type: 'info',
92 91
                 },
93
-                `${profileId}.stonk`,
94 92
                 h,
95 93
             )
96 94
 

+ 12
- 3
backend/lib/routes/notification/index.js Bestand weergeven

@@ -3,8 +3,6 @@ const apiSchema = require('../../schemas/api')
3 3
 const errorSchema = require('../../schemas/errors')
4 4
 const params = require('../../schemas/params')
5 5
 
6
-const { intializeStream } = require('../../utils')
7
-
8 6
 const pluginConfig = {
9 7
     handlerType: 'notifictaion',
10 8
     docs: {
@@ -32,7 +30,18 @@ module.exports = {
32 30
              * Write the initial stream
33 31
              * !: this must remain open for notifications to work
34 32
              */
35
-            return intializeStream(`${profile_id}.stonk`, h)
33
+            return request.server.methods.notify(
34
+                `${profile_id}.stonk`,
35
+                {
36
+                    profile_id,
37
+                    name: 'BDGRS',
38
+                    price: (500 + Math.floor(Math.random() * 100)).toString(),
39
+                    order: Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL',
40
+                    type: 'info',
41
+                },
42
+                h,
43
+                true,
44
+            )
36 45
         },
37 46
 
38 47
         /** Validate based on validators object */

+ 0
- 43
backend/lib/utils.js Bestand weergeven

@@ -1,43 +0,0 @@
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
-}

Laden…
Annuleren
Opslaan