This directory contains comprehensive examples demonstrating various CodeUChain patterns and features in JavaScript and TypeScript.
Demonstrates the fan-out/fan-in pattern where data is split into parallel branches and merged back together.
Pattern:
+-> (Normalize A) -+
[Input] -> (Fan) (Merge) -> (Aggregate) -> [Output]
+-> (Normalize B) -+
Features:
- Parallel processing of data branches
- Different normalization strategies per branch
- Result aggregation and merging
- Performance optimization through concurrency
Shows how to handle errors by routing them through classification and recovery paths.
Pattern:
(Link) -X-> [Error?]--yes--> (Classify) -> (Retry or Fail)
| no
v
Next Link
Features:
- Error type classification (temporary, validation, auth, unknown)
- Conditional routing based on error type
- Retry logic for recoverable errors
- Permanent failure handling
Demonstrates splitting work into parallel branches and synchronizing results.
Pattern:
+-> (Link A) --+
[Ctx] -> ( Split ) ( Join ) -> [Ctx']
+-> (Link B) --+
Features:
- Work distribution across parallel branches
- Concurrent processing with Promise.all
- Result synchronization and joining
- Performance metrics and load balancing
Shows how to wrap links with cross-cutting concerns using middleware.
Pattern:
[Ctx] -> [Before MW] -> (Link) -> [After MW] -> [Ctx']
| error
v
[OnError MW]
Features:
- Timing middleware for performance monitoring
- Validation middleware for pre/post conditions
- Metrics collection middleware
- Error handling middleware
Implements distributed transactions with compensation logic for rollback.
Pattern:
(Do Step 1) -> (Do Step 2) -> (Do Step 3)
| | |
v v v
(Push C1) (Push C2) (Push C3)
On failure -> Pop & run compensations: C3, C2, C1
Features:
- Saga orchestrator with compensation stack
- LIFO compensation execution
- Failure recovery and cleanup
- Transaction-like behavior for distributed operations
Demonstrates retry logic with exponential backoff for transient failures.
Pattern:
+---------+ failure +-----------+
| Attempt | ---------> | Backoff n | --+
+----+----+ +-----------+ |
^ |
+-------------- success <----------+
Features:
- Exponential backoff with jitter
- Configurable retry limits
- Different failure types (temporary vs persistent)
- Metrics collection and analysis
Comprehensive demonstration of opt-in typed features in JavaScript.
Features:
- JSDoc annotations for TypeScript-like experience
- Generic Context with type evolution
- Generic Link<TInput, TOutput> interfaces
- Type-safe insertAs() method
- Backward compatibility with untyped code
TypeScript example showing clean type evolution through processing layers.
Features:
- TypeScript interface definitions
- Clean type progression (UserInput -> ValidatedUser -> CompleteUser)
- Type-safe data transformation
- Simple processing chain demonstration
Basic CodeUChain usage with user registration flow.
Features:
- Basic Link and Chain usage
- Manual and automatic link naming
- Middleware integration
- Error handling
Each example can be run independently:
# Run a specific example
node examples/branch_merge_pipeline.js
node examples/error_classification_pipeline.js
node examples/parallel_fanout_join.js
node examples/middleware_wrap_pipeline.js
node examples/saga_compensations.js
node examples/retry_with_backoff.js
node examples/typed_features_demo.js
# For TypeScript examples
npx ts-node examples/simple_type_evolution.ts- Linear Processing: Sequential link execution
- Branching: Conditional and parallel processing paths
- Error Handling: Classification, retry, and recovery patterns
- Middleware: Cross-cutting concerns and aspect-oriented programming
- Opt-in Typing: Optional type safety without breaking changes
- Type Evolution: Clean transformation between data shapes
- Generic Interfaces: Type-safe Link<TInput, TOutput> patterns
- Backward Compatibility: Mixed typed/untyped usage
- Saga Transactions: Distributed operations with compensation
- Retry Logic: Exponential backoff and failure recovery
- Parallel Processing: Work distribution and synchronization
- Metrics Collection: Performance monitoring and analysis
- Start Here:
simple_chain.js- Basic concepts - Type System:
typed_features_demo.js+simple_type_evolution.ts - Pipeline Patterns: Branch/merge, error handling, middleware
- Advanced Topics: Saga, retry, parallel processing
- Node.js 14+
- For TypeScript examples:
npm install -g ts-node typescript
These examples showcase CodeUChain's flexibility and power across different processing patterns while maintaining clean, maintainable code.