22 lines
497 B
TypeScript
22 lines
497 B
TypeScript
import type { Product } from '@/models/products'
|
|||
|
|
|
||
|
|
type ProductListHandler = (products: Product[]) => void
|
||
|
|
|
||
|
|
const handlers = new Set<ProductListHandler>()
|
||
|
|
|
||
|
|
export function subscribeProductListStream(handler: ProductListHandler) {
|
||
|
|
handlers.add(handler)
|
||
|
|
|
||
|
|
return () => {
|
||
|
|
handlers.delete(handler)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function applyProductsListFromStream(products: Product[]) {
|
||
|
|
if (!Array.isArray(products) || !products.length) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
handlers.forEach((handler) => handler(products))
|
||
|
|
}
|