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

feat: Apply data enhancement to realtime results for specific doctypes #1411

Merged
merged 1 commit into from
Oct 8, 2024
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
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
Loading