about summary refs log tree commit diff stats
path: root/js/scripting-lang/scripting-harness/examples/basic-usage.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/scripting-harness/examples/basic-usage.js')
-rw-r--r--js/scripting-lang/scripting-harness/examples/basic-usage.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/js/scripting-lang/scripting-harness/examples/basic-usage.js b/js/scripting-lang/scripting-harness/examples/basic-usage.js
new file mode 100644
index 0000000..df99b06
--- /dev/null
+++ b/js/scripting-lang/scripting-harness/examples/basic-usage.js
@@ -0,0 +1,74 @@
+/**
+ * Basic Usage Example - FunctionalHarness
+ * 
+ * @description Demonstrates basic usage of the FunctionalHarness with
+ * state management, versioning, and command processing.
+ */
+
+// Import the harness components
+import { FunctionalHarness } from '../core/harness.js';
+
+// Sample script that uses ..listen and ..emit
+const sampleScript = `
+/* Sample script demonstrating ..listen and ..emit */
+
+/* Get current state */
+current_state : ..listen;
+
+/* Process the state */
+processed_data : when current_state is
+    { status: "active" } then { result: "active_processed", data: current_state }
+    { status: "inactive" } then { result: "inactive_processed", data: current_state }
+    _ then { result: "unknown_processed", data: current_state };
+
+/* Emit the processed data */
+..emit processed_data;
+
+/* Emit a status update */
+..emit { action: "status_update", timestamp: 1234567890 };
+`;
+
+async function runBasicExample() {
+    console.log('=== Basic Harness Usage Example ===\n');
+
+    // Create harness with script content
+    const harness = new FunctionalHarness(sampleScript, {
+        logStateChanges: true,
+        logCommands: true
+    });
+
+    // Initial state
+    const initialState = {
+        status: "active",
+        user: { name: "Alice", score: 100 },
+        settings: { theme: "dark" }
+    };
+
+    console.log('1. Processing initial state...');
+    const result1 = await harness.processState(initialState);
+    console.log('Result:', JSON.stringify(result1, null, 2));
+
+    console.log('\n2. Processing state change...');
+    const newState = {
+        status: "inactive",
+        user: { name: "Alice", score: 150 },
+        settings: { theme: "light" }
+    };
+    const result2 = await harness.processState(newState);
+    console.log('Result:', JSON.stringify(result2, null, 2));
+
+    console.log('\n3. Version history...');
+    const history = harness.getVersionHistory();
+    console.log('History:', JSON.stringify(history, null, 2));
+
+    console.log('\n4. State diff...');
+    const diff = harness.stateHistory.getDiff(1, 2);
+    console.log('Diff:', JSON.stringify(diff, null, 2));
+
+    console.log('\n=== Example Complete ===');
+}
+
+// Run the example
+runBasicExample().catch(console.error);
+
+export { runBasicExample }; 
\ No newline at end of file