Skip to content

Commit e97c566

Browse files
committed
refactor: unbind type
1 parent 5089c6c commit e97c566

File tree

5 files changed

+22
-20
lines changed

5 files changed

+22
-20
lines changed

src/database/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
noop,
1515
OperationsType,
1616
ResetOption,
17+
UnbindWithReset,
1718
walkSet,
1819
_MaybeRef,
1920
_RefWithState,
@@ -46,14 +47,12 @@ export interface UseDatabaseRefOptions extends _DatabaseRefOptions {
4647
target?: Ref<unknown>
4748
}
4849

49-
type UnbindType = ReturnType<typeof bindAsArray | typeof bindAsObject>
50-
5150
export function _useDatabaseRef(
5251
reference: _MaybeRef<DatabaseReference | Query>,
5352
localOptions: UseDatabaseRefOptions = {}
5453
) {
5554
const options = Object.assign({}, rtdbOptions, localOptions)
56-
let _unbind!: UnbindType
55+
let _unbind!: UnbindWithReset
5756

5857
const data = options.target || ref<unknown | null>(options.initialValue)
5958
const error = ref<Error>()
@@ -112,7 +111,7 @@ export function _useDatabaseRef(
112111
// should take an option like once: true to not setting up any listener
113112
}
114113

115-
let stopWatcher: ReturnType<typeof watch> = noop
114+
let stopWatcher = noop
116115
if (isRef(reference)) {
117116
stopWatcher = watch(reference, bindDatabaseRef, { immediate: true })
118117
} else {
@@ -142,7 +141,7 @@ export function _useDatabaseRef(
142141

143142
export function internalUnbind(
144143
key: string,
145-
unbinds: Record<string, UnbindType> | undefined,
144+
unbinds: Record<string, UnbindWithReset> | undefined,
146145
reset?: ResetOption
147146
) {
148147
if (unbinds && unbinds[key]) {
@@ -185,5 +184,5 @@ export function useObject<T = unknown>(
185184
}) as _RefDatabase<VueDatabaseDocumentData<T> | undefined>
186185
}
187186

188-
export const unbind = (target: Ref, reset?: _DatabaseRefOptions['reset']) =>
187+
export const unbind = (target: Ref, reset?: ResetOption) =>
189188
internalUnbind('', rtdbUnbinds.get(target), reset)

src/database/optionsApi.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { DatabaseReference, DataSnapshot, Query } from 'firebase/database'
22
import { App, ComponentPublicInstance, toRef } from 'vue'
33
import { isVue3 } from 'vue-demi'
4+
import { ResetOption, UnbindWithReset } from '../shared'
45
import { internalUnbind, _useDatabaseRef } from './index'
56
import {
67
bindAsArray,
@@ -44,7 +45,7 @@ declare module '@vue/runtime-core' {
4445
/**
4546
* Unbinds a bound reference
4647
*/
47-
$rtdbUnbind: (name: string, reset?: _DatabaseRefOptions['reset']) => void
48+
$rtdbUnbind: (name: string, reset?: ResetOption) => void
4849

4950
/**
5051
* Bound database references
@@ -58,7 +59,7 @@ declare module '@vue/runtime-core' {
5859
* @internal
5960
*/
6061
// _firebaseUnbinds: Readonly<
61-
// Record<string, ReturnType<typeof bindAsArray | typeof bindAsObject>>
62+
// Record<string, UnbindWithReset>
6263
// >
6364
}
6465
export interface ComponentCustomOptions {
@@ -74,7 +75,7 @@ export type FirebaseOption = VueFirebaseObject | (() => VueFirebaseObject)
7475

7576
export const rtdbUnbinds = new WeakMap<
7677
object,
77-
Record<string, ReturnType<typeof bindAsArray | typeof bindAsObject>>
78+
Record<string, UnbindWithReset>
7879
>()
7980

8081
/**
@@ -101,7 +102,7 @@ export function databasePlugin(
101102

102103
GlobalTarget[unbindName] = function rtdbUnbind(
103104
key: string,
104-
reset?: _DatabaseRefOptions['reset']
105+
reset?: ResetOption
105106
) {
106107
internalUnbind(key, rtdbUnbinds.get(this), reset)
107108
delete this.$firebaseRefs[key]

src/firestore/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
noop,
2323
OperationsType,
2424
ResetOption,
25+
UnbindWithReset,
2526
walkSet,
2627
_MaybeRef,
2728
_Nullable,
@@ -42,8 +43,6 @@ export const ops: OperationsType = {
4243
remove: (array, index) => array.splice(index, 1),
4344
}
4445

45-
type UnbindType = ReturnType<typeof bindCollection | typeof bindDocument>
46-
4746
export interface _UseFirestoreRefOptions extends FirestoreRefOptions {
4847
/**
4948
* Use the `target` ref instead of creating one.
@@ -64,7 +63,7 @@ export function _useFirestoreRef(
6463
>,
6564
localOptions?: _UseFirestoreRefOptions
6665
) {
67-
let _unbind: UnbindType = noop
66+
let _unbind: UnbindWithReset = noop
6867
const options = Object.assign({}, firestoreOptions, localOptions)
6968

7069
// TODO: allow passing pending and error refs as option for when this is called using the options api
@@ -124,7 +123,7 @@ export function _useFirestoreRef(
124123
})
125124
}
126125

127-
let stopWatcher: ReturnType<typeof watch> = noop
126+
let stopWatcher = noop
128127
if (isRef(docOrCollectionRef)) {
129128
stopWatcher = watch(docOrCollectionRef, bindFirestoreRef, {
130129
immediate: true,
@@ -264,9 +263,7 @@ export function useDocument<T>(
264263

265264
export function internalUnbind(
266265
key: string,
267-
unbinds:
268-
| Record<string, ReturnType<typeof bindCollection | typeof bindDocument>>
269-
| undefined,
266+
unbinds: Record<string, UnbindWithReset> | undefined,
270267
reset?: FirestoreRefOptions['reset']
271268
) {
272269
if (unbinds && unbinds[key]) {

src/firestore/optionsApi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
_GlobalFirestoreRefOptions,
1515
} from './subscribe'
1616
import { internalUnbind, _useFirestoreRef } from '.'
17-
import { ResetOption } from '../shared'
17+
import { ResetOption, UnbindWithReset } from '../shared'
1818

1919
export type FirestoreOption = VueFirestoreObject | (() => VueFirestoreObject)
2020

@@ -27,7 +27,7 @@ export type VueFirestoreObject = Record<
2727

2828
export const firestoreUnbinds = new WeakMap<
2929
object,
30-
Record<string, ReturnType<typeof bindCollection | typeof bindDocument>>
30+
Record<string, UnbindWithReset>
3131
>()
3232

3333
/**
@@ -192,7 +192,7 @@ declare module '@vue/runtime-core' {
192192
* @internal
193193
*/
194194
// _firestoreUnbinds: Readonly<
195-
// Record<string, ReturnType<typeof bindCollection | typeof bindDocument>>
195+
// Record<string, UnbindWithReset>
196196
// >
197197
}
198198

src/shared.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export interface OperationsType {
3030
*/
3131
export type ResetOption = boolean | (() => unknown)
3232

33+
/**
34+
* Return type of `$rtdbBind()` and `$firestoreBind()`
35+
*/
36+
export type UnbindWithReset = (reset?: ResetOption) => void
37+
3338
/**
3439
* @internal
3540
*/

0 commit comments

Comments
 (0)