Skip to content

Commit 5b3f056

Browse files
committed
refactor: cleaner
1 parent 9239166 commit 5b3f056

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

src/database/optionsApi.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,19 @@ export function databasePlugin(
117117
) {
118118
const options = Object.assign({}, globalOptions, userOptions)
119119
const target = toRef(this.$data as any, key)
120-
let unbinds = rtdbUnbinds.get(this)
121-
122-
if (unbinds) {
123-
if (unbinds[key]) {
124-
unbinds[key](
125-
// if wait, allow overriding with a function or reset, otherwise, force reset to false
126-
// else pass the reset option
127-
options.wait
128-
? typeof options.reset === 'function'
129-
? options.reset
130-
: false
131-
: options.reset
132-
)
133-
}
134-
} else {
135-
rtdbUnbinds.set(this, (unbinds = {}))
120+
if (!rtdbUnbinds.has(this)) {
121+
rtdbUnbinds.set(this, {})
136122
}
123+
const unbinds = rtdbUnbinds.get(this)!
137124

138-
// TODO: ensure creating refs outside of the scope of the component here doesn't create a memory leak
125+
if (unbinds[key]) {
126+
unbinds[key](options.wait)
127+
}
128+
129+
// FIXME: Create a single scopeEffect per instance that wraps thin call and stop the effect scope when `unbind()` is called
139130
const { promise, unbind } = _useDatabaseRef(source, { target, ...options })
140131
unbinds[key] = unbind
141132

142-
// TODO:
143-
// this._firebaseSources[key] = source
144133
// we make it readonly for the user but we must change it. Maybe there is a way to have an internal type here but expose a readonly type through a d.ts
145134
;(this.$firebaseRefs as Mutable<Record<string, DatabaseReference>>)[key] =
146135
source.ref
@@ -161,8 +150,13 @@ export function databasePlugin(
161150
if (!bindings) return
162151

163152
for (const key in bindings) {
164-
// @ts-expect-error: TODO: try fixing it
165-
this[bindName](key, bindings[key], globalOptions)
153+
// @ts-expect-error: bindName is a string here
154+
this[bindName](
155+
// ts
156+
key,
157+
bindings[key],
158+
globalOptions
159+
)
166160
}
167161
},
168162

@@ -173,7 +167,7 @@ export function databasePlugin(
173167
unbinds[key]()
174168
}
175169
}
176-
// @ts-expect-error: we are freeing the memory
170+
// @ts-expect-error: we are freeing the references to avoid memory leaks
177171
this.$firebaseRefs = null
178172
},
179173
})

0 commit comments

Comments
 (0)