Persisting the state in the memory
Using in-memory objects and instances is another option for keeping the state of the app synchronized across the components and instances.
We can achieve this by creating a class that has the property to be stored, a delegate so that the consumer components can subscribe to it to execute some logic when its value changes, and an update method, which sets the value of the property and triggers the delegate.
After creating the class, we register an instance of it in the dependency injection container as a singleton, which will be a single instance created when the app is loaded in the browser, and the same instance is used when any component or service injects the object.
One component can call the Update
method and pass the value, and other components can be listeners for the change by subscribing to the delegate and executing some logic in reaction to the value change.
Let’s take a scenario of an app that has certain dashboards...