mkuklis/depot.js — pull request #8 by jdbartlett (2013-07-29 02:39:44+00:00)
Title: Fix persistence across multiple tabs / windows / instances

localStorage and sessionStorage are expected to persist across multiple tabs and windows; e.g., once you `localStorage.setItem('foo', 'bar')` in Tab A, `localStorage.getItem('foo') === 'bar'` should immediately be true in Tab B.

depot.js noticeably breaks this expectation when a new record is created. i.e., if a store is instantiated in Tab A and in Tab B, Tab B's store object will not be aware of records subsequently created in Tab A:

1. Tab A: `var todoStore = depot('todos');`
2. Tab B: `var todoStore = depot('todos');`
3. Tab A: `todoStore.save({ title: "todo1" });`
4. Tab B: `todoStore.all()` returns empty array. :(

This happens because each store object's `.ids` array is not kept up-to-date with changes made elsewhere. This leads to data loss and other misbehavior when end users have multiple tabs open.

This issue is avoided by having vulnerable methods (like `.all()`) check for updates to the ids list before returning new values.

This change does not affect the public-facing API.

Tests have been added to demonstrate the issue and fix.