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