You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pk8f3pmu2/lib/live-query-binding.js

26 lines
828 B

module.exports = (subscription, initialStats, onChange) => {
let stats = [...initialStats]
const remove = value => {
stats = stats.filter(target => target.id !== value.id)
return onChange(stats)
}
const upsert = value => {
let existed = false;
stats = stats.map(target => (target.id === value.id ? ((existed = true), value) : target))
if (!existed) stats = [value, ...stats]
return onChange(stats)
}
subscription.on('create', upsert)
subscription.on('update', upsert)
subscription.on('enter', upsert)
subscription.on('leave', remove)
subscription.on('delete', remove)
return () => {
subscription.off('create', upsert)
subscription.off('update', upsert)
subscription.off('enter', upsert)
subscription.off('leave', remove)
subscription.off('delete', remove)
}
}