3 ms·
I'm not sure I understand what you're imagining here. It sounds like you're talking about having in-memory per-user state that persists between requests on the
by slashdev 3y ago
I'm not sure I understand what you're imagining here.
It sounds like you're talking about having in-memory per-user state that persists between requests on the server side. Each time a request comes in, it's handled in the context of the state from previous requests?
That's a lot like Cloudflare's Durable Objects.
But it's also not much different from storing state for a user in the database and/or cache and fetching it on demand.
I seems like you're thinking of having like the ability to yield a response from code and then have a request come back in and resume at that point again. I'm not sure that makes much sense over just looking up some state from somewhere on a request.
- VikingCoder 3y agoYes, you're very close. I apologize for my poor description. Writeline("What's your first name?"); string First = await Readline(); Writeline($"Hello, {First}, what's your last name?"); string Last = await Readline(); Writing event-driven, database backed code is a definite skill to have. But I see so much value in the async / await model, where you're writing code that looks very stateful, very synchronous. I think it's just much easier to reason about. But unfortunately, that requires a long-lived process on a server somewhere. So I'm imagining the best-of-both worlds. I'd be willing to compile to WebAssembly, or something, that would make it easy to keep track of the memory. I think it would be awesome if .Net provided some compilation flag that enabled what I'm thinking about. I'd even be willing to switch languages, if this existed somewhere.
- slashdev 3y agoThis exists, as I mentioned before, in Cloudflare's Durable Objects. You have serverless JavaScript and WebAssembly with consistent, durable, per user state. You could emulate this in your language and framework of choice by loading user state at the start of the request, manipulating it in memory, and save it at the end. Like with durable objects, you’d also need to gate requests and process them one at a time to keep things consistent. A simple queue could be used to do that. It’s really not connected to async/await, you could write it synchronously or with goroutines or actors or whatever.
- VikingCoder 3y agoI'm quite certain I'm not explaining myself well. Being able to write my code as though it's synchronous is the most important aspect of this to me. Writeline("What's your first name?"); string First = await Readline(); Writeline($"Hello, {First}, what's your last name?"); string Last = await Readline(); I understand what you're saying about using a state object. But then I'm responsible for keeping track of the state machine of my code. It stops looking like async/await code, and starts looking like event-driven code again.
- slashdev 3y agoI don’t see the point of that. Your code won’t look like that anyway, change your example to a typical web application backend to see the problem. All you have is requests and responses. They don’t come in a predictable order. Whether you use async/await or not it’s irrelevant. Loading and restoring the state is trivial. If you just want one state object per user, you can load and save it in your framework middleware. In real life you typically need more state than just per user state, which is why it’s uncommon to see that. In the end you’ll find yourself back at the typical application server plus database architecture.