Compare commits

..

No commits in common. "c5c4e973a5c3c312da8c546bcc1402665f296033" and "5a29b0e662ddbb006918a957815fede5dc506f65" have entirely different histories.

2 changed files with 20 additions and 33 deletions

View file

@ -1,6 +1,6 @@
{
"name": "skooma",
"version": "1.7.0",
"version": "1.6.1",
"author": "darkwiiplayer",
"license": "Unlicense",
"main": "skooma.js",

View file

@ -60,12 +60,12 @@ export class State extends SimpleState {
this.#options = options
this.#target = target
this.values = new Proxy(target, {
this.proxy = new Proxy(target, {
set: (_target, prop, value) => {
const old = this.get(prop)
if (old !== value) {
this.emit(prop, value)
if (this.#options.shallow) {
if (this.#options.deep !== false) {
if (State.isState(old)) this.disown(prop, old)
if (State.isState(value)) this.adopt(prop, value)
}
@ -90,8 +90,8 @@ export class State extends SimpleState {
}
// When you only need one value, you can skip the proxy.
set value(value) { this.values.value = value }
get value() { return this.values.value }
set value(value) { this.proxy.value = value }
get value() { return this.proxy.value }
adopt(prop, state) {
let handlers = this.#nested.get(state)
@ -146,33 +146,13 @@ export class State extends SimpleState {
}
}
set(...args) {
if (args.length === 1) return this.set("value", ...args)
const [prop, value] = args
set(prop, value) {
this.#target[prop] = value
}
get(...args) {
if (args.length === 0) return this.get("value")
const prop = args[0]
get(prop) {
return this.#target[prop]
}
subscribe(prop, callback) {
if (!callback) return this.subscribe("value", prop)
const controller = new AbortController()
this.addEventListener("change", ({final}) => {
if (final.has(prop)) return callback(final.get(prop))
}, {signal: controller.signal})
callback(this.value)
return () => controller.abort()
}
// Backwards compatibility
get proxy() { return this.values }
}
const forwardFinalizationRegistry = new FinalizationRegistry(([cache, name]) => {
@ -205,8 +185,8 @@ export class ForwardState extends SimpleState {
}, {signal: abortController.signal})
}
get value() { return this.#backend.values[this.#property] ?? this.#fallback }
set value(value) { this.#backend.values[this.#property] = value }
get value() { return this.#backend.proxy[this.#property] ?? this.#fallback }
set value(value) { this.#backend.proxy[this.#property] = value }
}
class StorageChangeEvent extends Event {
@ -221,13 +201,16 @@ class StorageChangeEvent extends Event {
export class StoredState extends State {
#storage
#valueKey
constructor(init, options={}) {
super({}, options)
this.#storage = options.storage ?? localStorage ?? new MapStorage()
this.#valueKey = options.key ?? 'value'
// Initialise storage from defaults
for (const [prop, value] of Object.entries(init)) {
for (let [prop, value] of Object.entries(init)) {
if (prop === 'value') prop = this.#valueKey
if (this.#storage[prop] === undefined)
this.set(prop, value)
}
@ -243,7 +226,9 @@ export class StoredState extends State {
// Listen for changes from other windows
const handler = event => {
if (event.targetState !== this && event.storageArea == this.#storage) {
this.emit(event.key, JSON.parse(event.newValue))
let prop = event.key
if (prop === this.#valueKey) prop = 'value'
this.emit(prop, JSON.parse(event.newValue))
}
}
addEventListener("storage", handler)
@ -251,12 +236,14 @@ export class StoredState extends State {
}
set(prop, value) {
if (prop == "value") prop = this.#valueKey
const json = JSON.stringify(value)
dispatchEvent(new StorageChangeEvent(this.#storage, prop, json, this))
this.#storage[prop] = json
}
get(prop) {
if (prop == "value") prop = this.#valueKey
const value = this.#storage[prop]
return value && JSON.parse(value)
}
@ -267,8 +254,8 @@ const attributeObserver = new MutationObserver(mutations => {
if (type == "attributes") {
const next = target.getAttribute(name)
const camelName = kebabToCamel(name)
if (String(target.state.values[camelName]) !== next)
target.state.values[camelName] = next
if (String(target.state.proxy[camelName]) !== next)
target.state.proxy[camelName] = next
}
}
})