Speed up memo stores

This commit is contained in:
Andrew Kingston 2024-08-12 09:55:12 +01:00
parent 304c244943
commit d77e4381cc
No known key found for this signature in database
1 changed files with 8 additions and 17 deletions

View File

@ -4,32 +4,23 @@ import { writable, get, derived } from "svelte/store"
// subscribed children will only fire when a new value is actually set
export const memo = initialValue => {
const store = writable(initialValue)
let currentJSON = null
const tryUpdateValue = (newValue, currentValue) => {
// Sanity check for primitive equality
if (currentValue === newValue) {
return
}
// Otherwise deep compare via JSON stringify
const currentString = JSON.stringify(currentValue)
const newString = JSON.stringify(newValue)
if (currentString !== newString) {
const tryUpdateValue = newValue => {
const newJSON = JSON.stringify(newValue)
if (newJSON !== currentJSON) {
store.set(newValue)
currentJSON = newJSON
}
}
return {
subscribe: store.subscribe,
set: newValue => {
const currentValue = get(store)
tryUpdateValue(newValue, currentValue)
},
set: tryUpdateValue,
update: updateFn => {
const currentValue = get(store)
let mutableCurrentValue = JSON.parse(JSON.stringify(currentValue))
let mutableCurrentValue = JSON.parse(currentJSON)
const newValue = updateFn(mutableCurrentValue)
tryUpdateValue(newValue, currentValue)
tryUpdateValue(newValue)
},
}
}