Skip to content

Commit

Permalink
feat: Apply data enhancement to realtime results for specific doctypes
Browse files Browse the repository at this point in the history
This enrichment closes the gap between the results of queries from the
cozy-stack and realtime. In fact, realtime returns the document
directly from the database, unlike the stack, which can add computed
fields. Without them, it can lead to instable behaviour in
applications. Some applications fix some issue by reimplementing the
RealTimeQueries component. This commit aims to provide unifed api to
avoid duplicated code. It can also be used as a reference when we want
to close the gap.
  • Loading branch information
cballevre committed Oct 8, 2024
1 parent 1743f00 commit 7bd3a29
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 119 deletions.
21 changes: 12 additions & 9 deletions docs/api/cozy-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ Deconstructed link

### dispatchCreate

**dispatchCreate**(`client`, `doctype`, `couchDBDoc`): `void`
**dispatchCreate**(`client`, `doctype`, `couchDBDoc`, `options?`): `Promise`<`void`>

Dispatches a create action for a document to update CozyClient store from realtime callbacks.

Expand All @@ -347,20 +347,21 @@ Dispatches a create action for a document to update CozyClient store from realti
| `client` | `any` | CozyClient instance |
| `doctype` | `string` | Doctype of the document to create |
| `couchDBDoc` | `CouchDBDocument` | Document to create |
| `options` | `DispatchOptions` | - |

*Returns*

`void`
`Promise`<`void`>

*Defined in*

[packages/cozy-client/src/store/realtime.js:58](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/store/realtime.js#L58)
[packages/cozy-client/src/store/realtime.js:76](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/store/realtime.js#L76)

***

### dispatchDelete

**dispatchDelete**(`client`, `doctype`, `couchDBDoc`): `void`
**dispatchDelete**(`client`, `doctype`, `couchDBDoc`, `options?`): `Promise`<`void`>

Dispatches a delete action for a document to update CozyClient store from realtime callbacks.

Expand All @@ -371,20 +372,21 @@ Dispatches a delete action for a document to update CozyClient store from realti
| `client` | `any` | CozyClient instance |
| `doctype` | `string` | Doctype of the document to create |
| `couchDBDoc` | `CouchDBDocument` | Document to create |
| `options` | `DispatchOptions` | - |

*Returns*

`void`
`Promise`<`void`>

*Defined in*

[packages/cozy-client/src/store/realtime.js:80](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/store/realtime.js#L80)
[packages/cozy-client/src/store/realtime.js:120](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/store/realtime.js#L120)

***

### dispatchUpdate

**dispatchUpdate**(`client`, `doctype`, `couchDBDoc`): `void`
**dispatchUpdate**(`client`, `doctype`, `couchDBDoc`, `options?`): `Promise`<`void`>

Dispatches a update action for a document to update CozyClient store from realtime callbacks.

Expand All @@ -395,14 +397,15 @@ Dispatches a update action for a document to update CozyClient store from realti
| `client` | `any` | CozyClient instance |
| `doctype` | `string` | Doctype of the document to create |
| `couchDBDoc` | `CouchDBDocument` | Document to create |
| `options` | `DispatchOptions` | - |

*Returns*

`void`
`Promise`<`void`>

*Defined in*

[packages/cozy-client/src/store/realtime.js:69](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/store/realtime.js#L69)
[packages/cozy-client/src/store/realtime.js:98](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/store/realtime.js#L98)

***

Expand Down
34 changes: 23 additions & 11 deletions packages/cozy-client/src/RealTimeQueries.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import {
dispatchDelete,
dispatchUpdate
} from './store/realtime'
import { ensureFilePath } from './helpers/realtime'

/**
* Component that subscribes to a doctype changes and keep the
* internal store updated.
*
* @param {object} options - Options
* @param {import("./types").Doctype} options.doctype - The doctype to watch
* @param {object} options - Options
* @param {import("./types").Doctype} options.doctype - The doctype to watch
* @returns {null} The component does not display anything.
*/
const RealTimeQueries = ({ doctype }) => {
Expand All @@ -26,16 +27,27 @@ const RealTimeQueries = ({ doctype }) => {
)
}

let options = {}
if (doctype === 'io.cozy.files') {
options.enhanceDocFn = ensureFilePath
}

const handleCreated = data => {
dispatchCreate(client, doctype, data, options)
}

const handleUpdated = data => {
dispatchUpdate(client, doctype, data, options)
}

const handleDeleted = data => {
dispatchDelete(client, doctype, data, options)
}

const subscribe = async () => {
await realtime.subscribe('created', doctype, data =>
dispatchCreate(client, doctype, data)
)
await realtime.subscribe('updated', doctype, data =>
dispatchUpdate(client, doctype, data)
)
await realtime.subscribe('deleted', doctype, data =>
dispatchDelete(client, doctype, data)
)
await realtime.subscribe('created', doctype, handleCreated)
await realtime.subscribe('updated', doctype, handleUpdated)
await realtime.subscribe('deleted', doctype, handleDeleted)
}
subscribe()

Expand Down
Loading

0 comments on commit 7bd3a29

Please sign in to comment.