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, 5:46 AM UTC

20

No comments yet

Nobody commented on this post, or the comments weren't captured.