Skip to content

Commit

Permalink
feat: Expose real time dispatch changes function
Browse files Browse the repository at this point in the history
When in a React environment, we use the RealTimeQueries component to
subscribe to a doctype changes and keep the internal store updated.

To be able to do the same thing manually for non React environment,
we expose the underlaying functions : dispatchCreate, dispatchCreate,
dispatchDelete.
  • Loading branch information
zatteo committed Sep 18, 2024
1 parent 30d2ca1 commit dc17427
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 20 deletions.
81 changes: 81 additions & 0 deletions docs/api/cozy-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,87 @@ Deconstructed link

***

### dispatchCreate

**dispatchCreate**(`client`, `doctype`, `couchDBDoc`): `void`

Dispatches a create action for a document.

Allows to update CozyClient store from realtime callbacks where
we can not use the RealTimeQueries React component.

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `client` | [`CozyClient`](classes/CozyClient.md) | CozyClient instance |
| `doctype` | `string` | Doctype of the document to create |
| `couchDBDoc` | `CouchDBDocument` | Document to create |

*Returns*

`void`

*Defined in*

[packages/cozy-client/src/RealTimeQueries.jsx:64](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/RealTimeQueries.jsx#L64)

***

### dispatchDelete

**dispatchDelete**(`client`, `doctype`, `couchDBDoc`): `void`

Dispatches a delete action for a document.

Allows to update CozyClient store from realtime callbacks where
we can not use the RealTimeQueries React component.

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `client` | [`CozyClient`](classes/CozyClient.md) | CozyClient instance |
| `doctype` | `string` | Doctype of the document to create |
| `couchDBDoc` | `CouchDBDocument` | Document to create |

*Returns*

`void`

*Defined in*

[packages/cozy-client/src/RealTimeQueries.jsx:92](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/RealTimeQueries.jsx#L92)

***

### dispatchUpdate

**dispatchUpdate**(`client`, `doctype`, `couchDBDoc`): `void`

Dispatches a update action for a document.

Allows to update CozyClient store from realtime callbacks where
we can not use the RealTimeQueries React component.

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `client` | [`CozyClient`](classes/CozyClient.md) | CozyClient instance |
| `doctype` | `string` | Doctype of the document to create |
| `couchDBDoc` | `CouchDBDocument` | Document to create |

*Returns*

`void`

*Defined in*

[packages/cozy-client/src/RealTimeQueries.jsx:78](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/RealTimeQueries.jsx#L78)

***

### ensureFirstSlash

**ensureFirstSlash**(`path`): `any`
Expand Down
74 changes: 56 additions & 18 deletions packages/cozy-client/src/RealTimeQueries.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,53 @@ const dispatchChange = (
)
}

/**
* Dispatches a create action for a document.
*
* Allows to update CozyClient store from realtime callbacks where
* we can not use the RealTimeQueries React component.
*
* @param {CozyClient} client - CozyClient instance
* @param {import("./types").Doctype} doctype - Doctype of the document to create
* @param {import("./types").CouchDBDocument} couchDBDoc - Document to create
*/
export const dispatchCreate = (client, doctype, couchDBDoc) => {
dispatchChange(client, doctype, couchDBDoc, Mutations.createDocument)
}

/**
* Dispatches a update action for a document.
*
* Allows to update CozyClient store from realtime callbacks where
* we can not use the RealTimeQueries React component.
*
* @param {CozyClient} client - CozyClient instance
* @param {import("./types").Doctype} doctype - Doctype of the document to create
* @param {import("./types").CouchDBDocument} couchDBDoc - Document to create
*/
export const dispatchUpdate = (client, doctype, couchDBDoc) => {
dispatchChange(client, doctype, couchDBDoc, Mutations.updateDocument)
}

/**
* Dispatches a delete action for a document.
*
* Allows to update CozyClient store from realtime callbacks where
* we can not use the RealTimeQueries React component.
*
* @param {CozyClient} client - CozyClient instance
* @param {import("./types").Doctype} doctype - Doctype of the document to create
* @param {import("./types").CouchDBDocument} couchDBDoc - Document to create
*/
export const dispatchDelete = (client, doctype, couchDBDoc) => {
dispatchChange(
client,
doctype,
{ ...couchDBDoc, _deleted: true },
Mutations.deleteDocument
)
}

/**
* Component that subscribes to a doctype changes and keep the
* internal store updated.
Expand All @@ -71,25 +118,16 @@ const RealTimeQueries = ({ doctype }) => {
)
}

const dispatchCreate = couchDBDoc => {
dispatchChange(client, doctype, couchDBDoc, Mutations.createDocument)
}
const dispatchUpdate = couchDBDoc => {
dispatchChange(client, doctype, couchDBDoc, Mutations.updateDocument)
}
const dispatchDelete = couchDBDoc => {
dispatchChange(
client,
doctype,
{ ...couchDBDoc, _deleted: true },
Mutations.deleteDocument
)
}

const subscribe = async () => {
await realtime.subscribe('created', doctype, dispatchCreate)
await realtime.subscribe('updated', doctype, dispatchUpdate)
await realtime.subscribe('deleted', doctype, dispatchDelete)
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)
)
}
subscribe()

Expand Down
7 changes: 6 additions & 1 deletion packages/cozy-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ export {
} from './utils'
export { getQueryFromState } from './store'
export { default as Registry } from './registry'
export { default as RealTimeQueries } from './RealTimeQueries'
export {
default as RealTimeQueries,
dispatchCreate,
dispatchUpdate,
dispatchDelete
} from './RealTimeQueries'

import * as manifest from './models/manifest'
export { manifest }
Expand Down
4 changes: 4 additions & 0 deletions packages/cozy-client/types/RealTimeQueries.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export function dispatchCreate(client: CozyClient, doctype: import("./types").Doctype, couchDBDoc: import("./types").CouchDBDocument): void;
export function dispatchUpdate(client: CozyClient, doctype: import("./types").Doctype, couchDBDoc: import("./types").CouchDBDocument): void;
export function dispatchDelete(client: CozyClient, doctype: import("./types").Doctype, couchDBDoc: import("./types").CouchDBDocument): void;
declare var _default: import("react").MemoExoticComponent<({ doctype }: {
doctype: import("./types").Doctype;
}) => null>;
export default _default;
import CozyClient from "./CozyClient";
2 changes: 1 addition & 1 deletion packages/cozy-client/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export { default as StackLink } from "./StackLink";
export { default as compose } from "lodash/flow";
export { getQueryFromState } from "./store";
export { default as Registry } from "./registry";
export { default as RealTimeQueries } from "./RealTimeQueries";
export { default as CozyProvider } from "./Provider";
export { default as withMutation } from "./withMutation";
export { default as withMutations } from "./withMutations";
Expand All @@ -21,4 +20,5 @@ export { Association, HasMany, HasOne, HasOneInPlace, HasManyInPlace, HasManyTri
export { isReferencedBy, isReferencedById, getReferencedBy, getReferencedById } from "./associations/helpers";
export { deconstructCozyWebLinkWithSlug, deconstructRedirectLink, dehydrate, generateWebLink, ensureFirstSlash, rootCozyUrl, InvalidRedirectLinkError, InvalidCozyUrlError, InvalidProtocolError, BlockedCozyError } from "./helpers";
export { cancelable, isQueryLoading, hasQueryBeenLoaded, isQueriesLoading, hasQueriesBeenLoaded } from "./utils";
export { default as RealTimeQueries, dispatchCreate, dispatchUpdate, dispatchDelete } from "./RealTimeQueries";
export { queryConnect, queryConnectFlat, withClient } from "./hoc";

0 comments on commit dc17427

Please sign in to comment.