MeshKit

Sync

Queue provider work and retry durable operations.

Use mesh.sync when an app needs to queue work and flush it later: offline drafts, retryable uploads, background reconciliation, or provider operations that should survive process boundaries.

MeshKit stores sync jobs and checkpoints through the provider contract. It does not secretly run a background service for every runtime. Your app decides when to call flush, start a scheduler, or hand execution to a platform background task.

Queue And Flush Work

import { meshkit } from "@meshkit/meshkit";

const mesh = await meshkit();

await mesh.sync.enqueue({
  kind: "upload-draft",
  payload: { draftId: "draft-1" },
  conflictKey: "draft:draft-1",
  conflictPolicy: "replace",
});

const processed = await mesh.sync.flush({
  network: "online",
  handler: async (job) => {
    console.log("uploading", job.payload.draftId);
    return { uploaded: true };
  },
});

console.log(processed.map((job) => job.status));

How Sync Works

app creates job
-> provider stores durable job metadata
-> app resumes later
-> app calls flush when network/runtime allows
-> handler processes job
-> MeshKit stores status and checkpoints

This design is intentionally caller-managed. Browser service workers, React Native background tasks, Ionic/Capacitor tasks, and Node workers all have different lifecycle rules.

API

await mesh.sync.enqueue(input);
await mesh.sync.jobs();
await mesh.sync.checkpoint(jobId, checkpoint);
await mesh.sync.flush(options);
const scheduler = mesh.sync.createScheduler(options);
APIUse it for
sync.enqueue(input)Add a durable job
sync.jobs()List public sync jobs
sync.checkpoint(jobId, checkpoint)Store progress for a job
sync.flush(options)Process ready jobs now
sync.createScheduler(options)Create a caller-managed interval scheduler

Conflict Policies

PolicyBehavior
dedupeReuse an existing active job for the same conflictKey
failThrow when a conflicting active job exists
replaceReplace the active job for the same conflictKey

Use conflictKey for user-visible resources such as draft:draft-1, file:avatar, or workspace:123:sync.

Provider Requirements

Sync needs a provider that can store sync job records. Raw byte storage alone is not enough. If a provider does not support sync queues, MeshKit fails with sync_not_supported.

Failure Modes

Error or symptomMeaningAction
sync_not_supportedProvider cannot store durable sync jobsUse a MeshKit provider with sync job support
Job stays pendingApp never called flush under allowed network/runtime conditionsWire a scheduler, worker, or platform background callback
Duplicate workMissing or weak conflictKeyAdd a stable key and choose a conflict policy
Job loops foreverHandler returns failure without backoff or terminal stateAdd retry limits and checkpoint progress

Production Notes

  • Keep handlers idempotent. A retry can run after partial progress.
  • Persist user-visible status in your app if users need progress after restart.
  • Store checkpoints for large multi-step work.
  • In mobile apps, pair sync with the platform's real background execution model.
  • Do not put secrets in job payloads unless they are encrypted and scoped for that runtime.

Next Steps

On this page