farskid / farskid/localstorage-watcher

v2 RFC

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1
Forks
0
PR merge metrics
No merged PRs in 30d

Description

```js
window.localStorage.clear()

function getEntries(obj) {
return Object.entries(obj);
}

const toHashMap = entries => entries.reduce((t, c) => {
return {
...t,
[c[0]]: c[1]
}
}, {});

const hash = obj => encode(JSON.stringify(obj))

const encode = btoa;
const decode = atob;

function storageWatcher(duration = 1000, onChange = () => {}) {
let lastHash = hash(window.localStorage),
id;

const hasChanged = () => hash(window.localStorage) !== lastHash;

const check = () => {
if (hasChanged()) {
const entries = getEntries(window.localStorage);
const hashMap = toHashMap(entries)
lastHash = hash(window.localStorage);
onChange(hashMap)
}
}

return {
start() {
console.log('started')
id = setInterval(check, duration);
},
stop() {
console.log('stopped')
clearInterval(id);
}
}
}

let w = storageWatcher(1000, storage => {
console.log(`local storage changed`, storage)
});

w.start()

window.localStorage.setItem('a', 1);

setTimeout(() => window.localStorage.setItem('b', 2), 500);

setTimeout(() => window.localStorage.setItem('c', 3), 1500);

setTimeout(() => window.localStorage.removeItem('b'), 2500);

setTimeout(() => {
window.localStorage.removeItem('a')
window.localStorage.removeItem('c')

window.localStorage.setItem('c', 3)
window.localStorage.setItem('a', 1);

}, 1500);
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the JavaScript example in the issue, focusing on the storageWatcher entry point and its start, stop, and change-checking flow. The RFC does not identify project files, tests, or a defined acceptance condition, so clarify the v2 scope and expected behavior before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.