projectfluent / cached-iterable

Iterables which cache the values they yield
6 stars 2 forks source link

Add a way to keep state when creaing CachedIterable out of another CachedIterable #2

Closed stasm closed 6 years ago

stasm commented 6 years ago

In some cases, users might want to create a Cached*Iterable themselves and pass it into APIs which use Cached*Iterable behind the scenes. For instance, in fluent-react the Localization class wraps the iterable of messages in a CachedAsyncIterable. At the same time, the user of fluent-react might want to prefetch some contexts from that iterable before handing it off to Localization.

stasm commented 6 years ago

After a bit more thinking about this, I think it's best to not tinker with the constructor logic. It's not something the builtin objects like Array do.

Instead I suggest we add a static from() method which takes an iterable, in which case it behaves the same as the constructor, or a CachedIterable, in which case it just returns the same CachedIterable it was passed, thus keeping its cached state.