haxetink / tink_core

Core utilities
https://haxetink.github.io/tink_core
MIT License
117 stars 33 forks source link

Add Promise.cache #80

Closed kevinresol closed 6 years ago

kevinresol commented 6 years ago

Promise.cache returns a function which when called:

Sample use:

var example = Promise.cache(fetchExampleDotCom, triggerNoiseAfterDelayMs(1000));
example(); // will create a new promise that fetches example.com
example(); // will return the same promise (i.e. won't fetch again)

// after 1000ms
example(); // will fetch example.com again
back2dos commented 6 years ago

Hmm, I wonder if it's might be better to do it this way:

function cache<T>(gen:Void->Promise<Pair<T, Future<Noise>>):Void->Promise<T> { ... }

Mostly because otherwise if expiration should be somehow tied to data, you have to share state between the functions somehow. But I'm just thinking out loud.

kevinresol commented 6 years ago

Updated, also allowed sync expire

kevinresol commented 6 years ago

Well Java choked on this:

src/tink/core/_Promise/Promise_Impl_.java:309: error: generic array creation
        tink.core._Future.FutureObject<tink.core.Outcome>[] p = ( (tink.core._Future.FutureObject<tink.core.Outcome>[]) (new tink.core._Future.FutureObject<tink.core.Outcome>[] {null}) );
                                                                                                                         ^
back2dos commented 6 years ago

Gotta love hxjava \o/