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

ENG-3044: Introduce set streaming support #589

Merged
merged 1 commit into from
Feb 1, 2022
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
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()
})
})
})