lilyball / Tomorrowland

Lightweight Promises for Swift & Obj-C

Home Page:https://lilyball.github.io/Tomorrowland/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PromiseInvalidationTokenBox should clean up the callback list when possible

lilyball opened this issue · comments

When enqueuing a new callback onto a PromiseInvalidationTokenBox, it should walk the list and throw away any callbacks it finds whose cancellable has been nilled out.

There are two reasonable approaches I see:

  1. Walk the entire list, throw away any nilled nodes, and build a brand new list.
  2. Walk the list, but stop at the first non-nilled cancellable. This means we don't need to rebuild any existing nodes, we're just popping off dead nodes.

The first one is much more expensive than the second, but the second one has the downside where if you're always pushing live nodes onto the front, you can never clean up dead nodes on the tail. The second approach is also a lot simpler.

We can mitigate some of the expense of the first approach by only cleaning up the list after pushing N nodes on.

I'm inclined to go with the second approach, unless someone comes up with a real-world practical use case where the first is important.

The idea here is to help with the scenario where a PromiseInvalidationToken is used long-term just as a way to cancel outstanding requests when the owning object deinits, without otherwise invalidating the token.