GlobalValue
The GlobalValue module ensures that a single instance of a value is created globally, even when modules are imported multiple times (e.g., due to mixing CommonJS and ESM builds) or during hot-reloading in development environments like Next.js or Remix.
It achieves this by using a versioned global store, identified by a unique Symbol tied to the current version of the effect library. The store holds values that are keyed by an identifier, allowing the reuse of previously computed instances across imports or reloads.
This pattern is particularly useful in scenarios where frequent reloading can cause services or single-instance objects to be recreated unnecessarily, such as in development environments with hot-reloading.
Other
globalValue
Added in v2.0.0
Source
Signature
declare function globalValue<A>(id: unknown, compute: () => A): A;
Retrieves or computes a global value associated with the given
id. If the value for thisidhas already been computed, it will be returned from the global store. If it does not exist yet, the providedcomputefunction will be executed to compute the value, store it, and then return it.This ensures that even in cases where the module is imported multiple times (e.g., in mixed environments like CommonJS and ESM, or during hot-reloading in development), the value is computed only once and reused thereafter.