goofballLogic
goofballLogic

Reputation: 40489

Does AMD format require that modules are singletons?

Is there anything in the AMD specification to say that require'd modules must be supplied with the same object? It seems to be fairly common practice to assume that a require'd module is a single instance supplied to all requiring modules, but is there anything to prevent a module loader treating loaded modules as merely cached (possibly reloading them at some point)?

For example (hypothetically speaking), would an AMD loader be guaranteed to distribute the same instance of a message bus module across various different dependent modules, so they could use it to send each other messages?

Upvotes: 1

Views: 74

Answers (1)

ekuusela
ekuusela

Reputation: 5292

Yes, modules should be singletons.

From the spec:

define() function

...

factory

The third argument, factory, is a function that should be executed to instantiate the module or an object. If the factory is a function it should only be executed once. If the factory argument is an object, that object should be assigned as the exported value of the module.

Upvotes: 1

Related Questions