tejacques / crosstab

A utility library for cross-tab communication using localStorage.
Apache License 2.0
366 stars 58 forks source link

Writing "null" to LocalStorage causes a "Cannot read property 'id' of null" error to be thrown #40

Closed andrewwakeling closed 9 years ago

andrewwakeling commented 9 years ago

In onStorageEvent, it is optimistically parsing values as JSON and then assuming that the result is an object. This causes "eventValue" to be "null" and then "eventValue.id" to fail.

There are different ways of solving this, but I would have thought that crosstab should be checking the "key" property of all incoming StorageEvents and only processing ones that are relevant to crosstab.

Is using:

if (event.key.indexOf('crosstab') !== 0) return;

a possible fix?

tejacques commented 9 years ago

This is a great catch. I think checking the key is the right way to go, but it's probably a little more robust to do something like this:

// setup
util.storageEventKeys = {};
util.forEach(util.keys, function(key) {
    util.storageEventKeys[key] = 1;
});
// check
if (!event.key in util.storageEventKeys) {
    // Not a crosstab event
    return;
}