Injections
Sinks also support injections to its constructor, for better testing and other purpose.
Inject sink class
import { sink, state, effect } from 'redux-sink';
// shop sink to manage state of all products
@sink('shop')
export class ShopSink {
@state products = [];
@state loaded = false;
@effect
async function loadProducts() {
this.products = await fetch('http://api/products');
this.loaded = true;
}
}
// product sink to control current displaied product
// getting the data from ShopSink based on ShopSink.loaded
@sink('product', ShopSink)
export class ProductSink {
@state product;
constructor(shopSink) {
this.shopSink = shopSink;
}
@effect
async function loadProduct(id) {
if(!this.shopSink.loaded) {
await this.shopSink.loadProducts();
}
this.product = this.shopSink.products.find(x => x.id === id);
}
}Inject SinkFactory
Inject other objects
Last updated