Improved store helper
Now, when using subscriptions outside of a component, you can use abortController to unsubscribe.
- It should return 'abortController.signal' or any value. If the subscription function returns no value, the subscription is canceled immediately.
const subscribe = store<{ count1: number; count2: number; count3: number }>({
count1: 1,
count2: 1,
count3: 1,
});
const abortControl = new AbortController();
// abortControl.abort();
const proxyFirst = subscribe(
store => {
console.log(store.count1);
return abortControl.signal; // If it doesn't return 'true' etc, the subscription will be cancelled.
},
// The property you want to subscribe to. If omitted, all items will be subscribed.
store => [store.count1, store.count2]
);
If the initial value is not an object, it is automatically expanded to {value: value}
.
const subscribe = store<number>(0);
const abortControl = new AbortController();
const proxyFirst = subscribe(store => {
console.log(store); // {value: 0}
return abortControl.signal; // If it doesn't return 'true' etc, the subscription will be cancelled.
});
Fixed
Additionally, fixed a bug that did not work as desired even if a value was added to the subscription dependency.