|
| 1 | +## Event Bus |
| 2 | + |
| 3 | +### The main event bus |
| 4 | + |
| 5 | +The `StoreManager` has an event bus, this will allow us to register event listeners for anything, but also we can |
| 6 | +dispatch events to all registered stores |
| 7 | + |
| 8 | +This is used for dispatching the lifecycle hooks, but will also be useful in applications. It can be accessed |
| 9 | +via `StoreManager.bus.` |
| 10 | + |
| 11 | +#### Dispatching events |
| 12 | + |
| 13 | +```typescript |
| 14 | +import {StoreManager} from '@idevelopthings/vue-class-stores/vue'; |
| 15 | + |
| 16 | +StoreManager.bus.$dispatch('event-name', {my: 'data'}); |
| 17 | +``` |
| 18 | + |
| 19 | +#### Dispatching events to all stores |
| 20 | + |
| 21 | +```typescript |
| 22 | +import {StoreManager} from '@idevelopthings/vue-class-stores/vue'; |
| 23 | + |
| 24 | +StoreManager.bus.$dispatchToAllStores('event-name', {my: 'data'}); |
| 25 | +``` |
| 26 | + |
| 27 | +#### Listening for events |
| 28 | + |
| 29 | +```typescript |
| 30 | +import {StoreManager} from '@idevelopthings/vue-class-stores/vue'; |
| 31 | + |
| 32 | +const listener = (message) => console.log(message); |
| 33 | + |
| 34 | +StoreManager.bus.$on('message', listener); |
| 35 | +``` |
| 36 | + |
| 37 | +#### Removing listeners |
| 38 | + |
| 39 | +```typescript |
| 40 | +import {StoreManager} from '@idevelopthings/vue-class-stores/vue'; |
| 41 | + |
| 42 | +const listener = (message) => console.log(message); |
| 43 | + |
| 44 | +// This way requires a reference to the handler function |
| 45 | +StoreManager.bus.$off('message', listener); |
| 46 | + |
| 47 | +// We can also stop it this way |
| 48 | +const stopListener = StoreManager.bus.$on('message', listener); |
| 49 | +stopListener(); |
| 50 | +``` |
| 51 | + |
| 52 | +### Store event bus |
| 53 | + |
| 54 | +Each store has their own event bus also, this will allow us to dispatch events to individual stores and more. |
| 55 | + |
| 56 | +This can be accessed via `myStore.$eventBus` |
| 57 | + |
| 58 | +#### Dispatching events |
| 59 | + |
| 60 | +```typescript |
| 61 | +myStore.$dispatch('event-name', {my: 'data'}); |
| 62 | +``` |
| 63 | + |
| 64 | +#### Listening for events |
| 65 | + |
| 66 | +```typescript |
| 67 | +const listener = (message) => console.log(message); |
| 68 | + |
| 69 | +myStore.$on('message', listener); |
| 70 | +``` |
| 71 | + |
| 72 | +We can also define event listeners on our store to handle these, they work very similarly to hooks |
| 73 | + |
| 74 | +```typescript |
| 75 | +import {Store, On} from '@idevelopthings/vue-class-stores/vue'; |
| 76 | + |
| 77 | +type ITodoStore = { todos: Todo[] }; |
| 78 | + |
| 79 | +class TodoStore extends Store<TodoStore, ITodoStore>() { |
| 80 | + get state() { |
| 81 | + return {todos: []}; |
| 82 | + } |
| 83 | + |
| 84 | + @On('todo:added') |
| 85 | + todoAdded(todo: any) { |
| 86 | + // this is triggered like a regular listener |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// We can now dispatch an event to it |
| 91 | +todoStore.$dispatch('todo:added', {}) |
| 92 | + |
| 93 | +// If we had this @On('todo:added') decorator added to other stores |
| 94 | +// also, we could call it on all stores via the store manager |
| 95 | +StoreManager.bus.$dispatchToAllStores('todo:added', {}) |
| 96 | +``` |
| 97 | + |
| 98 | +#### Removing listeners |
| 99 | + |
| 100 | +```typescript |
| 101 | +const listener = (message) => console.log(message); |
| 102 | + |
| 103 | +// This way requires a reference to the handler function |
| 104 | +myStore.$off('message', listener); |
| 105 | + |
| 106 | +// We can also stop it this way |
| 107 | +const stopListener = myStore.$on('message', listener); |
| 108 | +stopListener(); |
| 109 | +``` |
0 commit comments