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

29 lines
914 B

5 years ago
module.exports = (subscription, initialStats, onChange) => {
console.log("query_bingding");
5 years ago
let stats = [...initialStats]
const remove = value => {
5 years ago
console.log("remove");
5 years ago
stats = stats.filter(target => target.id !== value.id)
return onChange(stats)
}
const upsert = value => {
5 years ago
console.log("upsert");
5 years ago
let existed = false;
stats = stats.map(target => (target.id === value.id ? ((existed = true), value) : target))
if (!existed) stats = [...stats,value]
5 years ago
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)
}
}