Skip to content

Commit 4463a84

Browse files
Document new features
1 parent 0d3fe07 commit 4463a84

File tree

5 files changed

+163
-1
lines changed

5 files changed

+163
-1
lines changed

docs/docs/.vitepress/config.ts

+30
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ const config: Partial<SiteData<ThemeConfig>> = {
106106
},
107107
]
108108
},
109+
{
110+
text: 'Events',
111+
collapsible: true,
112+
collapsed: false,
113+
items: [
114+
{
115+
text: 'Event Bus',
116+
link: '/events/',
117+
items: [
118+
{text : 'Global Event Bus', link : '/events/#the-main-event-bus'},
119+
{text : 'Store Event Bus', link : '/events/#store-event-bus'},
120+
]
121+
},
122+
{
123+
text: 'Typing',
124+
link: '/events/typing',
125+
},
126+
]
127+
},
128+
{
129+
text: 'Testing',
130+
collapsible: true,
131+
collapsed: false,
132+
items: [
133+
{
134+
text: 'Setup',
135+
link: '/testing/',
136+
},
137+
]
138+
},
109139
{
110140
text: 'Configuration',
111141
link: '/config/stores',

docs/docs/core-concepts/extending-stores.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ Let's create `store_extensions.d.ts` in our project src dir
4444

4545
```typescript
4646
declare module '@idevelopthings/vue-class-stores/vue' {
47-
export interface StoreCustomProperties {
47+
interface StoreCustomProperties {
4848
apiBaseUrl: string;
4949
appVersion: string;
5050

5151
getSomething(): string;
5252
}
5353
}
54+
export {}
5455
```
5556

5657
Now in our store:

docs/docs/events/index.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
```

docs/docs/events/typing.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Typing events
2+
3+
We can define a declaration file to provide some type information on the available keys and the event data.
4+
5+
```typescript
6+
declare module '@idevelopthings/vue-class-stores/vue' {
7+
interface StoreEventsMap {
8+
'todo:added' : {todo: ITodo}
9+
}
10+
}
11+
12+
export {};
13+
```
14+
15+
Now when we dispatch an event, we'll get completion:
16+
17+
```typescript
18+
// This will give type errors because our
19+
// second param isn't `{todo: ITodo}`
20+
store.$dispatch('todo:added', '...');
21+
```

docs/docs/testing/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TBD

0 commit comments

Comments
 (0)