ThreadContext
This file defines the ThreadContext
class, which encapsulates information about a thread request in the @sodacore/core
library. It is passed to all thread controller methods and provides access to the request UID, command, subprocess, and context data.
Features
- Request Metadata: Stores the UID, command, subprocess, and context for each thread request.
- Command Parsing: Provides methods to extract the namespace and method from the command string.
- Context Access: Methods to retrieve the entire context or specific values by key.
- Subprocess Info: Access to the calling subprocess and its PID.
Usage
typescript
const ctx = new ThreadContext('uid123', 'namespace:method', subprocess, { foo: 'bar' });
const uid = ctx.getUid();
const command = ctx.getCommand();
const namespace = ctx.getNamespace();
const method = ctx.getMethod();
const context = ctx.getContext();
const fooValue = ctx.getContextValue('foo');
const proc = ctx.getSubprocess();
const pid = ctx.getPid();
API
Constructor
typescript
constructor(
private uid: string,
private command: string,
private subprocess: Subprocess,
private context?: Record<string, any>,
)
- uid: The unique identifier for the request.
- command: The command string (format:
namespace:method
). - subprocess: The subprocess that initiated the command.
- context: Optional context data for the command.
Methods
getUid(): string
Returns the UID of the request.
getCommand(): string
Returns the command that was called.
getNamespace(): string
Returns the namespace part of the command (before the colon).
getMethod(): string
Returns the method part of the command (after the colon).
getContext<T = Record<string, any>>(): T
Returns the context object, typed as T
.
getContextValue<T = any>(key: string): T | null
Returns the value for the specified key from the context, or null
if not present.
getSubprocess(): Subprocess
Returns the subprocess that called the command.
getPid(): number
Returns the PID of the subprocess.
Notes
- Designed for use in thread controller methods to provide consistent access to request and context data.
- The command string is expected to be in the format
namespace:method
.