Skip to content

LocalStorage Events

W. "Mac" McMeans edited this page Feb 21, 2022 · 32 revisions

Native localStorage will not listen for change events fired from the same tab/page making the change, but fires only when the "storage area has been modified in the context of another document." This built-in limitation can be debilitating, but localDataStorage overcomes it.

By dispatching custom events, localDataStorage fires on storage key value changes, such as those made by the set, safeset, chopget or remove methods. The event returns an activity timestamp and message, as well as expected details about the affected key name with its old and new values. The old and new key value data types are also reported.

The following code shows everything the event reports:

// the function to call to catch emitted data
function nowICanSeeLocalStorageChangeEvents( e ) {
    console.log(
        "subscriber: "    + e.currentTarget.nodeName + "\n" +
        "timestamp: "     + e.detail.timestamp + " (" + new Date( e.detail.timestamp ) + ")" + "\n" +
        "prefix: "        + e.detail.prefix  + "\n" +
        "message: "       + e.detail.message + "\n" +
        "method: "        + e.detail.method  + "\n" +
        "key: "           + e.detail.key     + "\n" +
        "old value: "     + e.detail.oldval  + "\n" +
        "new value: "     + e.detail.newval  + "\n" +
        "old data type: " + e.detail.oldtype + "\n" +
        "new data type: " + e.detail.newtype
    );
};

// attach an event listener to our custom event
document.addEventListener(
    "localDataStorage"
    , nowICanSeeLocalStorageChangeEvents
    , false
);

The following methods may trigger a change event:
chopget
clear
forceset
rename
safeset
softset
remove

localStorage Keys

The usual suspects:

set / get      clear      key      remove

The esoteric ones:

Array Keys:
push / pull, pullall      poke      contains      where

Broadcasting:
broadcast

Bypass:
forceset / forceget

Data Transfer:
import / export

Duplicates:
countdupes, showdupes, listdupes

Internals:
cancrunch      crunch / uncrunch

shufflestring / unshufflestring

xorstring

Management:
keys

Memory Consumption:

Memory Quota:
showquota

Query:
haskey, hasval, hastype

Security:
safeset / safeget

setscramblekey / getscramblekey

Type Check:
isarray      isbigint      isboolean      iscrunch

isdate      isfloat      isinteger      isnull

isnumber      isobject      isstring

showtype

Utility:
chopget      copy      softset      rename

Properties:

channel      length      quota      version

Settings:

verbosity

Memory Keys

Standard:

_set / _get      _clear      _key      _remove

Unconventional:

Data Sync:
_backup / _restore

Management:
_keys

Security:
_safeset / _safeget

Type Check:
_isarray      _isbigint      _isboolean      _iscrunch

_isdate      _isfloat      _isinteger      _isnull

_isnumber      _isobject      _isstring

_showtype

Utility:
_chopget      _copy      _softset      _rename

Clone this wiki locally