-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Expose real time dispatch changes function
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
Showing
8 changed files
with
188 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Mutations } from '../queries/dsl' | ||
import { receiveMutationResult } from './mutations' | ||
import CozyClient from '../CozyClient' | ||
|
||
/** | ||
* Normalizes an object representing a CouchDB document | ||
* | ||
* Ensures existence of `_type` | ||
* | ||
* @public | ||
* @param {import("../types").CouchDBDocument} couchDBDoc - object representing the document | ||
* @returns {import("../types").CozyClientDocument} full normalized document | ||
*/ | ||
const normalizeDoc = (couchDBDoc, doctype) => { | ||
return { | ||
id: couchDBDoc._id, | ||
_type: doctype, | ||
...couchDBDoc | ||
} | ||
} | ||
|
||
/** | ||
* DispatchChange | ||
* | ||
* @param {CozyClient} client CozyClient instane | ||
* @param {import("../types").Doctype} doctype Doctype of the document to update | ||
* @param {import("../types").CouchDBDocument} couchDBDoc Document to update | ||
* @param {import("../types").Mutation} mutationDefinitionCreator Mutation to apply | ||
*/ | ||
const dispatchChange = ( | ||
client, | ||
doctype, | ||
couchDBDoc, | ||
mutationDefinitionCreator | ||
) => { | ||
const data = normalizeDoc(couchDBDoc, doctype) | ||
const response = { | ||
data | ||
} | ||
|
||
const options = {} | ||
client.dispatch( | ||
receiveMutationResult( | ||
client.generateRandomId(), | ||
response, | ||
options, | ||
mutationDefinitionCreator(data) | ||
) | ||
) | ||
} | ||
|
||
/** | ||
* Dispatches a create action for a document to update CozyClient store from realtime callbacks. | ||
* | ||
* @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 to update CozyClient store from realtime callbacks. | ||
* | ||
* @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 to update CozyClient store from realtime callbacks. | ||
* | ||
* @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 | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
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; | ||
import CozyClient from "../CozyClient"; |