Add deriveDeduplicatedByValue
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import {get, derived, Readable, Unsubscriber, Writable, Subscriber} from "svelte/store"
|
||||
import {memoize, throttle} from "@welshman/lib"
|
||||
import {memoize, equals, throttle} from "@welshman/lib"
|
||||
|
||||
// Define Stores and StoresValues types locally since they're not exported in Svelte 5
|
||||
type Stores = Readable<any> | [Readable<any>, ...Array<Readable<any>>] | Array<Readable<any>>
|
||||
@@ -93,3 +93,19 @@ export const deriveDeduplicated = <S extends Stores, T>(
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const deriveDeduplicatedByValue = <S extends Stores, T>(
|
||||
stores: S,
|
||||
get: (storeValues: StoresValues<S>) => T,
|
||||
): Readable<T> => {
|
||||
let prev: T
|
||||
|
||||
return derived(stores, (storeValues, set) => {
|
||||
const result = get(storeValues)
|
||||
|
||||
if (!equals(prev, result)) {
|
||||
prev = result
|
||||
set(result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user