Morrys Repositories

Morrys Repositories

  • Docs
  • GitHub

›offline

wora

  • wora

caching

  • Cache Persist
  • Relay Store
  • Apollo Cache

networking

  • NetInfo
  • Detect Network

offline

  • Offline Architecture
  • Thinking Offline
  • Offline First
  • Relay Offline
  • Apollo Offline

Offline First

@wora/offline-first

Installation

Install @wora/offline-first using yarn or npm:

yarn add @wora/offline-first

OfflineFirst

import { OfflineFirst, OfflineFirstOptions, OfflineRecordCache, Request } from "@wora/offline-first";

const persistOptionsStoreOffline = {
    prefix: 'example-offline',
    serialize: true,
};

asyncfunction execute(offlineRecord) {
    // ... fetch
}

async function  discard(offlineRecord) {
    // ... rollback
    return true;
}

async function complete(offlineRecord) {
    // ... commit
    return true;
}
    
const options: OfflineFirstOptions<Payload> = {
        execute: (offlineRecord: any) => execute(offlineRecord),
        finish?: (mutations: ReadonlyArray<OfflineRecordCache<T>>, error?: Error ) => undefined,
        onComplete: (options: { offlineRecord: OfflineRecordCache<T>, response: any }) => complete(options),
        onDiscard: (options: { offlineRecord: OfflineRecordCache<T>, error: any }) => discard(options),
            };

const offlineFirst = new OfflineFirst(options, persistOptionsStoreOffline);  


// ...

offlineFirst.restore().then(isRestored => setState(isRestored))

// ...

offlineFirst.publish({
    request : {
        payload: { url: '/api/todo', method: 'POST', json: { todoId } }
    }
})

Options


export type OfflineFirstOptions<T> = {
    manualExecution?: boolean;
    execute: (offlineRecord: OfflineRecordCache<T>) => Promise<any>;
    start?: (mutations: Array<OfflineRecordCache<T>>) => Promise<Array<OfflineRecordCache<T>>>;
    onExecute?: (mutation: OfflineRecordCache<T>) => Promise<OfflineRecordCache<T>>;
    finish?: (mutations: ReadonlyArray<OfflineRecordCache<T>>, error?: Error) => Promise<void>;
    onComplete?: (options: { offlineRecord: OfflineRecordCache<T>; response: any }) => Promise<boolean>;
    onDiscard?: (options: { offlineRecord: OfflineRecordCache<T>; error: any }) => Promise<boolean>;
    onPublish?: (offlineRecord: OfflineRecordCache<T>) => Promise<OfflineRecordCache<T>>;
    compare?: (v1: OfflineRecordCache<T>, v2: OfflineRecordCache<T>) => number;
    // onDispatch?: (request: any) => any;
};

  • execute: this is the only mandatory parameter. In this function, communications with the server must be implemented.

  • start: function that is called once the request queue has been started.

  • finish: function that is called once the request queue has been processed.

  • onExecute: function that is called before the request is sent to the network.

  • manualExecution: if set to true, requests in the queue are no longer performed automatically as soon as you go back online. invoke manually: offlineFirst.process();

  • onPublish: function that is called before saving the mutation in the store

  • onComplete: function that is called once the request has been successfully completed. Only if the function returns the value true, the request is deleted from the queue.

  • onDiscard: function that is called when the request returns an error. Only if the function returns the value true, the mutation is deleted from the queue

Publish


publish(options: {
        id?: string,
        request: Request<T>,
        serial?,
    }): Promise<OfflineRecordCache<T>>

  • This is the method that must be invoked when you want to insert a request in the store.

request parameter is the only mandatory one and consists of the main information useful for managing the execution of the request.

requests are executed in parallel, but with the serial parameter it is possible to specify whether you want to execute some or all of the requests sequentially.

Types


export type Request<T> = {
    payload: T,
    backup?: any,
    sink?: any,
}

  • it is advisable to use the backup and sink fields when working in contexts with optimistic responses. In order to manage any UI updates.

export type OfflineRecordCache<T> = {
    id: string,
    request: Request<T>,
    fetchTime: number,
    retry?: number,
    error?: any,
    serial?: boolean
}

  • The parameters fetchTime,retry and error are managed automatically by the library during the offline process.

This library depends exclusively on @wora/cache-persist and @wora/netinfo and I recommend using all their features and potential.

Last updated on 2020-4-18 by morrys
← Thinking OfflineRelay Offline →
  • Installation
  • OfflineFirst
  • Options
  • Publish
  • Types
  • This library depends exclusively on @wora/cache-persist and @wora/netinfo and I recommend using all their features and potential.
Morrys Repositories
Docs
about wora
Community
User Showcase
More
GitHubStar
Follow @m0rrys
Facebook Open Source
Copyright © 2021 Lorenzo Di Giacomo