dmonad / lib0

Monorepo of isomorphic utility functions
MIT License
345 stars 62 forks source link

Please document that Observable.once does not respect Observable.off #86

Open Flamenco opened 6 months ago

Flamenco commented 6 months ago

Describe the bug A clear and concise description of what the bug is.

Observable.once creates a wrapper around the function, so calling Observable.off does not prevent it from being called.

let fn = ()=>{};
item.once('update',  fn);
item.off('update',  fn);

fn, will still be invoked under this scenario.

This seems to by design, so it should be documented that off will not prevent once from being called.

Flamenco commented 6 months ago

As a workaround, the callback function can use on and then remove itself when invoked, in order to be both callable once, and also removable.

let fn = ()=>{
  item.off('update',  fn);
};

item.on('update',  fn);
item.off('update',  fn);