onSync
is called when a sync begins, and again when the sync ends. The parameter syncing
is set to true
when onSync
is called at the beginning of a sync, and false
when it
is called at the end of a sync.
This can be used in a React like app by doing something like the following:
const [syncing, setSyncing] = useState(false);
useEffect(() => {
rep.onSync = setSyncing;
}, [rep]);
The delay between when a change is made to Replicache and when Replicache attempts to push that change.
Whether the Replicache database has been closed. Once Replicache has been closed it no longer syncs and you can no longer read or write data out of it. After it has been closed it is pretty much useless and should not be used any more.
A rough heuristic for whether the client is currently online. Note that there is no way to know for certain whether a client is online - the next request can always fail. This is true if the last sync attempt succeeded, and false otherwise.
Closes this Replicache instance.
When closed all subscriptions end and no more read or writes are allowed.
Creates a persistent secondary index in Replicache which can be used with scan.
If the named index already exists with the same definition this returns success immediately. If the named index already exists, but with a different definition an error is returned.
Drops an index previously created with createIndex.
Get a single value from the database.
Determines if a single key
is present in the database.
Whether the database is empty.
Query is used for read transactions. It is recommended to use transactions
to ensure you get a consistent view across multiple calls to get
, has
and scan
.
Registers a mutator, which is used to make changes to the data.
Mutators run once when they are initially invoked, but they might also be replayed multiple times during sync. As such mutators should not modify application state directly. Also, it is important that the set of registered mutator names only grows over time. If Replicache syncs and needed mutator is not registered, it will substitute a no-op mutator, but this might be a poor user experience.
During sync, a description of each mutation is sent to the server's batch
endpoint
where it is applied. Once the mutation has been applied successfully, as
indicated by the client
view's
lastMutationId
field, the local version of the mutation is removed. See
the design
doc for
additional details on the sync protocol.
Mutators are atomic: all their changes are applied together, or none are. Throwing an exception aborts the transaction. Otherwise, it is committed. As with [query] and [subscribe] all reads will see a consistent view of the cache while they run.
register
returns the function to use to mutate Replicache.
const createTodo = rep.register('createTodo',
async (tx: WriteTransaction, args: JSONValue) => {
const key = `/todo/${args.id}`;
if (await tx.has(key)) {
throw new Error('Todo already exists');
}
await tx.put(key, args);
});
This will create the function to later use:
await createTodo({id: 1234, title: 'Make things work offline', complete: true});
Gets many values from the database. This returns a ScanResult
which
implements AsyncIterable
. It also has methods to iterate over the keys
and entries
.
If options
has an indexName
, then this does a scan over an index with
that name. A scan over an index uses a tuple for the key consisting of
[secondary: string, primary: string]
.
Convenience form of scan()
which returns all the entries as an array.
When this is set to true
the internal Replicache wasm module will log
more things to the console (using console.debug
). Setting this to false
reduces the amount of logging done by the wasm module.
If you want to see the verbose logging from Replicache in Devtools/Web
Inspector you also need to change the console log level to Verbose
.
Subcribe to changes to the underlying data. Every time the underlying data
changes body
is called and if the result of body
changes compared to
last time onData
is called. The function is also called once the first
time the subscription is added.
This returns a function that can be used to cancel the subscription.
If an error occurs in the body
the onError
function is called if
present. Otherwise, the error is thrown.
Synchronizes this cache with the server. New local mutations are sent to the server, and the latest server state is applied to the cache. Any local mutations not included in the new server state are replayed. See the Replicache design document for more information on sync: https://github.com/rocicorp/replicache/blob/master/design.md
Generated using TypeDoc
This gets called when we get an HTTP unauthorized from the client view or the batch endpoint. Set this to a function that will ask your user to reauthenticate.