#patterns
33 snippets tagged with #patterns
TypeScript Typed Event Emitter
Create type-safe event emitters in Node.js with full TypeScript support and autocomplete.
Best for: Type-safe pub/sub communication between modules
Event-Driven Architecture Pattern
Build loosely coupled systems with typed event bus, async event handlers, and domain event patterns.
Best for: Microservice communication
JavaScript Proxy Handler Pattern
Use Proxy and Reflect to create observable objects, validation layers, and dynamic API clients.
Best for: Observable state management
Middleware Chain Pattern
Implement the middleware/pipeline pattern for composable request processing without Express.
Best for: Custom HTTP framework
Pub/Sub Messaging Pattern
Implement publish/subscribe messaging with topics, filtered subscriptions, and message replay.
Best for: In-process event messaging
Circuit Breaker Pattern
Implements the circuit breaker pattern to prevent cascading failures when calling external services.
Best for: External API calls
Promise Queue with Concurrency Limit
A queue that processes async tasks with a configurable concurrency limit to prevent resource exhaustion.
Best for: Rate-limited API calls
Compound Component Pattern
Build flexible compound components using React Context for implicit parent-child communication.
Best for: Accordion components
Render Prop Pattern
Share stateful logic between components using the render prop pattern for maximum flexibility.
Best for: Mouse tracking
React Compound Component Pattern
Build flexible compound components using React context for shared state between related parts.
Best for: Building flexible component libraries
React useReducer for Complex State
Manage complex component state with useReducer pattern including typed actions and middleware.
Best for: Shopping cart state management
Context Selector Pattern
Avoid unnecessary re-renders with a context selector pattern that subscribes to specific state slices.
Best for: High-performance global state
Suspense Data Fetching Pattern
Use React Suspense for data fetching with resource caching, error boundaries, and streaming SSR.
Best for: Suspense-first data loading
Context Provider Factory
Utility function that creates a typed React Context with a Provider and a custom hook in one call.
Best for: Creating typed contexts
Server/Client Component Patterns
Correctly compose server and client components with data fetching, interactivity, and composition patterns.
Best for: RSC architecture planning
Server to Client Component Data Passing
Pass data from server components to client components using props and serialization patterns.
Best for: data hydration
Server Component Composition Patterns
Compose server and client components effectively with the donut pattern and children pass-through.
Best for: interactive layouts
Common Table Expression (CTE) Patterns
Use CTEs to write readable, composable SQL queries with WITH clauses for complex logic.
Best for: complex queries
Subquery Patterns — Scalar, Correlated, Derived
Common subquery patterns including scalar, correlated, and derived table subqueries.
Best for: complex filtering
Python Context Manager Patterns
Create custom context managers with __enter__/__exit__ and contextlib for resource management.
Best for: Automatic resource cleanup and lifecycle management
Python Dataclass Advanced Patterns
Advanced dataclass usage with validation, post-init processing, slots, and frozen instances.
Best for: Type-safe data models without ORMs
Python Functools and Decorator Patterns
Useful decorator patterns with functools including caching, retry, timing, and rate limiting.
Best for: Adding retry logic to flaky operations
Python Enum Advanced Patterns
Use Python enums with custom values, methods, auto(), and Flag for type-safe constants.
Best for: type-safe constants
Enum Classes — Advanced Patterns
Use Kotlin enum classes with properties, methods, interfaces, and companion utilities.
Best for: Type-safe constant sets with behavior
Custom Property Delegates
Create reusable property delegates: validation, logging, caching, and thread-safe lazy initialization.
Best for: Input validation on property assignment
Interface Delegation with 'by'
Delegate interface implementations with the 'by' keyword: composition over inheritance patterns.
Best for: Composition over inheritance
Null Safety — Advanced Patterns
Master null safety in Kotlin: chaining, smart casts, safe builders, orElse patterns, and nullable collections.
Best for: Safe navigation through nested nullable objects
Extension Properties and Receiver Functions
Add properties and functions to existing classes: extension receivers, generic extensions, and DSL patterns.
Best for: Adding utility methods to existing types
Structured Concurrency Patterns
Master coroutine structured concurrency: coroutineScope, async/await, fan-out/fan-in, and parallel map.
Best for: Parallel I/O operations with concurrency limits
Type Class Pattern with Generics
Implement type class patterns in Kotlin: ad-hoc polymorphism, generic serializers, and extension-based dispatch.
Best for: Ad-hoc polymorphism without inheritance
Sealed Hierarchies for Domain Modeling
Use sealed interfaces and classes for exhaustive domain modeling with when expressions.
Best for: Payment processing with type-safe methods
Structured Concurrency Patterns
Apply structured concurrency: parallel decomposition, scoped tasks, cancellation, and resource cleanup.
Best for: Parallel API calls with structured lifecycle
Kotlin Error Handling Patterns
Comprehensive error handling: sealed result types, validated aggregation, and railway-oriented programming.
Best for: Form validation with error accumulation