svelte-writable-derived
Advanced tools
Comparing version 2.1.5 to 3.0.0
@@ -16,5 +16,4 @@ import type { Readable, Writable } from "svelte/store"; | ||
/** Values sent to origin stores. */ | ||
type SetValues<T> = T extends MinimalWritable<infer U> ? U : any[]; | ||
// See this discussion for why the array form is underspecified: | ||
// https://github.com/PixievoltNo1/svelte-writable-derived/issues/19 | ||
type SetValues<T> = T extends MinimalWritable<infer U> ? U : | ||
{ [K in keyof T]?: T[K] extends MinimalWritable<infer U> ? U : never }; | ||
@@ -41,3 +40,3 @@ /** | ||
derive: (values: StoresValues<S>) => T, | ||
reflect: (reflecting: T) => SetValues<S>, | ||
reflect: (reflecting: T, old: StoresValues<S>) => SetValues<S>, | ||
initial?: T | ||
@@ -49,54 +48,6 @@ ): Writable<T>; | ||
derive: (values: StoresValues<S>, set: (value: T) => void) => void, | ||
reflect: (reflecting: T) => SetValues<S>, | ||
reflect: (reflecting: T, old: StoresValues<S>) => SetValues<S>, | ||
initial?: T | ||
): Writable<T>; | ||
export default function writableDerived<S extends Stores, T>( | ||
origins: S, | ||
derive: (values: StoresValues<S>) => T, | ||
reflect: (reflecting: T, set: (value: SetValues<S>) => void) => ( () => void ) | void, | ||
initial?: T | ||
): Writable<T>; | ||
export default function writableDerived<S extends Stores, T>( | ||
origins: S, | ||
derive: (values: StoresValues<S>, set: (value: T) => void) => void, | ||
reflect: (reflecting: T, set: (value: SetValues<S>) => void) => ( () => void ) | void, | ||
initial?: T | ||
): Writable<T>; | ||
export default function writableDerived<S extends Stores, T>( | ||
origins: S, | ||
derive: (values: StoresValues<S>) => T, | ||
reflect: { withOld: (reflecting: T, old: StoresValues<S>) => SetValues<S> }, | ||
initial?: T | ||
): Writable<T>; | ||
export default function writableDerived<S extends Stores, T>( | ||
origins: S, | ||
derive: (values: StoresValues<S>, set: (value: T) => void) => void, | ||
reflect: { withOld: (reflecting: T, old: StoresValues<S>) => SetValues<S> }, | ||
initial?: T | ||
): Writable<T>; | ||
export default function writableDerived<S extends Stores, T>( | ||
origins: S, | ||
derive: (values: StoresValues<S>) => T, | ||
reflect: { | ||
withOld: (reflecting: T, old: StoresValues<S>, set: (value: SetValues<S>) => void) | ||
=> ( () => void ) | void | ||
}, | ||
initial?: T | ||
): Writable<T>; | ||
export default function writableDerived<S extends Stores, T>( | ||
origins: S, | ||
derive: (values: StoresValues<S>, set: (value: T) => void) => void, | ||
reflect: { | ||
withOld: (reflecting: T, old: StoresValues<S>, set: (value: SetValues<S>) => void) | ||
=> ( () => void ) | void | ||
}, | ||
initial?: T | ||
): Writable<T>; | ||
export { writableDerived }; | ||
@@ -103,0 +54,0 @@ |
{ | ||
"name": "svelte-writable-derived", | ||
"version": "2.1.5", | ||
"version": "3.0.0", | ||
"description": "Two-way data-transforming stores for Svelte", | ||
@@ -8,2 +8,3 @@ "keywords": [ | ||
"svelte3", | ||
"derived", | ||
"store" | ||
@@ -18,3 +19,3 @@ ], | ||
"repository": "github:PixievoltNo1/svelte-writable-derived", | ||
"funding": "https://sponsus.org/u/pixievoltno1", | ||
"funding": "https://ko-fi.com/pixievoltno1", | ||
"module": "index.mjs", | ||
@@ -43,5 +44,5 @@ "types": "index.d.ts", | ||
"mocha": "^9.2.2", | ||
"svelte": "^3.46.4" | ||
"svelte": "^3.55.0" | ||
}, | ||
"license": "MIT" | ||
} |
# svelte-writable-derived | ||
[![npm](https://img.shields.io/npm/v/svelte-writable-derived.svg)](https://www.npmjs.com/package/svelte-writable-derived) [![Bundle size](https://img.shields.io/bundlephobia/minzip/svelte-writable-derived.svg)](https://bundlephobia.com/result?p=svelte-writable-derived) [![License](https://img.shields.io/github/license/PixievoltNo1/svelte-writable-derived.svg)](https://github.com/PixievoltNo1/svelte-writable-derived/blob/master/LICENSE.txt) ![GitHub Repo stars](https://img.shields.io/github/stars/PixievoltNo1/svelte-writable-derived?style=social) | ||
[![npm](https://img.shields.io/npm/v/svelte-writable-derived.svg)](https://www.npmjs.com/package/svelte-writable-derived) [![Bundle size](https://img.shields.io/bundlephobia/minzip/svelte-writable-derived.svg)](https://bundlephobia.com/result?p=svelte-writable-derived) ![GitHub CI Status](https://img.shields.io/github/actions/workflow/status/PixievoltNo1/svelte-writable-derived/node.js.yml?branch=master&label=tests) [![License](https://img.shields.io/github/license/PixievoltNo1/svelte-writable-derived.svg)](https://github.com/PixievoltNo1/svelte-writable-derived/blob/master/LICENSE.txt) ![GitHub Repo stars](https://img.shields.io/github/stars/PixievoltNo1/svelte-writable-derived?style=social) | ||
@@ -38,18 +38,14 @@ For users of [Svelte](https://svelte.dev/) v3, this is a read-write variant of Svelte's [derived stores](https://svelte.dev/tutorial/derived-stores) that accepts an extra callback to send values back to the source. It builds on the derived & writable stores provided by Svelte, and emulates their behavior as closely as possible. | ||
It is not possible for `derived` and `reflect` to trigger calls to each other, provided they only use the `set` callbacks provided to them and do not reach out to any outer `set` or `update`. | ||
As long as `derived` and `reflect` set stores only by the means provided to them and not via any store's methods, they won't trigger calls to each other. | ||
### New parameter: `reflect` | ||
<i>One of the following:</i> | ||
* <i>Function with parameters: `reflecting` (any), optional `set` (function)</i> | ||
* <i>Object with property `withOld` containing function with parameters: `reflecting` (any), `old` (any), optional `set` (function)</i> | ||
<i>Function with parameters: `reflecting` (any), optional `old` (any)</i> | ||
The provided function is called when the derived store gets a new value via its `set` and `update` methods (not via the `derive` callback). Its `reflecting` parameter is this new value. The `set` parameter accepts a value to set in the origin store, if `origins` was a store, or an array of values to set if `origins` was an array. If the `set` parameter receives an array that's sparse or shorter than `origins`, it will only set the stores it has elements for, and other stores don't necessarily need to be writable. If the function doesn't take a `set` parameter, its return value will be used to set origin stores just as if it were passed to `set`. | ||
This function is called when the derived store gets a new value via its `set` and `update` methods (not via the `derive` callback). Its `reflecting` parameter is this new value, and `old` is the origin store's current value, or an array of values if `origins` is an array. It must return a value to set in the origin store, or an array of values to set if `origins` was an array. If the returned array is sparse or shorter than `origins`, it will only set the stores it has elements for, and other stores don't necessarily need to be writable. | ||
`reflect` is called after the derived store's subscriptions are called. If the derived store has its `set` and/or `update` methods called again in the process of calling its subscriptions, `reflect` will be called only once, with the most-recently-set value. | ||
If `reflect` takes a `set` parameter, it may return a cleanup function that will be called immediately before the next `reflect` call. (Unlike its `derive` counterpart, `reflect`'s cleanup function is never called in response to unsubscriptions.) | ||
`arguments` and rest parameters will not receive `old` unless the function's [length](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length) is at least 2. | ||
If the `reflect` parameter is provided a function via an object with a `withOld` property, that function will be called with an additional `old` parameter after `reflecting`. This is the initial value of the origin stores, and will be an array if `origins` was an array. | ||
## Named export: `propertyStore()` | ||
@@ -233,3 +229,3 @@ | ||
You can make a one-time donation or become an ongoing sponsor at [my Sponsus page](https://sponsus.org/u/pixievoltno1), and sponsors can ask me to prioritize development of this package. | ||
At [my Ko-Fi page](https://ko-fi.com/pixievoltno1), you can make a one-time or monthly donation, or [commission work on an issue](https://ko-fi.com/pixievoltno1/commissions). | ||
@@ -236,0 +232,0 @@ ## 💌 ... with kind words |
Sorry, the diff of this file is not supported yet
21610
214
236