Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FirebaseCacheService

whatitdoes

Wraps some angularfire2 read methods for returning data from Firebase with the added function of storing the data locally for offline use.

consumers

ApiService

providerscope

AppComponent

--------------------------------------------------------

Features:

  • Returns real-time Firebase data via Observables
  • While online Firebase data is stored locally (as data changes the local store is updated)
  • While offline local data is served if available
  • On reconnect, Observables update app with new Firebase data
  • Even while online, local data is used first when available which results in a faster load ux
  • If loaded from local store while online, and Firebase sends changes (usually a few moments later), the changes will be sent to all subscribers and the local store will be updated right away.

Hierarchy

  • FirebaseCacheService

Index

Constructors

constructor

Properties

Private af

af: AngularFire

Angular Fire service used to connect to Firebase

cache

  • In-memory cache containing ReplaySubjects that return the latest value for any given Firebase reference.
  • That value can come from a Firebase subscription or from the device if there is no internet connection.

Private localforage

localforage: LocalForageService

Angular 2 wrapper of localforage that allows storing data offline using asynchronous storage (IndexedDB or WebSQL) with a simple, localStorage-like API

Methods

Private getList

  • getList(key: string): void
  • Retrives a list if locally stored on the device

    • Lists are stored as individual objects, to allow for better offline reuse.
    • Each locally stored list uses a map to stitch together the list from individual objects

    Parameters

    • key: string

    Returns void

list

  • list(key: string, query?: FirebaseListFactoryOpts, objKey?: string): Observable<[any]>
  • Returns an Observable array of Firebase snapshot data

    • This method can be used in place of angularfire2's list method and it will work offline
    • Sets up a list via setupList if cache is empty for this reference.
    • Each list item is stored as a separate object for offline use. This allows offline access to the entire list or a specific object in the list if the list is stored offline.
    • Does not include angularfire2 meta-fields such as $key or $exists

    Parameters

    • key: string

      the Firebase reference for this list

    • Optional query: FirebaseListFactoryOpts

      optional angularfire2 query param. Allows all valid queries

    • Optional objKey: string

      used as the object key when storing each list item as an object for offline use via setupList

    Returns Observable<[any]>

object

  • object(key: string, query?: FirebaseObjectFactoryOpts): Observable<object>
  • Returns an Observable object of Firebase snapshot data

    • This method can be used in place of angularfire2's object method and it will work offline
    • Sets up a list via setupList if cache is empty for this reference
    • Does not include angularfire2 meta-fields such as $key or $value

    Parameters

    • key: string

      the Firebase reference for this list

    • Optional query: FirebaseObjectFactoryOpts

      optional angularfire2 query param. Allows all valid queries available for objects

    Returns Observable<object>

Private setList

  • setList(key: string, array: Array<any>, objKeyPartial: string): void
  • Stores a list for offline use

    • Stores each list item as a separate object using the relavant Firebase reference string to allow offline use of the entire list or just a specific object
    • Stores a map of all the objects, used to stitch together the list for local use

    Parameters

    • key: string
    • array: Array<any>
    • objKeyPartial: string

    Returns void

Private setupList

  • setupList(key: string, query?: FirebaseListFactoryOpts, objKey?: string): void
    • Sets up a FirebaseCache item that provides Firebase data
    • Subscribes to the list's Firebase reference
    • Gets the most recent locally stored non-null value and sends to all app subscribers via getList
    • When Firebase sends a value this FirebaseCache item is set to loaded, the new value is sent to all app subscribers, and the value is stored locally via setList

    Parameters

    • key: string

      passed directly from list's key param

    • Default value query: FirebaseListFactoryOpts = {}

      passed directly from list's query param

    • Optional objKey: string

      passed directly from list's objKey param

    Returns void

Private setupObject

  • setupObject(key: string, query?: FirebaseObjectFactoryOpts): void
    • Sets up a FirebaseCache item that provides Firebase data
    • Subscribes to the object's Firebase reference
    • Gets the most recent locally stored non-null value and sends to all app subscribers
    • When Firebase sends a value this FirebaseCache item is set to loaded, the new value is sent to all app subscribers, and the value is stored locally

    Parameters

    • key: string

      passed directly from object's key param

    • Default value query: FirebaseObjectFactoryOpts = {}

      passed directly from object's query param

    Returns void

Generated using TypeDoc