eventBus.test.js 816 B

12345678910111213141516171819202122232425262728293031323334
  1. const eventBus = require('../src/service/EventBus').default
  2. class TestJob {
  3. constructor(name) {
  4. this.name = name
  5. this.interval = setInterval(() => {
  6. console.log(`${this.name} is running`)
  7. }, 1000);
  8. this.subscription = eventBus.subscribe('pause', arg => {
  9. this.onPause(arg)
  10. })
  11. }
  12. onPause(i) {
  13. if (this.name === i) {
  14. clearInterval(this.interval)
  15. this.subscription.unsubscribe()
  16. console.log(`${this.name} pasued`)
  17. }
  18. }
  19. }
  20. for (let i = 0; i < 5; i++) {
  21. let job = new TestJob(`job ${i}`)
  22. }
  23. setTimeout(() => {
  24. console.log('pause job3')
  25. eventBus.publish('pause', 'job 3')
  26. }, 4000)
  27. setTimeout(() => {
  28. console.log('pause job3')
  29. eventBus.publish('pause', 'job 3')
  30. }, 10000)