Few improvements

1) Passing previous value to `invalidate` callback.

```javascript
store$.subscribe(value => {
  console.log('new value', value);
}, value => {
  console.log('prev value', value);
});
```

2) Custom equality checking callback passed as the third argument of `writable`. Very useful then store value is object or array.

```
const user$ = writable({}, undefined, (value, newValue) => {
  return value.name !== newValue.name;
});
```
pull/3575/head
PaulMaly 6 years ago committed by GitHub
parent 14a46a17d0
commit df8f686fc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,6 +12,9 @@ type Updater<T> = (value: T) => T;
/** Cleanup logic callback. */
type Invalidator<T> = (value?: T) => void;
/** Equal logic callback. */
type Comparator<T> = (value: T, newValue: T) => boolean;
/** Start and stop notification callbacks. */
type StartStopNotifier<T> = (set: Subscriber<T>) => Unsubscriber | void;
@ -61,18 +64,19 @@ export function readable<T>(value: T, start: StartStopNotifier<T>): Readable<T>
* @param {*=}value initial value
* @param {StartStopNotifier=}start start and stop notifications for subscriptions
*/
export function writable<T>(value: T, start: StartStopNotifier<T> = noop): Writable<T> {
export function writable<T>(value: T, start: StartStopNotifier<T> = noop, is_changed: Comparator<T> = safe_not_equal): Writable<T> {
let stop: Unsubscriber;
const subscribers: Array<SubscribeInvalidateTuple<T>> = [];
function set(new_value: T): void {
if (safe_not_equal(value, new_value)) {
if (is_changed(value, new_value)) {
let prev_value = value;
value = new_value;
if (stop) { // store is ready
const run_queue = !subscriber_queue.length;
for (let i = 0; i < subscribers.length; i += 1) {
const s = subscribers[i];
s[1]();
s[1](prev_value);
subscriber_queue.push(s, value);
}
if (run_queue) {

Loading…
Cancel
Save