Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

notification.service.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { remote } from '../utils/db.js'
  2. /**
  3. * Base notifier class
  4. * @param {number} profileId needed to listen for events for this profile
  5. */
  6. class Toaster {
  7. constructor(profileId) {
  8. this.url = `${remote}/notification/${profileId}/subscribe`
  9. this.source = null
  10. this.source = new EventSource(this.url)
  11. this.listenFor('end', message => this.source.close)
  12. }
  13. listenFor(event, callback) {
  14. this.source.addEventListener(event, callback)
  15. }
  16. stop() {
  17. this.source.close()
  18. }
  19. }
  20. /**
  21. * Example extension that listens for 'stonk' events
  22. */
  23. class StonkAlert extends Toaster {
  24. constructor(profileId, waveCb) {
  25. super(profileId)
  26. this.event = 'stonk'
  27. this.stonks = {}
  28. this.listenFor(`${profileId}.${this.event}`, message => {
  29. const parsed = JSON.parse(message.data)
  30. this.stonks[parsed.name] = parsed
  31. waveCb(this._formatToast(parsed), parsed.type)
  32. })
  33. }
  34. _formatToast(parsed) {
  35. return `${parsed.name}: ${parsed.profile_id} ${parsed.order} at ${parsed.price}`
  36. }
  37. }
  38. export { Toaster, StonkAlert }