@lynx-js/react / import-attributes
Import Attributes
runtime: 'shared'
Explicitly declare that certain modules can be shared between the main thread and the background thread via Import Attributes.
This is strictly for use with Main Thread Functions. For details, please refer to Cross-thread Shared Modules.
The runtime attribute in the with clause of an import statement specifies the runtime behavior of the imported module. When set to 'shared', the module is marked as shared across threads.
Syntax
Parameters
runtime:'shared'
Examples
Precautions
- Static Analysis: This feature relies on compile-time static analysis. Only identifiers imported directly via
import ... with { runtime: 'shared' }are recognized as shared. - Identifier Passing: If a shared imported function is assigned to another variable, the new variable loses the shared characteristic and cannot be called directly in a main thread function (unless wrapped again as a main thread function).
- Main Thread Limitations: Although code in shared modules can run on the main thread, it does not automatically gain the main thread context (e.g., cannot directly use
MainThreadRef) unless explicitly marked with'main thread'. - State Isolation: Variables and states in a Shared Module are completely isolated between the two threads, each possessing independent instances. Modifying state in one thread does not affect the other.