Skip to content

Commit 09bb0f2

Browse files
committed
fix(types): fixed types again
1 parent 816a13c commit 09bb0f2

File tree

11 files changed

+76
-85
lines changed

11 files changed

+76
-85
lines changed

.eslintrc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@ module.exports = {
1010
'plugin:react-hooks/recommended',
1111
'plugin:jest/recommended',
1212
],
13+
rules: {
14+
'@typescript-eslint/ban-types': [
15+
'error',
16+
{
17+
types: {
18+
'{}': false,
19+
},
20+
},
21+
],
22+
},
1323
}

src/global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ declare module 'pouchdb-utils' {
77
}
88

99
declare module 'pouchdb-selector-core' {
10-
export function matchesSelector<T extends Record<string, unknown>>(
10+
export function matchesSelector<T extends {}>(
1111
doc: PouchDB.Core.Document<T>,
1212
selector: PouchDB.Find.Selector
1313
): boolean

src/subscription.test.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ test('should only subscribe once to document updates', () => {
109109
})
110110

111111
test('should handle unsubscribing during an doc update', () => {
112-
expect.assertions(0)
113112
const { changes, get } = myPouch
114113

115114
const doc = {
@@ -119,27 +118,29 @@ test('should handle unsubscribing during an doc update', () => {
119118
}
120119

121120
let callback: (
122-
change: PouchDB.Core.ChangesResponseChange<{}>
123-
) => void = () => {}
121+
change: PouchDB.Core.ChangesResponseChange<Record<string, unknown>>
122+
) => void = () => {
123+
console.error('should not be called')
124+
}
124125

125126
;(myPouch as unknown as { changes: unknown }).changes = () => ({
126127
on(_type: string, callFn: (c: unknown) => void) {
127128
callback = callFn
128129
return {
129-
cancel() {},
130+
cancel: jest.fn(),
130131
}
131132
},
132133
})
133134

134135
const subscriptionManager = new SubscriptionManager(myPouch)
135-
const unsubscribe = subscriptionManager.subscribeToDocs(['test'], () => {})
136+
const unsubscribe = subscriptionManager.subscribeToDocs(['test'], jest.fn())
136137

137-
let getCallback = (_doc: unknown) => {}
138+
let getCallback: (doc: unknown) => void = jest.fn()
138139
;(myPouch as unknown as { get: unknown }).get = jest.fn(() => {
139140
return {
140-
then(fn = (_doc: unknown) => {}) {
141+
then(fn: (doc: unknown) => void) {
141142
getCallback = fn
142-
return { catch() {} }
143+
return { catch: jest.fn() }
143144
},
144145
}
145146
})
@@ -153,11 +154,7 @@ test('should handle unsubscribing during an doc update', () => {
153154
unsubscribe()
154155
myPouch.changes = changes
155156
myPouch.get = get
156-
try {
157-
getCallback(doc)
158-
} catch (err) {
159-
expect(err).toBeUndefined()
160-
}
157+
expect(() => getCallback(doc)).not.toThrow()
161158
})
162159

163160
test('should subscribe to view updates', () => {

src/subscription.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { clone } from 'pouchdb-utils'
22

3-
export type DocsCallback<T extends Record<string, unknown>> = (
3+
export type DocsCallback<T extends {}> = (
44
deleted: boolean,
55
id: PouchDB.Core.DocumentId,
66
doc?: PouchDB.Core.Document<T>
77
) => void
88

99
interface DocsSubscription {
10-
changesFeed: PouchDB.Core.Changes<Record<string, unknown>>
11-
all: Set<DocsCallback<Record<string, unknown>>>
12-
ids: Map<PouchDB.Core.DocumentId, Set<DocsCallback<Record<string, unknown>>>>
10+
changesFeed: PouchDB.Core.Changes<{}>
11+
all: Set<DocsCallback<{}>>
12+
ids: Map<PouchDB.Core.DocumentId, Set<DocsCallback<{}>>>
1313
}
1414

1515
export type ViewCallback = (id: PouchDB.Core.DocumentId) => void
@@ -19,10 +19,10 @@ export type subscribeToView = (
1919
) => () => void
2020

2121
interface SubscriptionToAView {
22-
feed: PouchDB.Core.Changes<Record<string, unknown>>
22+
feed: PouchDB.Core.Changes<{}>
2323
callbacks: Set<ViewCallback>
2424
}
25-
export type subscribeToDocs = <T extends Record<string, unknown>>(
25+
export type subscribeToDocs = <T extends {}>(
2626
ids: PouchDB.Core.DocumentId[] | null,
2727
callback: DocsCallback<T>
2828
) => () => void
@@ -44,7 +44,7 @@ export default class SubscriptionManager {
4444
pouch.once('destroyed', this.#destroyListener)
4545
}
4646

47-
subscribeToDocs<T extends Record<string, unknown>>(
47+
subscribeToDocs<T extends {}>(
4848
ids: PouchDB.Core.DocumentId[] | null,
4949
callback: DocsCallback<T>
5050
): () => void {
@@ -63,19 +63,15 @@ export default class SubscriptionManager {
6363
if (isIds) {
6464
for (const id of ids ?? []) {
6565
if (this.#docsSubscription.ids.has(id)) {
66-
this.#docsSubscription.ids
67-
.get(id)
68-
?.add(callback as DocsCallback<Record<string, unknown>>)
66+
this.#docsSubscription.ids.get(id)?.add(callback as DocsCallback<{}>)
6967
} else {
70-
const set: Set<DocsCallback<Record<string, unknown>>> = new Set()
71-
set.add(callback as DocsCallback<Record<string, unknown>>)
68+
const set: Set<DocsCallback<{}>> = new Set()
69+
set.add(callback as DocsCallback<{}>)
7270
this.#docsSubscription.ids.set(id, set)
7371
}
7472
}
7573
} else {
76-
this.#docsSubscription.all.add(
77-
callback as DocsCallback<Record<string, unknown>>
78-
)
74+
this.#docsSubscription.all.add(callback as DocsCallback<{}>)
7975
}
8076

8177
let didUnsubscribe = false
@@ -86,16 +82,14 @@ export default class SubscriptionManager {
8682
if (isIds) {
8783
for (const id of ids ?? []) {
8884
const set = this.#docsSubscription?.ids.get(id)
89-
set?.delete(callback as DocsCallback<Record<string, unknown>>)
85+
set?.delete(callback as DocsCallback<{}>)
9086

9187
if (set?.size === 0) {
9288
this.#docsSubscription?.ids.delete(id)
9389
}
9490
}
9591
} else {
96-
this.#docsSubscription?.all.delete(
97-
callback as DocsCallback<Record<string, unknown>>
98-
)
92+
this.#docsSubscription?.all.delete(callback as DocsCallback<{}>)
9993
}
10094

10195
if (
@@ -191,15 +185,15 @@ function createDocSubscription(pouch: PouchDB.Database): DocsSubscription {
191185
docsSubscription.all,
192186
false,
193187
change.id,
194-
doc as unknown as PouchDB.Core.Document<Record<string, unknown>>
188+
doc as unknown as PouchDB.Core.Document<{}>
195189
)
196190
}
197191
if (idSubscriptions) {
198192
notify(
199193
idSubscriptions,
200194
false,
201195
change.id,
202-
doc as unknown as PouchDB.Core.Document<Record<string, unknown>>
196+
doc as unknown as PouchDB.Core.Document<{}>
203197
)
204198
}
205199
})
@@ -217,10 +211,10 @@ function createDocSubscription(pouch: PouchDB.Database): DocsSubscription {
217211
}
218212

219213
function notify(
220-
set: Set<DocsCallback<Record<string, unknown>>>,
214+
set: Set<DocsCallback<{}>>,
221215
deleted: boolean,
222216
id: PouchDB.Core.DocumentId,
223-
doc?: PouchDB.Core.Document<Record<string, unknown>>
217+
doc?: PouchDB.Core.Document<{}>
224218
) {
225219
for (const subscription of set) {
226220
try {

src/test-utils.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export async function waitForLoadingChange(
8888
})
8989
}
9090

91-
export async function sleep(milliseconds: number) {
91+
export async function sleep(milliseconds: number): Promise<void> {
9292
return new Promise(resolve => {
9393
setTimeout(resolve, milliseconds)
9494
})

src/useAllDocs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useDeepMemo, CommonOptions } from './utils'
88
* Get all docs or a slice of all docs and subscribe to their updates.
99
* @param options PouchDB's allDocs options.
1010
*/
11-
export default function useAllDocs<Content extends Record<string, unknown>>(
11+
export default function useAllDocs<Content extends {}>(
1212
options?: CommonOptions &
1313
(
1414
| PouchDB.Core.AllDocsWithKeyOptions

src/useDoc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useContext } from './context'
44
import useStateMachine, { ResultType } from './state-machine'
55
import type { CommonOptions } from './utils'
66

7-
type DocResultType<T extends Record<string, unknown>> = ResultType<{
7+
type DocResultType<T extends {}> = ResultType<{
88
doc: (PouchDB.Core.Document<T> & PouchDB.Core.GetMeta) | null
99
}>
1010

@@ -14,7 +14,7 @@ type DocResultType<T extends Record<string, unknown>> = ResultType<{
1414
* @param {object} [options] - PouchDB get options. Excluding 'open_revs'.
1515
* @param {object|function} [initialValue] - Value that should be returned while fetching the doc.
1616
*/
17-
export default function useDoc<Content extends Record<string, unknown>>(
17+
export default function useDoc<Content extends {}>(
1818
id: PouchDB.Core.DocumentId,
1919
options?: (PouchDB.Core.GetOptions & CommonOptions) | null,
2020
initialValue?: (() => Content) | Content

src/useFind.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface FindHookOptions extends CommonOptions {
6161
* Query, and optionally create, a Mango index and subscribe to its updates.
6262
* @param {object} [opts] A combination of PouchDB's find options and create index options.
6363
*/
64-
export default function useFind<Content extends Record<string, unknown>>(
64+
export default function useFind<Content extends {}>(
6565
options: FindHookOptions
6666
): ResultType<PouchDB.Find.FindResponse<Content>> {
6767
const { pouchdb: pouch, subscriptionManager } = useContext(options.db)
@@ -236,7 +236,7 @@ export default function useFind<Content extends Record<string, unknown>>(
236236
function getIndex(
237237
db: PouchDB.Database,
238238
index: FindHookIndexOption | undefined,
239-
selector: PouchDB.Find.FindRequest<Record<string, unknown>>
239+
selector: PouchDB.Find.FindRequest<{}>
240240
): Promise<[string | null, string]> {
241241
if (index && typeof index === 'string') {
242242
return findIndex(db, selector)
@@ -260,7 +260,7 @@ async function createIndex(
260260
): Promise<[string, string]> {
261261
const result = (await db.createIndex(
262262
index
263-
)) as PouchDB.Find.CreateIndexResponse<Record<string, unknown>> & {
263+
)) as PouchDB.Find.CreateIndexResponse<{}> & {
264264
id: PouchDB.Core.DocumentId
265265
name: string
266266
}
@@ -274,7 +274,7 @@ async function createIndex(
274274
*/
275275
async function findIndex(
276276
db: PouchDB.Database,
277-
selector: PouchDB.Find.FindRequest<Record<string, unknown>>
277+
selector: PouchDB.Find.FindRequest<{}>
278278
): Promise<[string | null, string]> {
279279
const database = db as PouchDB.Database & {
280280
explain: (selector: PouchDB.Find.Selector) => Promise<ExplainResult>
@@ -303,22 +303,19 @@ function subscribe(
303303
? '_design/' + id.replace(/^_design\//, '') // normalize, user can add a ddoc name
304304
: undefined
305305

306-
return subscriptionManager.subscribeToDocs<Record<string, unknown>>(
307-
null,
308-
(_del, id, doc) => {
309-
if (idsInResult.ids.has(id)) {
310-
query()
311-
} else if (id === ddocName) {
312-
query()
313-
} else if (doc && typeof matchesSelector !== 'function') {
314-
// because pouchdb-selector-core is semver-free zone
315-
// If matchesSelector doesn't exist, just query every time
316-
query()
317-
} else if (doc && matchesSelector(doc, selector)) {
318-
query()
319-
}
306+
return subscriptionManager.subscribeToDocs<{}>(null, (_del, id, doc) => {
307+
if (idsInResult.ids.has(id)) {
308+
query()
309+
} else if (id === ddocName) {
310+
query()
311+
} else if (doc && typeof matchesSelector !== 'function') {
312+
// because pouchdb-selector-core is semver-free zone
313+
// If matchesSelector doesn't exist, just query every time
314+
query()
315+
} else if (doc && matchesSelector(doc, selector)) {
316+
query()
320317
}
321-
)
318+
})
322319
}
323320

324321
interface ExplainResult {

src/usePouch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useContext } from './context'
44
* Get access to the PouchDB database that is provided by the provider.
55
* @param {string | undefined} dbName Select the database to be returned by its name/key.
66
*/
7-
export default function usePouch<T extends Record<string, unknown>>(
7+
export default function usePouch<T extends {}>(
88
dbName?: string
99
): PouchDB.Database<T> {
1010
return useContext(dbName).pouchdb as PouchDB.Database<T>

src/useView.test.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@ import useView from './useView'
1414
PouchDB.plugin(memory)
1515
PouchDB.plugin(mapReduce)
1616

17-
type TempView = PouchDB.Map<
18-
PouchDB.Core.Document<Record<string, unknown>>,
19-
unknown
20-
>
21-
type TempViewDoc = PouchDB.Filter<
22-
PouchDB.Core.Document<Record<string, unknown>>,
23-
unknown
24-
>
17+
type Doc = PouchDB.Core.Document<{ type: string; test: number; value?: number }>
18+
type TempView = PouchDB.Map<Doc, {}>
19+
type TempViewDoc = PouchDB.Filter<Doc, {}>
2520

2621
let myPouch: PouchDB.Database
2722

@@ -975,7 +970,7 @@ describe('temporary function only views', () => {
975970

976971
const view: TempView = (doc, emit) => {
977972
if (doc.type === 'tester') {
978-
emit?.(doc.type, doc.value)
973+
emit?.(doc.type, doc.value ?? 1)
979974
}
980975
}
981976

@@ -2235,7 +2230,7 @@ describe('temporary views objects', () => {
22352230
const view: TempViewDoc = {
22362231
map: (doc, emit) => {
22372232
if (doc.type === 'tester') {
2238-
emit?.(doc.type, doc.value)
2233+
emit?.(doc.type, doc.value ?? 1)
22392234
}
22402235
},
22412236
}

src/useView.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,26 @@ import type SubscriptionManager from './subscription'
66
import useStateMachine, { ResultType, Dispatch } from './state-machine'
77
import { useDeepMemo, CommonOptions } from './utils'
88

9-
type ViewResponseBase<Result extends Record<string, unknown>> =
10-
PouchDB.Query.Response<Result> & {
11-
/**
12-
* Include an update_seq value indicating which sequence id of the underlying database the view
13-
* reflects.
14-
*/
15-
update_seq?: number | string
16-
}
9+
/* typescript-eslint-disable @typescript-eslint/ban-types */
10+
type ViewResponseBase<Result extends {}> = PouchDB.Query.Response<Result> & {
11+
/**
12+
* Include an update_seq value indicating which sequence id of the underlying database the view
13+
* reflects.
14+
*/
15+
update_seq?: number | string
16+
}
1717

18-
export type ViewResponse<T extends Record<string, unknown>> = ResultType<
19-
ViewResponseBase<T>
20-
>
18+
export type ViewResponse<T extends {}> = ResultType<ViewResponseBase<T>>
2119

2220
/**
2321
* Query a view and subscribe to its updates.
2422
* @param {string | function | object} fun The name of the view or a temporary view.
2523
* @param {object} [opts] PouchDB's query-options
2624
*/
2725
export default function useView<
28-
Content extends Record<string, unknown>,
29-
Result extends Record<string, unknown>,
30-
Model extends Record<string, unknown> = Content
26+
Content extends {},
27+
Result extends {},
28+
Model extends {} = Content
3129
>(
3230
fun: string | PouchDB.Map<Model, Result> | PouchDB.Filter<Model, Result>,
3331
opts?: PouchDB.Query.Options<Model, Result> & {

0 commit comments

Comments
 (0)