選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

utils.js 1.1KB

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