-
Notifications
You must be signed in to change notification settings - Fork 12
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: Expose real time dispatch changes function #1532
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Mutations } from '../queries/dsl' | ||
import { receiveMutationResult } from './mutations' | ||
|
||
/** | ||
* 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 {object} client CozyClient instane | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why removing the CozyClient type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had issues with tests when moving the functions in the a file that is imported and exported in
|
||
* @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 {object} 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 {object} 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 {object} 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 | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function dispatchCreate(client: object, doctype: import("../types").Doctype, couchDBDoc: import("../types").CouchDBDocument): void; | ||
export function dispatchUpdate(client: object, doctype: import("../types").Doctype, couchDBDoc: import("../types").CouchDBDocument): void; | ||
export function dispatchDelete(client: object, doctype: import("../types").Doctype, couchDBDoc: import("../types").CouchDBDocument): void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really nitpicking, but why the plural in realtimes.js? There is only one, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... I should change it. It was because other file were plurals in the folder...