-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathoffline-store.js
More file actions
489 lines (436 loc) · 17.1 KB
/
Copy pathoffline-store.js
File metadata and controls
489 lines (436 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
* SpellBloc Offline-First Persistence Layer (issue #12)
*
* IndexedDB-backed, event-sourced store meant to become the single
* on-device source of truth for progress data. Every progress mutation
* (word attempt, session end, star award, achievement trigger) or settings
* change is modeled as an immutable, UUID-tagged event; the current
* snapshot is a pure reduction over the event log, and the same log is used
* as an append-only outbox that gets flushed to the server once online.
*
* Scope note: this module is additive infrastructure. It does not read from
* or write to the existing `spellbloc_*` localStorage keys that game.js
* still uses directly — it only *copies* them once (migrateFromLocalStorage)
* so existing players' progress isn't lost when this store becomes the
* source of truth. Rewiring game.js's game loop onto this store is tracked
* separately in issue #6; the server ingestion endpoint is tracked in #9.
* See docs/OFFLINE_ARCHITECTURE.md for the full design and conflict-
* resolution rules.
*/
(function (global) {
'use strict';
const DB_NAME = 'spellbloc';
const DB_VERSION = 1;
const STORE_EVENTS = 'events';
const STORE_OUTBOX = 'outbox';
const STORE_KV = 'kv';
const MIGRATION_FLAG_KEY = 'migrated_local_storage_v1';
const DEVICE_ID_KEY = 'spellbloc_device_id';
const LEGACY_KEYS = {
age: 'spellbloc_age',
totalStars: 'spellbloc_totalStars',
playerLevel: 'spellbloc_playerLevel',
language: 'spellbloc_language',
gameMode: 'spellbloc_gameMode',
analytics: 'spellbloc_analytics',
accessibility: 'spellbloc_accessibility',
};
const EVENT_TYPES = new Set([
'word_attempt',
'session_end',
'star_award',
'achievement_trigger',
'level_up',
'settings_change',
]);
const DEFAULT_SYNC_ENDPOINT = '/api/sync/events';
const BACKGROUND_SYNC_TAG = 'spellbloc-outbox-sync';
const MIN_RETRY_DELAY_MS = 5000;
const MAX_RETRY_DELAY_MS = 5 * 60 * 1000;
// ---------------------------------------------------------------------
// Event log: creation, dedupe, and conflict resolution.
// Pure functions — no IndexedDB/browser API involved — so they're
// testable directly under Node (see tests/offline-store.test.js).
// ---------------------------------------------------------------------
function generateUUID() {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
// Fallback RFC4122-ish v4 for environments without crypto.randomUUID.
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
function getClientId() {
if (typeof localStorage === 'undefined') return 'unknown-device';
try {
let id = localStorage.getItem(DEVICE_ID_KEY);
if (!id) {
id = generateUUID();
localStorage.setItem(DEVICE_ID_KEY, id);
}
return id;
} catch (error) {
return 'unknown-device';
}
}
/**
* Create an immutable progress/settings event. `type` must be one of
* EVENT_TYPES. The event's `id` is the idempotency key used both for
* local dedupe (reduceEvents) and, once #9 lands, server-side dedupe on
* replay — a double flush of the same event must never double-count.
*/
function createEvent(type, payload, options = {}) {
if (!EVENT_TYPES.has(type)) {
throw new Error(`Unknown offline event type: ${type}`);
}
return {
id: options.id || generateUUID(),
type,
payload,
clientId: options.clientId || getClientId(),
childId: options.childId || null,
createdAt: options.createdAt || Date.now(),
};
}
/** Union of two (or more) event logs, deduped by id. This *is* the
* multi-device merge: events are immutable and content-addressed by id,
* so combining two devices' logs is just a set union — feeding the
* result through reduceEvents gives the reconciled snapshot. */
function mergeEventLogs(...eventLists) {
return dedupeById(eventLists.flat());
}
function dedupeById(events) {
const byId = new Map();
for (const event of events) {
byId.set(event.id, event);
}
return Array.from(byId.values());
}
/**
* Reduce an event log to a progress snapshot. Conflict-resolution rules
* (see docs/OFFLINE_ARCHITECTURE.md):
* - star_award, word_attempt, session_end: additive/append-only —
* disjoint events are summed/collected. Dedup by id first so a
* replayed event is never double-counted.
* - level_up: monotonic high-water-mark — take the max across all
* events, order-independent.
* - achievement_trigger: idempotent set membership, keyed by
* achievementId — triggering the same achievement twice (same device
* or two devices) is a no-op.
* - settings_change: last-write-wins by event timestamp.
*/
function reduceEvents(events) {
const sorted = dedupeById(events).sort((a, b) => a.createdAt - b.createdAt);
const snapshot = {
totalStars: 0,
playerLevel: 1,
currentAge: null,
language: null,
gameMode: null,
accessibility: null,
achievements: [],
sessions: [],
wordAttempts: [],
};
const achievementIds = new Set();
for (const event of sorted) {
switch (event.type) {
case 'star_award':
snapshot.totalStars += event.payload.stars || 0;
break;
case 'level_up':
snapshot.playerLevel = Math.max(snapshot.playerLevel, event.payload.level || 1);
break;
case 'settings_change':
snapshot[event.payload.key] = event.payload.value;
break;
case 'achievement_trigger':
achievementIds.add(event.payload.achievementId);
break;
case 'session_end':
snapshot.sessions.push(event.payload);
break;
case 'word_attempt':
snapshot.wordAttempts.push(event.payload);
break;
default:
break;
}
}
snapshot.achievements = Array.from(achievementIds);
return snapshot;
}
// ---------------------------------------------------------------------
// One-time migration from the legacy scattered localStorage keys.
// Read-only with respect to localStorage: existing keys are never
// modified or cleared, since game.js still reads/writes them directly
// until issue #6 rewires the game loop onto this store.
// ---------------------------------------------------------------------
function readLegacyLocalStorage() {
if (typeof localStorage === 'undefined') return null;
const readJSON = (key) => {
try {
const raw = localStorage.getItem(key);
return raw ? JSON.parse(raw) : null;
} catch (error) {
return null;
}
};
return {
age: parseInt(localStorage.getItem(LEGACY_KEYS.age), 10) || null,
totalStars: parseInt(localStorage.getItem(LEGACY_KEYS.totalStars), 10) || null,
playerLevel: parseInt(localStorage.getItem(LEGACY_KEYS.playerLevel), 10) || null,
language: localStorage.getItem(LEGACY_KEYS.language) || null,
gameMode: localStorage.getItem(LEGACY_KEYS.gameMode) || null,
accessibility: readJSON(LEGACY_KEYS.accessibility),
analyticsSessions: readJSON(LEGACY_KEYS.analytics),
};
}
/** Synthesize the one-time migration event log from a legacy snapshot
* (as returned by readLegacyLocalStorage). Each field becomes a single
* lump-sum/LWW event rather than a per-unit event, so replaying the
* migration never inflates totals. */
function buildMigrationEvents(legacy, options = {}) {
if (!legacy) return [];
const baseOptions = { clientId: options.clientId, createdAt: options.createdAt || Date.now() };
const events = [];
if (Number.isFinite(legacy.totalStars) && legacy.totalStars > 0) {
events.push(createEvent('star_award', { stars: legacy.totalStars, source: 'legacy-migration' }, baseOptions));
}
if (Number.isFinite(legacy.playerLevel) && legacy.playerLevel > 0) {
events.push(createEvent('level_up', { level: legacy.playerLevel }, baseOptions));
}
if (Number.isFinite(legacy.age)) {
events.push(createEvent('settings_change', { key: 'currentAge', value: legacy.age }, baseOptions));
}
if (legacy.language) {
events.push(createEvent('settings_change', { key: 'language', value: legacy.language }, baseOptions));
}
if (legacy.gameMode) {
events.push(createEvent('settings_change', { key: 'gameMode', value: legacy.gameMode }, baseOptions));
}
if (legacy.accessibility) {
events.push(createEvent('settings_change', { key: 'accessibility', value: legacy.accessibility }, baseOptions));
}
if (Array.isArray(legacy.analyticsSessions)) {
legacy.analyticsSessions.forEach((session) => {
events.push(createEvent('session_end', session, baseOptions));
});
}
return events;
}
// ---------------------------------------------------------------------
// IndexedDB-backed store. Only instantiated/used in a browser context.
// ---------------------------------------------------------------------
function requestToPromise(request) {
return new Promise((resolve, reject) => {
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
function txDone(tx) {
return new Promise((resolve, reject) => {
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
tx.onabort = () => reject(tx.error);
});
}
class OfflineStore {
constructor({ dbName = DB_NAME, dbVersion = DB_VERSION } = {}) {
this.dbName = dbName;
this.dbVersion = dbVersion;
this._dbPromise = null;
}
open() {
if (this._dbPromise) return this._dbPromise;
this._dbPromise = new Promise((resolve, reject) => {
const request = indexedDB.open(this.dbName, this.dbVersion);
request.onupgradeneeded = () => {
const db = request.result;
if (!db.objectStoreNames.contains(STORE_EVENTS)) {
db.createObjectStore(STORE_EVENTS, { keyPath: 'id' });
}
if (!db.objectStoreNames.contains(STORE_OUTBOX)) {
db.createObjectStore(STORE_OUTBOX, { keyPath: 'id' });
}
if (!db.objectStoreNames.contains(STORE_KV)) {
db.createObjectStore(STORE_KV, { keyPath: 'key' });
}
};
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
return this._dbPromise;
}
/** Append an event to both the durable event log and the sync outbox
* in a single transaction. */
async appendEvent(event) {
const db = await this.open();
const tx = db.transaction([STORE_EVENTS, STORE_OUTBOX], 'readwrite');
tx.objectStore(STORE_EVENTS).put(event);
tx.objectStore(STORE_OUTBOX).put(event);
return txDone(tx);
}
async getAllEvents() {
const db = await this.open();
const tx = db.transaction(STORE_EVENTS, 'readonly');
return requestToPromise(tx.objectStore(STORE_EVENTS).getAll());
}
async getOutboxEvents() {
const db = await this.open();
const tx = db.transaction(STORE_OUTBOX, 'readonly');
return requestToPromise(tx.objectStore(STORE_OUTBOX).getAll());
}
async removeFromOutbox(ids) {
const db = await this.open();
const tx = db.transaction(STORE_OUTBOX, 'readwrite');
const store = tx.objectStore(STORE_OUTBOX);
ids.forEach((id) => store.delete(id));
return txDone(tx);
}
async getSnapshot() {
return reduceEvents(await this.getAllEvents());
}
async getKV(key) {
const db = await this.open();
const tx = db.transaction(STORE_KV, 'readonly');
const record = await requestToPromise(tx.objectStore(STORE_KV).get(key));
return record ? record.value : undefined;
}
async setKV(key, value) {
const db = await this.open();
const tx = db.transaction(STORE_KV, 'readwrite');
tx.objectStore(STORE_KV).put({ key, value });
return txDone(tx);
}
/** One-time copy of the legacy localStorage state into the event log.
* Safe to call on every load: no-ops once MIGRATION_FLAG_KEY is set. */
async migrateFromLocalStorage() {
const already = await this.getKV(MIGRATION_FLAG_KEY);
if (already) return { migrated: false, reason: 'already-migrated' };
const legacy = readLegacyLocalStorage();
if (!legacy) return { migrated: false, reason: 'no-localStorage' };
const events = buildMigrationEvents(legacy, { clientId: getClientId() });
for (const event of events) {
await this.appendEvent(event);
}
await this.setKV(MIGRATION_FLAG_KEY, true);
return { migrated: true, eventCount: events.length };
}
}
// ---------------------------------------------------------------------
// Sync outbox: flushes pending events to the server ingestion endpoint
// (tracked in issue #9 — until it exists, flush() will simply fail and
// retry with backoff, which is expected/harmless).
// ---------------------------------------------------------------------
class SyncOutbox {
constructor(store, options = {}) {
this.store = store;
this.endpoint = options.endpoint || DEFAULT_SYNC_ENDPOINT;
this.fetchImpl = options.fetchImpl || (typeof fetch !== 'undefined' ? fetch.bind(global) : null);
this._retryDelay = MIN_RETRY_DELAY_MS;
this._retryTimer = null;
this._flushing = false;
}
/** Wire up the online-event fallback and attempt a Background Sync
* registration, then try an immediate flush. */
start() {
if (typeof window !== 'undefined') {
window.addEventListener('online', () => this.flush());
}
this._registerBackgroundSync();
this.flush();
}
async _registerBackgroundSync() {
try {
if (typeof navigator === 'undefined' || !('serviceWorker' in navigator)) return;
const registration = await navigator.serviceWorker.ready;
if (registration.sync) {
await registration.sync.register(BACKGROUND_SYNC_TAG);
}
} catch (error) {
// Background Sync unsupported/unavailable — the online-event
// fallback in start() still covers this device.
console.warn('SpellBloc: background sync registration skipped', error);
}
}
async flush() {
if (this._flushing || !this.fetchImpl) return;
if (typeof navigator !== 'undefined' && navigator.onLine === false) return;
this._flushing = true;
try {
const pending = await this.store.getOutboxEvents();
if (pending.length === 0) {
this._retryDelay = MIN_RETRY_DELAY_MS;
return;
}
const response = await this.fetchImpl(this.endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ events: pending }),
});
if (!response.ok) {
throw new Error(`Sync endpoint responded with ${response.status}`);
}
await this.store.removeFromOutbox(pending.map((event) => event.id));
this._retryDelay = MIN_RETRY_DELAY_MS;
} catch (error) {
console.warn('SpellBloc: outbox flush failed, will retry', error);
this._scheduleRetry();
} finally {
this._flushing = false;
}
}
_scheduleRetry() {
if (this._retryTimer) return;
this._retryTimer = setTimeout(() => {
this._retryTimer = null;
this._retryDelay = Math.min(this._retryDelay * 2, MAX_RETRY_DELAY_MS);
this.flush();
}, this._retryDelay);
}
}
// ---------------------------------------------------------------------
// Auto-init: opens the store, runs the one-time migration, and starts
// the sync outbox. Runs independently of game.js — it does not read
// from or write to any of the variables/DOM game.js manages, so it
// cannot change current gameplay behavior.
// ---------------------------------------------------------------------
async function initSpellBlocOffline(options = {}) {
const store = new OfflineStore(options.storeOptions);
await store.migrateFromLocalStorage();
const outbox = new SyncOutbox(store, options.outboxOptions);
outbox.start();
return { store, outbox };
}
if (typeof document !== 'undefined' && typeof indexedDB !== 'undefined') {
document.addEventListener('DOMContentLoaded', () => {
initSpellBlocOffline()
.then((instance) => {
// Exposed for issue #6's game-loop integration and for debugging.
global.spellBlocOffline = instance;
})
.catch((error) => {
console.warn('SpellBloc: offline store init failed', error);
});
});
}
const api = {
createEvent,
dedupeById,
mergeEventLogs,
reduceEvents,
readLegacyLocalStorage,
buildMigrationEvents,
OfflineStore,
SyncOutbox,
initSpellBlocOffline,
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = api;
}
global.SpellBlocOfflineStore = api;
})(typeof window !== 'undefined' ? window : globalThis);