Thread Decorator
This file defines the Thread
decorator, which is used to mark classes to be run as threads in the @sodacore/core
library. When applied, the class will execute in a separate process with its own context and memory space, isolated from the main process.
Features
- Thread Registration: Marks a class to be executed as a thread (separate process).
- Filename Association: Associates the thread with the file it resides in.
- Flag Support: Allows passing custom flags to the thread at creation time.
- Metadata Management: Stores thread-related metadata for later discovery and execution.
- Deprecated: This decorator is deprecated in favor of
@Worker
and may change in future releases.
Usage
typescript
import Thread from './decorator/thread';
@Thread(__filename, () => ({ debug: true }))
export class MyThreadClass {
// Thread logic here
}
- The
filename
should be the path to the file containing the class. - The
flags
function is evaluated when the thread is created and should return an object of flags.
API
Thread(filename: string, flags?: () => Record<string, string | number>): ClassDecorator
- filename: The filename of the file where the class resides.
- flags: (optional) A function returning an object of flags to be passed to the thread.
- Returns: A class decorator that registers the class as a thread with the specified metadata.
Notes
- Context is not shared between the main process and threads; avoid accessing shared state like the Registry.
- Ensure the filename is correct, especially when working with compiled vs. uncompiled code.
- Flags are evaluated at thread creation and can be used to customize thread behavior.
- Deprecated: Use
@Worker
instead, as this feature will change in an upcoming release.