Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updates to stream types #365

Merged
merged 2 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/types/Client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Expr from './Expr'
import PageHelper from './PageHelper'
import { ExprArg } from './query'
import RequestResult from './RequestResult'
import { Subscription } from './Stream'
import { Subscription, SubscriptionEventHandlers } from './Stream'

type StreamEventFields = ['action', 'document', 'diff', 'prev']
type StreamEventFields = 'action' | 'document' | 'diff' | 'prev'

export interface ClientConfig {
secret: string
Expand All @@ -29,15 +29,16 @@ export interface QueryOptions
Pick<ClientConfig, 'secret' | 'queryTimeout' | 'fetch' | 'observer'>
> {}

type StreamFn = (
type StreamFn<T> = (
expr: Expr,
options?: {
fields?: StreamEventFields[]
}
) => Subscription
) => Subscription<T>

interface StreamApi extends StreamFn {
document: StreamFn
interface StreamApi
extends StreamFn<Omit<SubscriptionEventHandlers, 'snapshot'>> {
document: StreamFn<SubscriptionEventHandlers>
}

export default class Client {
Expand Down
58 changes: 27 additions & 31 deletions src/types/Stream.d.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
import Client from './Client'
import { ExprArg } from './query'
import { values } from './values'

export interface Subscription {
on: <T extends keyof SubscriptionEventHandlers>(
export interface Subscription<TEventHandlerMap> {
on: <T extends keyof TEventHandlerMap>(
type: T,
callback: SubscriptionEventHandlers[T]
callback: TEventHandlerMap[T]
) => this
start: () => this
close: () => void
}

type SubscriptionEventHandlers = {
start: (type: 'start', txn: number, event: number) => void
error: (
type: 'error',
txn: number,
event: {
code: string
description: string
}
) => void
version: (
type: 'version',
txn: number,
event: {
type Handler<TEventType extends string, TEventData> = (
data: TEventData,
event: {
type: TEventType
event: TEventData
txn?: number
}
) => void

export type SubscriptionEventHandlers = {
start: Handler<'start', number>
error: Handler<'error', unknown>
version: Handler<
'version',
{
action: 'create' | 'update' | 'delete'
document: object
diff: object
prev: object
document?: object
diff?: object
prev?: object
}
) => void
history_rewrite: (
type: 'history_rewrite',
txn: number,
event: {
>
history_rewrite: Handler<
'history_rewrite',
{
action: 'history_rewrite'
document: object
}
) => void
snapshot: (type: 'snapshot', txn: number, event: object) => void
>
snapshot: Handler<'snapshot', object>
}