/** * ScriptEnvironment - Manages script interaction with the harness * * @description Provides the interface between scripts and the harness, handling * state access via ..listen and command emission via ..emit. This class maintains * the current state and collects commands emitted by the script during execution. * * Features: * - State access for ..listen operations * - Command collection for ..emit operations * - Pure table data extraction from metadata wrapper * - Command batching for atomic processing */ class ScriptEnvironment { constructor(currentState) { this.currentState = currentState; this.commands = []; } /** * Get current state for ..listen operations * * @returns {Object} Pure table data (without metadata wrapper) */ getCurrentState() { // Return pure table data, removing metadata wrapper if present return this.currentState.data || this.currentState; } /** * Emit value for ..emit operations * * @param {*} value - Value to emit (any table-compatible data) * @returns {*} The emitted value (for script continuation) */ emitValue(value) { this.commands.push({ type: 'emit', value }); return value; // Return value for script continuation } /** * Get all collected commands * * @returns {Array} Array of command objects */ getCommands() { return this.commands; } /** * Clear commands (useful for resetting between executions) */ clearCommands() { this.commands = []; } /** * Update current state * * @param {Object} newState - New state with metadata wrapper */ updateState(newState) { this.currentState = newState; } } export { ScriptEnvironment };