4.5.2 • Published 3 years ago

@getcircuit/snappy v4.5.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Snappy

Snappy was born from the idea of not creating duplicate snapshot listeners for a single document. Since then we've confirmed that Firebase already does that (hooray!), which left Snappy as a thin API to access firestore snapshots more easily, in our own coding style and with the possibility of enforcing types without an ugly type-casting.

Options

type Options = {
    // if true, debug logs will be output to the console
    // default: false
    debug?: boolean
    // the delay to unmount a snapshot listener when it has no observers
    // default: 30s
    cleanupDelay?: number
    // the actual firestore object
    firestore: firebase.firestore.Firestore
})

Options can be set via the setConfig method.

Watchers

Snappy provide watcher methods for one to subscribe to changes to a snapshot target.

function watchDocument(document, onNext, onError): UnsubcribeFn
function watchCollection(collection, onNext, onError): UnsubcribeFn
function watchQuery(query, onNext, onError): UnsubcribeFn

Every watcher returns an unsubscribe method that will cancel the subscription created by it. A watcher works by:

  1. Creating a listener to the passed target if it doesn't exist yet.
  2. Attaching a new observer to the listener.
  3. Returning a method to cancel the subscription, detaching the observer from the listener.

Once a listener doesn't have any observers left, a timer is schedulled to destroy the listener. The default delay for destroying a listener is 30s.

API

Document, Collection and Query watchers

function watchDocument(document, onNext, onError): UnsubcribeFn
function watchCollection(collection, onNext, onError): UnsubcribeFn
function watchQuery(query, onNext, onError): UnsubcribeFn

Document, collection and query watchers support a onNext and onError parameters. The former being executed when a change is dispatched succesfully and the latter when an error occurs.

The onNext receives a single parameter, called bag, which hold the values of what the target being watched. The onNext method can also return a function that will be executed when the observer is unsubscribed.

readDocument

Allows to read the value of a document in non-callback manner.

function readDocument(document): Promise<{
  id: string
  ref: DocumentReference
  data: unknown
  exists: boolean
}>

Generally, you should read the value by watching it and using the value as it changes over time. Occasionally, you may need to retrieve the value to which you're not subscribed. readDocument allows you to do so.

This works by creating a subscription, reading the value, then unsubscribing. It's therefore not recommended in hot code paths.

4.4.1

3 years ago

4.4.0

3 years ago

4.4.3

3 years ago

4.4.2

3 years ago

4.4.4

3 years ago

4.4.4-fbv9.0

3 years ago

4.5.0

3 years ago

4.5.2

3 years ago

4.5.1

3 years ago

4.2.4-potato.0

3 years ago

4.2.4-potato.1

3 years ago

4.2.4-potato.2

3 years ago

4.3.1

3 years ago

4.3.0

3 years ago

4.2.3

3 years ago

4.2.2

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

3.2.6

3 years ago

3.2.5

3 years ago

3.3.3-alpha.11

3 years ago

3.3.3-alpha.12

3 years ago

3.3.3-alpha.13

3 years ago

3.3.3-alpha.14

3 years ago

3.3.3-alpha.15

3 years ago

3.3.3-alpha.16

3 years ago

3.3.3-alpha.17

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.3.3-alpha.8

3 years ago

3.3.3-alpha.9

3 years ago

3.3.3-alpha.6

3 years ago

3.3.3-alpha.5

3 years ago

3.3.3-alpha.3

3 years ago

3.3.3-alpha.0

3 years ago

3.2.9

3 years ago

3.2.8

3 years ago

3.2.7

3 years ago

4.0.2-kiwi.0

3 years ago

4.0.1-alpha.0

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.3.2

3 years ago

3.2.10

3 years ago

4.1.0

3 years ago

3.2.2

4 years ago

3.2.4

4 years ago

3.2.3

4 years ago

3.2.1

4 years ago

3.1.3

4 years ago

3.2.0

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.3.10

4 years ago

2.3.9

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.4

4 years ago

2.3.3

4 years ago

2.3.6

4 years ago

2.3.8

4 years ago

2.3.7

4 years ago

2.3.0

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.5

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.4

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.2.0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago