This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.js
104 lines (88 loc) · 3.28 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const Vue = window.Vue;
const dbModuleId = 'store-example';
// See https://vuex.vuejs.org/guide/ for a full guide on how to use VueX modules
// See https://github.com/BrewBlox/brewblox-ui/blob/develop/src/plugins/database/types.ts for the database spec
/*
* Datastore objects must implement the StoreObject interface
* {
* id: string; <- required
* _rev?: string; <- assigned by the database when you create the object
* } required when you later save the object again.
*/
export const exampleModule = {
namespaced: true,
state: {
// This message is saved to the VueX store, but not to the datastore.
// It will remain when you switch pages, but disappear if you reload the UI.
localMessage: null,
// These messages are saved to the datastore.
// They will be reloaded when you reload the UI.
persistentMessages: {},
},
getters: {
persistentValues(state) {
return Object.values(state.persistentMessages);
},
},
mutations: {
commitLocal(state, msg) {
state.localMessage = msg;
},
commitPersistent(state, msg) {
// We use Vue.set() because creating a new entry with obj[key] doesn't trigger reactivity
Vue.set(state.persistentMessages, msg.id, msg);
},
commitAllPersistent(state, messages) {
state.persistentMessages = messages.reduce((acc, msg) => ({ ...acc, [msg.id]: msg }), {});
},
commitRemovePersistent(state, msg) {
// To trigger reactivity updates, we have to use Vue.delete()
Vue.delete(state.persistentMessages, msg.id);
},
},
actions: {
async saveLocal(context, msg) {
context.commit('commitLocal', msg);
},
async createPersistent(context, msg) {
const created = await Vue.$database.create(dbModuleId, msg);
context.commit('commitPersistent', created);
},
async savePersistent(context, msg) {
if (!msg._rev) {
throw new Error("Can't save a message without revision ID (_rev)");
}
const saved = await Vue.$database.persist(dbModuleId, msg);
context.commit('commitPersistent', saved);
},
async removePersistent(context, msg) {
const removed = await Vue.$database.remove(dbModuleId, msg);
context.commit('commitRemovePersistent', removed);
},
async start(context) {
// Initial fetch to populate the VueX store
const messages = await Vue.$database.fetchAll(dbModuleId);
context.commit('commitAllPersistent', messages);
// The onChanged / onDeleted callbacks will keep the store synchronized
// Try it out by opening the UI in two browser tabs
await Vue.$database.subscribe({
id: dbModuleId,
onChanged: async (msg) => {
// The update may have been triggered here
// Don't bother re-committing the same message
// This only causes unnecessary re-renders in the UI
const existing = context.state.persistentMessages[msg.id];
if (!existing || existing._rev !== msg._rev) {
context.commit('commitPersistent', msg);
}
},
onDeleted: async (id) => {
const existing = context.state.persistentMessages[id];
if (existing) {
context.commit('commitRemovePersistent', existing);
}
},
});
},
},
};