0

This question is about HTML5 offline apps. This answer is from 2009 about regular web pages.

I have a heavyweight HTML5 PouchDB offline app. It's an AngularJS app, that writes to/from a PouchDB.

The app needs to handle multiple tabs without help from the server.

If a user opens my app in two tabs, they'll create a race condition.

1. tab1 opens docA and edits
2. tab2 opens docA and edits
3. tab1 saves docA
4. tab2 saves docA (overwriting tab1's changes)

My app needs to gracefully handle this, but how?

Some ideas:

  1. Prevent multiple tabs of the same app.
    • I haven't found a graceful solution for this.
  2. Get change notifications from PouchDB from onChange
    • Will onChange fire in another tab?
    • This doesn't solve the problem, just exposes it sooner.
  3. Use different PouchDB's for each tab, sync between them.
    • Doesn't solve the problem, just makes it a Sync problem.
    • Seems like alot of memory.
  4. Document Locking
    • Complex onBeforeUnload
    • How to recover from tab crashes that don't unload correctly?
  5. Browser-tab IPC.
    • When opening a doc, ask other tabs if they have it open.
    • The best I've found is using localStorage
  6. Something I haven't thought of yet.

Any suggestions?

Community
  • 1
  • 1
Michael Cole
  • 15,473
  • 7
  • 79
  • 96
  • 1
    How about PouchDB in SharedWorker? – ermouth Feb 16 '15 at 05:26
  • 1
    The race condition you describe wouldn't result in corrupt data, just a conflict on the record. If you already have conflict resolution setup, the conflict resulting from the race condition should already be handled as a part of the normal conflict resolution. – Scott Swezey Aug 19 '17 at 22:45

1 Answers1

1

That's weird, I thought that IndexedDB handled that automatically under the hood. You are using IndexedDB, yes? Are you seeing this in many browsers, or just Chrome?

Ideally this wouldn't be something that you would fix in your application code; it would be something handled by PouchDB or by the browser implementation of IndexedDB/WebSQL. If you have a reproducible test case, it would be nice to file an issue on PouchDB. :)

nlawson
  • 11,510
  • 4
  • 40
  • 50
  • Hi, if I get a test case I will. I'm trying to understand how to changes in one docs move tab-> AngularJS -> PouchDB -> browser storage -> tab -> PouchDB -> AngularJS – Michael Cole Feb 16 '15 at 05:44