I would like to find a way to get only one notification after an array sort
Is there a way? Thank you
const callback = function () {
console.log (...arguments)
}
const array = [2,1]
const handler = {
set (target, prop, value, receiver) {
callback (prop, value)
return Reflect.set (target, prop, value, receiver)
}
}
const proxy = new Proxy (array, handler)
proxy.sort()
// calls two times callback
// prints "0" 1 and "0" 2
// would like one notification : "array sorted"
You can use the
.apply()trap onArray#sort()itself:...or, if you want the proxy to affect your array only, you can return a custom function if the
propertybeing accessed is'sort':