View Source Sanity.Listen (sanity_listen v0.2.0)

For listening to changes using the Sanity CMS listening API endpoint.

The streams returned by listen!/2 and listen_for_doc_changes!/2 must be iterated upon in the same process that called the function. When this process exits then the underlying HTTPS connection will be closed. The HTTPS connection will also be closed if the stream is halted.

Summary

Functions

Returns {doc, doc_conn} where doc is the latest version, including drafts, of a document. doc_conn can be passed to listen_for_doc_changes!/1 to stream document updates.

Returns an endless Stream of Sanity.Listen.Event items.

Returns an endless Stream that emits the latest version of a document. Call get_draft_or_doc!/2 to get the doc_conn to be passed to this function. If a draft version of the document exists then the draft will be emitted. Otherwise, the published version of the document will be emitted. If neither the draft not the published document exist then nil will be emitted.

Functions

Link to this function

get_draft_or_doc!(id, opts)

View Source

Returns {doc, doc_conn} where doc is the latest version, including drafts, of a document. doc_conn can be passed to listen_for_doc_changes!/1 to stream document updates.

Returns an endless Stream of Sanity.Listen.Event items.

Link to this function

listen_for_doc_changes!(doc_conn)

View Source

Returns an endless Stream that emits the latest version of a document. Call get_draft_or_doc!/2 to get the doc_conn to be passed to this function. If a draft version of the document exists then the draft will be emitted. Otherwise, the published version of the document will be emitted. If neither the draft not the published document exist then nil will be emitted.

When the "Publish" or "Unpublish" actions are being performed in Sanity Studio then it is normal to temporarily have an outdated document or nil emitted. For example, consider the case where you have a unpublished draft and the click the "Publish" button. Sanity Studio will delete the draft document and create the published document. The order of these mutations is inconsistent and if draft is deleted before the published document is created then nil will be emitted briefly.