-
I'm trying out context but I'm curious how it can handle objects as the "value". For example, I want to have a timer that stores both the elapsed time and a status for if its stopped, paused, running. If I create a Timer object that is provided as the initial data, is there anyway for that object to tell the context that its value has updated? I see that the provider has a setValue command but that means remaking the object every time the timer counts up? Here's a lit playground example of what I'm trying to do. I have a Timer class that handles keeping track of state and intervals. A button element that shows the control buttons, a display element that shows the timer count and an app container. Also, TLDRI think provider logic should be able to be separated from the element that's providing them (maybe it already is?). In this case, I'd want to provide a bunch of contexts from the root application element for data that's specifically global, such as the current user, what organization/workspace their in, settings such as user preferences or defaults. What I wouldn't want to happen is to have to make a separate element for each of these contexts and nest my entire app like:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
This is interesting, I had the same interrogation last time, but of course the context only reacts when your object reference changes (just like when you use a decorator like
class AppContainer {
private _timerController = new Timer(this);
@provide({context: timerContext})
@property({attribute: false})
public timerContext = { timer: this._timerController }
updateContext() {
// By changing the reference your context will trigger children updates.
// Children can then access `this.timerContext.timer`
this.timerContext = { timer: this._timerController }
}
} Not sure if that's the best way to do that but one I can think of.
|
Beta Was this translation helpful? Give feedback.
@jrj2211 Hey,
Remember I said I would come back to you when I finish developing a solution I had in mind.
Well time has flew and I was quite busy but I remember this conversation so I am sharing this with you.
Here's the updated Playground.
I think you might be interested about it, I've managed to reduce the code to less than 90 lines, but it's not just about writing less, it's also about having a really easy way to manage states across your elements.
It's still a controller behind the scene, but a multi-host and reactive controller.
In the playground, you can see that all definition is made in the parent class
app-container
(binding the controller to all consumers in the first update met…