Skip to content

Commit

Permalink
Merge pull request #9 from victorqribeiro/victor/feature/add-state
Browse files Browse the repository at this point in the history
[feat] add createState function
  • Loading branch information
victorqribeiro authored Jan 26, 2025
2 parents 33fb417 + 41f1707 commit 251867e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tiny.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@
})
window['$'] = selector => document.querySelector(selector)
window['$$'] = selector => Array.from(document.querySelectorAll(selector))
window['createState'] = state => {
const appState = { ...state }
appState._updates = Object.fromEntries(Object.keys(state).map(s => [s, []]))
appState._update = s => appState._updates[s].forEach(u => u())
appState.addUpdate = (s, u) => appState._updates[s].push(u)
return new Proxy(appState, {set(o, p, v) {o[p] = v; o._update(p); return true}})
}
})()

0 comments on commit 251867e

Please # to comment.