What are the best ways to share information between modules?
I imagine this is a pretty common situation, but I've gotten very conflicting responses. In a MVC-ish structure, say that the controller is triggered and needs to share the updated data with some other parts of the application somehow. Feel free to assume that the data in question is quite simple, such as { width, height }.
There are a number of ways to do this:
Controller calls a method like setData in the /model/data.js module. Data module exports both setData and getData. When other modules need data, they can call getData.
Con: Data module is exporting more than one thing
Con: This is basically sharing global state, and global state should be avoided. (hopefully there is no asynchronous code that could result in a race condition bug)
Con (?): Modules that consume the data don't see how the stateful data they use is maintained - those interactions are all in a separate module.
I was recommended to use an EventEmitter instead: when the model data gets updated, a dataChange event is emitted, and all data consumers see the event and update their own module-internal state.
Con: Explicit reassignment is necessary in every consuming module, example:
let width;
dataEmitter.on('dataChange', (dataName, newValue) => {
if (dataName === 'width') width = newValue;
});
Con (?): Data is now being stored in, say, 8 places around the application, and not just one - it's all fragmented and semi-superfluous now, though there's now no need to import (global) state anymore, so maybe that's just the natural result.
Another option someone suggested: keep all state in the data module only, and have the data module call every consuming module with the required data as a parameter
Con: Data module now has to manage pretty much all entry logic used by any module that consumes data - the data module would probably be doing way too many things at once
Another option, somewhat similar to the first: have the data module consume an EventEmitter triggered by the controller, and export only the data itself ({ width, height })
Con: Some think mutable exports should not be used - exporting a getter function instead, while identical in functionality, would make it more explicit
When I posted my issue to some people, I got all of these as possible responses, and many of them disagreed with each other. I'm not experienced with modules; I don't know enough to differentiate the good from the subjective from the bad.
I know responses will be opinionated, but: What is/are the best-practices solution(s)? Is there a better pattern than those described above? Are some of the above definitely bad choices? Figuring out the right way to do this seems fundamental to working with modules, but I haven't seen anything yet that goes into detail on this subject, and I feel like I can't continue writing modular code until I definitively figure this out. (Links, especially long ones, would be much appreciated)
Thank you!
Posted Mar 19, 2018, 6:05 AM UTC
Hello,
we have two problems here:
The first one, the problem known as 'the source of thruth'
The second one, is known as 'invalid the cach'
Let's trying to understand and to fix each one separated :
Source of thruth (WRITE side): we can reformulate it as 'from where and how my data has been modified ?'. The problem with shared data, is not on the 'read'. It is on the 'write'. A common solution is to provide a SINGLE manner to mutate the 'data' (said 'setState((prevState) => nextState)'). If you can modify the data where and how you want, you will finish with a 'unpredictable state of your data'. And unpredictable state finish on unpredictable behavior.
Invalid the cach (or rerender your view) (READ side): The problem with the cach is to 'know when to invalidate it'. Or for the view, when to rerender. This the 'read' problem. A common solution is to provide a SINGLE manner to read the 'data' (said 'getState()'). Each time you call getState, you get your data. But the question, is when do you call 'getState()' ? In a game/canvas/frame base interface, is in an infinite loop
while (true) getState();easy. But in a wep app, when do you call 'getState()' each second ? each millisecond ?The pattern that resolve this problem (when to read the new state) is known as 'Observer/Observable pattern'.
Note : All solution of state management are base on this pattern. Why ? Because this is the only one that resolve this problem.
So, what is the pattern : we can reduce it as : 'I have many Obervers that observe (listen) an 'Observable'. Each 'observer' will be uptated when 'observabable is updated'. Done.
Resume :
Store.observe : (observer) => Store.observers.push(observer)
Store.getState : () => Store.state
Store.setState : js (nextState) => Store.state = nextState; Store.update(); //how do you compute the nextState ?
Store.update : () => Store.observers.forEach((observer) => observer.update());
Note: observer.update is the contract that link Observer and observable.
Link wikipedia
P.S : If you want to practice, try to implement an Clock with this pattern ;)
All that said, REDUX is an elegant implementation of this pattern. EventEmitter is also an other implementation. And so on. They, all of them, implement this pattern in the underlying source.
Have fun,
Cheers !