Sanity / gradient document store
npm install @sanity/document-storeSanity / gradient document store
js
// If you are interested in everything that happens with document lol42
const subscription = documents.byId("lol42").subscribe(event => {
console.log('Something happened with document lol42!', event)
})// After a while, when you are no longer interested in updates fom document
lol42:
subscription.unsubscribe()
`How it works
When you add a subscriber to a document the first thing that happens is that the subscriber function is called with a snapshot of the document's current state. This happens as soon as the snapshot is retrieved. If there is a cached version of it on the client, the snapshot is received immediately (synchronously).From that moment on, you will receive a new event every time something happens to the document, until you unsubscribe again.
API
-
createDocumentStore({serverConnection: ServerConnection}) : DocumentStoreServerConnection must be an object with the following api:
-
byId(id : string) : Observable
- query(query : string) : Observable
(todo, add more)$3
$3
- byId(id : string) : Observable
- byIds(ids : Array
- query(query : string) : Observable$3
- update(id : string, spec : UpdateSpec) : Observable
- create(document : Document) : Observable
- replace(documentId : string, document : Document) : Observable
- createOrReplace(document : Document) : Observable
- delete(documentId : string) : : Observable$3
- _id : string
- _type : string$3
$3
- type : string either one of snapshot, update
- snapshot : Document`