/
firestore.ts
163 lines (153 loc) · 4.18 KB
/
firestore.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { VUEXFIRE_SET_VALUE, VUEXFIRE_ARRAY_ADD, VUEXFIRE_ARRAY_REMOVE } from './mutations-types'
import {
bindCollection,
bindDocument,
OperationsType,
FirestoreOptions,
firestoreOptions,
} from '@posva/vuefire-core'
import { CommitFunction } from './shared'
import { firestore } from 'firebase'
import { ActionContext, Action } from 'vuex'
const commitOptions = { root: true }
export { firestoreOptions }
// Firebase binding
const subscriptions = new WeakMap()
function bind(
state: Record<string, any>,
commit: CommitFunction,
key: string,
ref: firestore.Query | firestore.CollectionReference,
ops: OperationsType,
options: FirestoreOptions
): Promise<firestore.DocumentData[]>
function bind(
state: Record<string, any>,
commit: CommitFunction,
key: string,
ref: firestore.DocumentReference,
ops: OperationsType,
options: FirestoreOptions
): Promise<firestore.DocumentData>
function bind(
state: Record<string, any>,
commit: CommitFunction,
key: string,
ref: firestore.DocumentReference | firestore.Query | firestore.CollectionReference,
ops: OperationsType,
options: FirestoreOptions
): Promise<firestore.DocumentData> | Promise<firestore.DocumentData[]> {
// TODO: check ref is valid warning
// TODO: check defined in state warning
let sub = subscriptions.get(commit)
if (!sub) {
sub = Object.create(null)
subscriptions.set(commit, sub)
}
// unbind if ref is already bound
if (key in sub) {
unbind(
commit,
key,
options.wait ? (typeof options.reset === 'function' ? options.reset : false) : options.reset
)
}
return new Promise((resolve, reject) => {
sub[key] =
'where' in ref
? bindCollection(
{
vm: state,
key,
collection: ref,
ops,
resolve,
reject,
},
options
)
: bindDocument(
{
vm: state,
key,
document: ref,
ops,
resolve,
reject,
},
options
)
})
}
function unbind(commit: CommitFunction, key: string, reset?: FirestoreOptions['reset']) {
const sub = subscriptions.get(commit)
if (!sub || !sub[key]) return
// TODO dev check before
sub[key](reset)
delete sub[key]
}
interface FirestoreActionContext<S, R> extends ActionContext<S, R> {
bindFirestoreRef(
key: string,
ref: firestore.Query | firestore.CollectionReference,
options?: FirestoreOptions
): Promise<firestore.DocumentData[]>
bindFirestoreRef(
key: string,
ref: firestore.DocumentReference,
options?: FirestoreOptions
): Promise<firestore.DocumentData>
unbindFirestoreRef(key: string): void
}
export function firestoreAction<S, R>(
action: (context: FirestoreActionContext<S, R>, payload: any) => any
): Action<S, R> {
return function firestoreEnhancedActionFn(context, payload) {
// get the local state and commit. These may be bound to a module
const { state, commit } = context
const ops: OperationsType = {
set: (target, path, data) => {
commit(
VUEXFIRE_SET_VALUE,
{
path,
target,
data,
},
commitOptions
)
return data
},
add: (target, newIndex, data) =>
commit(VUEXFIRE_ARRAY_ADD, { target, newIndex, data }, commitOptions),
remove: (target, oldIndex) => {
const data = target[oldIndex]
commit(VUEXFIRE_ARRAY_REMOVE, { target, oldIndex }, commitOptions)
return [data]
},
}
return action.call(
this,
{
...context,
bindFirestoreRef: (
key: string,
ref: firestore.DocumentReference | firestore.Query | firestore.CollectionReference,
options?: FirestoreOptions
) =>
bind(
state,
commit,
key,
// @ts-ignore
ref,
ops,
Object.assign({}, firestoreOptions, options)
),
unbindFirestoreRef: (key: string, reset?: FirestoreOptions['reset']) =>
unbind(commit, key, reset),
},
payload
)
}
}