liveview-native / liveview-client-swiftui

MIT License
349 stars 30 forks source link

Pattern/abstraction for caching data that originates from the server on the client #1013

Open AZholtkevych opened 11 months ago

AZholtkevych commented 11 months ago

We have to have an established pattern or abstraction for caching data that originates from the server on the client, particularly for session tokens and the like? I know it’s possible to do this by messing about with the custom registry but I feel like its a common enough use case that we should have a solution for it.

AZholtkevych commented 11 months ago

Link to the discussion: https://dockyard.slack.com/team/U03GBMZ68JE

Entire discussion:

May Matyi 1:35 PM @Carson Katri Do we have an established pattern or abstraction for caching data that originates from the server on the client, particularly for session tokens and the like? I know it’s possible to do this through messing about with the custom registry but I feel like its a common enough use case that we should have a solution for it. 1:37 If possible, it would be nice if this could be handled by core so that its available for all platforms. I’m imagining an API that gets exposed via the platform’s FFI bridge that has put / get / delete methods for storing basic values like strings so that they’re available if you quit and relaunch the app.

bcardarella 3:10 PM Anil did this for the original chat app and the TODO app 3:10 @May Matyi I don't know if he established a pattern or just found a place to stash 3:10 but maybe the Registry should do this?

May Matyi 3:11 PM Yeah, its possible with the custom registry but kind of a pain point IMO (edited) 3:11 I think for such a common use case we should have a formal solution or an add on lib right?

bcardarella 3:11 PM I agree we probably should have a pattern for this 3:11 but I defer to @Carson Katri on the "how"

May Matyi 3:16 PM I do think having an underlying representation of clientside state (i.e. in liveview-native-core) is worth considering though as this is something that every platform will need to figure out.

bcardarella 3:16 PM yeah, even LiveView web allows for this 3:16 there are certainly things that don't make sense to have as a server state (edited) 3:17 but what I wouldn't want is for people to just use LiveView Native as a api-less thing to send structured information over the wire for storage and client side handling 3:17 it's a fine line

May Matyi 3:18 PM mhm, I think we should just start with primitive values like strings and ints; especially if we’re handling this in core to simplify the FFI path between Rust and whatever language the linked platform library is using

bcardarella 3:19 PM yeah things like you're referring to for auth state I assume?

May Matyi 3:19 PM correct, the main use case I have in mind is storing a session token so that when I close and open the app it preserves my login state

bcardarella 3:20 PM yeah that's completely reasonable... if you are blocked by this I suggest just stashing somewhere you can get it for now and when Carson is back tomorrow we can chat about a better long-term API :+1: 1

3:20 we should open an issue for this @alex.zholtkevych

alex.zholtkevych 3:22 PM On it

AZholtkevych commented 9 months ago

We have an issue in Core regarding that https://github.com/liveview-native/liveview-native-core/issues/37, but should we solve it here?

matthieuchabert commented 8 months ago

I came across the issue you're mentioning. If the user closes the app after being logged in, he will have to login again which is a non-ideal situation.

Do you have a pattern for this? I can't find any documentation about this issue. Or how would I go with a custom registry?

Thank you 🙏

bcardarella commented 8 months ago

@matthieuchabert nothing out of the box just yet but there is a workaround solution in the ElixirConf Chat app if you want to dig through there. @supernintendo can probably point out where it is happening.

In the future we are looking to add a localStorage type API to LiveView Native Core so you will be able to push and request data cached within the client. At that point if you want to restore state you should follow a pattern for doing so in LiveView like discussed in this article: https://fly.io/phoenix-files/restore-liveview-state-on-startup/

bcardarella commented 8 months ago

Sorry I didn't mean to close

matthieuchabert commented 8 months ago

@bcardarella Thanks for your answer, I'll dig through the ElixirConf chat app.

Yes we're already using localStorage to restore some data when reconnecting a Liveview! Looking forward to your implementation with LiveView Native Core.

bcardarella commented 8 months ago

It might make sense for us to do this straight up in Swift for now so we can have the API there then refactor to use Core's when it is ready next year. I'll discuss with @carson-katri today to see if this is something we can add for v0.2. If it is a good amount of work I'll probably punt as I don't want to delay the v0.2 release

carson-katri commented 3 months ago

We can store data via cookies at the moment. But there is no built-in support for local storage yet.

AZholtkevych commented 3 months ago

Blocked by https://github.com/liveview-native/liveview-native-core/issues/37