Lint and improve comments
This commit is contained in:
parent
9d10e938a4
commit
e124a70def
|
@ -1,4 +1,4 @@
|
||||||
import { writable, derived } from "svelte/store"
|
import { writable } from "svelte/store"
|
||||||
import { computed } from "../utils/computed.js"
|
import { computed } from "../utils/computed.js"
|
||||||
|
|
||||||
const createDndStore = () => {
|
const createDndStore = () => {
|
||||||
|
|
|
@ -1,20 +1,22 @@
|
||||||
import { writable } from "svelte/store"
|
import { writable } from "svelte/store"
|
||||||
|
|
||||||
const getKey = value => {
|
/**
|
||||||
if (value == null || typeof value !== "object") {
|
* Extension of Svelte's built in "derived" stores, which the addition of deep
|
||||||
return value
|
* comparison of non-primitives. Falls back to using shallow comparison for
|
||||||
} else {
|
* primitive types to avoid performance penalties.
|
||||||
return JSON.stringify(value)
|
* Useful for instances where a deep comparison is cheaper than an additional
|
||||||
}
|
* store invalidation.
|
||||||
}
|
* @param store the store to observer
|
||||||
|
* @param deriveValue the derivation function
|
||||||
export const computed = (store, getValue) => {
|
* @returns {Writable<*>} a derived svelte store containing just the derived value
|
||||||
const initialValue = getValue(store)
|
*/
|
||||||
|
export const computed = (store, deriveValue) => {
|
||||||
|
const initialValue = deriveValue(store)
|
||||||
const computedStore = writable(initialValue)
|
const computedStore = writable(initialValue)
|
||||||
let lastKey = getKey(initialValue)
|
let lastKey = getKey(initialValue)
|
||||||
|
|
||||||
store.subscribe(state => {
|
store.subscribe(state => {
|
||||||
const value = getValue(state)
|
const value = deriveValue(state)
|
||||||
const key = getKey(value)
|
const key = getKey(value)
|
||||||
if (key !== lastKey) {
|
if (key !== lastKey) {
|
||||||
lastKey = key
|
lastKey = key
|
||||||
|
@ -24,3 +26,13 @@ export const computed = (store, getValue) => {
|
||||||
|
|
||||||
return computedStore
|
return computedStore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to serialise any value into a primitive which can be cheaply
|
||||||
|
// and shallowly compared
|
||||||
|
const getKey = value => {
|
||||||
|
if (value == null || typeof value !== "object") {
|
||||||
|
return value
|
||||||
|
} else {
|
||||||
|
return JSON.stringify(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue