Skip to content

Commit

Permalink
introduce set streaming support
Browse files Browse the repository at this point in the history
  • Loading branch information
erickpintor committed Jan 28, 2022
1 parent b81a8da commit 0bfb9f2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var http = require('./_http')
var q = require('./query')
var util = require('./_util')

var DefaultEvents = ['start', 'error', 'version', 'history_rewrite']
var DefaultEvents = ['start', 'error', 'version', 'history_rewrite', 'set']
var DocumentStreamEvents = DefaultEvents.concat(['snapshot'])

/**
Expand Down Expand Up @@ -285,13 +285,14 @@ EventDispatcher.prototype.dispatch = function(event) {

/**
* @typedef {Object} Options
* @property {string[]} [fields=['action', 'document', 'diff', 'prev']]
* @property {string[]} [fields=['action', 'document', 'diff', 'prev', 'index']]
* The fields event fields to opt-in during stream subscription. Possible
* options:
* * 'action': The action type
* * 'document': The document's data
* * 'diff': The difference between 'document' and 'prev'
* * 'prev': The event's previous data
* * 'index': The event's source index, if a set event
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion src/types/Client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ExprArg } from './query'
import RequestResult from './RequestResult'
import { Subscription, SubscriptionEventHandlers } from './Stream'

type StreamEventFields = 'action' | 'document' | 'diff' | 'prev'
type StreamEventFields = 'action' | 'document' | 'diff' | 'prev' | 'index'

export interface ClientConfig {
secret: string
Expand Down
8 changes: 8 additions & 0 deletions src/types/Stream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export type SubscriptionEventHandlers = {
prev?: object
}
>
set: Handler<
'set',
{
action: 'add' | 'remove'
document?: object
index?: object
}
>
history_rewrite: Handler<
'history_rewrite',
{
Expand Down
30 changes: 30 additions & 0 deletions test/stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,34 @@ describe('StreamAPI', () => {
assertActiveSessions(0)
})
})

describe('set', () => {
test('can listen to set adds', done => {
stream = client.stream(q.Documents(coll.ref))
.on('start', () => {
client.query(
q.Create(coll.ref, {
data: { name: "foo" }
})
)
})
.on('set', event => {
expect(event.action).toEqual('add')
done()
})
.start()
})

test('can listen to set removes', done => {
stream = client.stream(q.Documents(coll.ref))
.on('start', () => {
client.query(q.Delete(doc.ref))
})
.on('set', event => {
expect(event.action).toEqual('remove')
done()
})
.start()
})
})
})

0 comments on commit 0bfb9f2

Please sign in to comment.