-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal improvement: detach listener and attach #10
Comments
betula
changed the title
fix(0.9.1): unsubscribe and attach listener should mutate array of listeners
fix(0.9.1): unsubscribe listener should mutate array of listeners
Aug 11, 2022
The example above has an error. If I at first add a new listener, and at second remove the old one. module.exports = {
event: () => {
let ev = (data) => {
ev[1] ++;
try {
for (ev[3] = 0; i < ev[0].length; ev[3]++)
ev[0][ev[3]](data)
}
finally {
ev[1]--;
if (!ev[1] && ev[2].length) {
ev[0] = ev[0].concat(ev[2]);
ev[2].length = 0;
}
}
}
ev[0] = []
ev[1] = 0 // listen phase
ev[2] = [] // new listeners
ev[3] = 0 // current iteration index
return ev
},
listen: (ev, fn) => (
ev[0].indexOf(fn) < 0 && (ev[1] ? (ev[2].indexOf(fn) < 0 && ev[2].push(fn)) : ev[0].push(fn)),
() => {
let i = ev[0].indexOf(fn)
i > -1 && (ev[1] && i <= ev[3] && ev[3]--, ev[0].splice(i, 1))
i = ev[2].indexOf(fn)
i > -1 && ev[2].splice(i, 1)
}
)
} I should check its performance. Compare it with the original algorithm. We should understand how much we will miss. |
betula
changed the title
fix(0.9.1): unsubscribe listener should mutate array of listeners
proposal improvement: detach listener and attach
Aug 11, 2022
50% - 100% slower Lines 6 to 17 in 6c9f985
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reason:
The text was updated successfully, but these errors were encountered: