Skip to content

Commit

Permalink
feat(realtime): Add _type on returned doc by the handlers
Browse files Browse the repository at this point in the history
in the document returned by the realtime, there is no _type
associated with the doctype, which requires some gymnastics
on the app side when the realtime is connected to different
doctypes.

Thanks to this improvement, it is enough to analyze the document
to know to which doctype it is associated.

It is in the case where the same callback is connected on different
subscribes, we do not discriminate upstream but downstream
via the doc.

The other reason is also to standardize compared to the return
of CC which normalizes the doc and adds this _type.
  • Loading branch information
JF-Cozy committed Mar 8, 2023
1 parent 5f9a0d4 commit 759cdb3
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/cozy-realtime/src/CozyRealtime.js
Expand Up @@ -456,7 +456,7 @@ class CozyRealtime {
)
for (const handler of handlers) {
try {
handler(payload.doc)
handler({ ...payload.doc, _type: payload.type })
} catch (e) {
logger.error(
`handler did throw for ${event}, ${payload.type}, ${payload.id}`
Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-realtime/src/CozyRealtime.legacy.spec.js
Expand Up @@ -64,7 +64,7 @@ describe('CozyRealtime', () => {

const type = 'io.cozy.bank.accounts'
const id = 'doc_id'
const fakeDoc = { _id: id, title: 'title1' }
const fakeDoc = { _id: id, title: 'title1', _type: type }

describe('subscribe', () => {
it('should launch handler when document is created', async done => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-realtime/src/CozyRealtime.spec.js
Expand Up @@ -21,7 +21,7 @@ const doctype = 'io.cozy.files'
const type = doctype
const id = 'my-document-id'
const message = { message: 'hello' }
const doc = message
const doc = { ...message, _type: type }
const event = 'UPDATED'
const payload = { type, doc, id }

Expand Down

0 comments on commit 759cdb3

Please sign in to comment.