Переглянути джерело

:recycle: further refactor notifications into server methods

tags/0.0.1^2
J 3 роки тому
джерело
коміт
a01a599d16

+ 25
- 7
backend/lib/plugins/notification.js Переглянути файл

1
-const { allStreams } = require('../utils')
1
+const _allStreams = {}
2
 
2
 
3
 const NotificationRoute = require('../routes/notification')
3
 const NotificationRoute = require('../routes/notification')
4
 
4
 
16
  * @param {Stream} event
16
  * @param {Stream} event
17
  * @returns {string}
17
  * @returns {string}
18
  */
18
  */
19
-const stringifyEvent = function (event) {
19
+const _stringifyEvent = function (event) {
20
     let str = ''
20
     let str = ''
21
     const endl = '\r\n'
21
     const endl = '\r\n'
22
     for (const i in event) {
22
     for (const i in event) {
57
         if (this.event) {
57
         if (this.event) {
58
             event.event = this.event
58
             event.event = this.event
59
         }
59
         }
60
-        this.push(stringifyEvent(event))
60
+        this.push(_stringifyEvent(event))
61
         callback()
61
         callback()
62
     }
62
     }
63
     _flush(callback) {
63
     _flush(callback) {
64
-        this.push(stringifyEvent(ENDER))
64
+        this.push(_stringifyEvent(ENDER))
65
         callback()
65
         callback()
66
     }
66
     }
67
 }
67
 }
74
  * @param {Toolkit} h hapi common response toolkit
74
  * @param {Toolkit} h hapi common response toolkit
75
  * @param {object} streamOptions
75
  * @param {object} streamOptions
76
  */
76
  */
77
-const onEvent = (event, h, streamOptions) => {
77
+const _event = (event, h, streamOptions) => {
78
     let active
78
     let active
79
     if (event instanceof Stream.Readable) {
79
     if (event instanceof Stream.Readable) {
80
         if (event._readableState.objectMode) {
80
         if (event._readableState.objectMode) {
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
 module.exports = {
112
 module.exports = {
94
     name: 'notification-plugin',
113
     name: 'notification-plugin',
95
     version: '1.0.0',
114
     version: '1.0.0',
96
     register: async server => {
115
     register: async server => {
97
         await server.route(NotificationRoute)
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 Переглянути файл

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

+ 12
- 3
backend/lib/routes/notification/index.js Переглянути файл

3
 const errorSchema = require('../../schemas/errors')
3
 const errorSchema = require('../../schemas/errors')
4
 const params = require('../../schemas/params')
4
 const params = require('../../schemas/params')
5
 
5
 
6
-const { intializeStream } = require('../../utils')
7
-
8
 const pluginConfig = {
6
 const pluginConfig = {
9
     handlerType: 'notifictaion',
7
     handlerType: 'notifictaion',
10
     docs: {
8
     docs: {
32
              * Write the initial stream
30
              * Write the initial stream
33
              * !: this must remain open for notifications to work
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
         /** Validate based on validators object */
47
         /** Validate based on validators object */

+ 0
- 43
backend/lib/utils.js Переглянути файл

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
-}

Завантаження…
Відмінити
Зберегти