@jagoankasir/offline
v1.0.1
Published
Offline-first sync queue untuk POS — penyimpanan IndexedDB (Dexie.js) dengan idempotency key mencegah struk ganda.
Maintainers
Readme
@jagoankasir/offline
Offline-first sync queue untuk POS. Transaksi disimpan di IndexedDB (Dexie.js) dan dikirim otomatis saat koneksi pulih. Idempotency key mencegah struk ganda.
Install
npm install @jagoankasir/offlineUsage
import { useOfflineSync } from '@jagoankasir/offline'
// You provide a sync adapter that talks to your backend
const sync = useOfflineSync({
adapter: {
async insertTransaction(tx) {
const res = await fetch('/api/transactions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(tx),
})
if (res.status === 409) return { ok: false, conflict: true }
return { ok: res.ok }
},
async reduceStock(items) {
await fetch('/api/stock/decrement', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(items),
})
},
},
})
// Enqueue when offline
sync.enqueue({
localId: 'tx_123',
items: [...],
subtotal: 50000,
discountPct: 0,
discountValue: 0,
taxValue: 5500,
total: 55500,
paid: 60000,
change: 4500,
})
// sync.pendingCount — jumlah antrian
// sync.isOnline — status koneksi
// sync.flush() — kirim manualHow it works
- Saat offline, transaksi disimpan ke IndexedDB via Dexie.js
- Saat browser mendeteksi koneksi kembali, antrian otomatis dikirim
localIdberfungsi sebagai idempotency key — jika backend mengembalikan konflik (409), transaksi ditandai sudah tersinkron- Setelah berhasil,
reduceStockdipanggil untuk mengurangi stok
License
MIT
