angelnikolov / ts-cacheable

Observable/Promise Cache Decorator
https://npmjs.com/package/ts-cacheable
ISC License
340 stars 42 forks source link

changing contents of cached observable #12

Closed jmls closed 5 years ago

jmls commented 6 years ago

is it possible to change / emit new values from a cached call ?

Let's say I've loaded 40 items from an http call - and these are being displayed onscreen. Another session adds a new item - which notifies via websockets all interested clients.

How can I take this new item and insert it into the cache so my screen is updated withe the new record, without having to re-read the data from the http call ?

angelnikolov commented 6 years ago

Yes, you can use

@Cacheable({
    maxCacheCount: 40
})

. Let me know if that works for ya!

jmls commented 6 years ago

the 40 items are an array from a api call. I now have a new item that I want to insert into this array.

angelnikolov commented 6 years ago

Well can you do it @ the component level. Say you have

this.data = this.service.getCachedData() // this returns the initial forty items
socket.on('event', data=> this.data.push(data))

? If you want to update the cache to contain 41 items afterwards, I don't think this is possible right now, since the cache is not exposed out of the decorator and it's accessed by it via closure.

jmls commented 6 years ago

yeah, I was trying to keep it at the store level - I thought that this may be the case. At the component level won't work because the next component that makes the same api request will get the old 40 items, unless I bust the cache and re-read (Which seems a shame as I have to re-read 41 items, where I already have the 40 in memory)

Thanks

angelnikolov commented 6 years ago

Yeah I remember that I was thinking about a third decorator called @CacheUpdater, which will basically work the same way as the @CacheBuster (i.e share a notifier subject) but only work one-way and be used to update the cache of the @Cacheable but didn't have time for it. If you got time and want to do it, go ahead, I think it will be a nice addition! :))

angelnikolov commented 5 years ago

Closing due to inactivity.