@formwire/core
v0.1.0
Published
Core form engine with service injection architecture
Maintainers
Readme
@formwire/core
Core form engine with service injection architecture for FormWire.
Features
- Service Injection Architecture - Modular, testable, and extensible
- Zero Dependencies - No external dependencies
- High Performance - Optimized with WeakMap caching and microtask batching
- TypeScript Ready - Full type support
- Memory Efficient - Automatic cleanup with WeakMap
Installation
npm install @formwire/coreUsage
Basic Usage
import { FormEngine, ValidationService, CacheService, EventService, BatchService } from '@formwire/core';
// Create services
const validationService = new ValidationService({
debounceDelay: 300,
validateOnChange: true,
});
const cacheService = new CacheService({
enableValueCache: true,
maxCacheSize: 1000,
});
const eventService = new EventService({
enableContextTracking: true,
enableErrorHandling: true,
});
const batchService = new BatchService({
enableBatching: true,
batchDelay: 100,
});
// Create engine with custom services
const engine = new FormEngine({
validationService,
cacheService,
eventService,
batchService,
});
// Initialize form
engine.init({ email: '', name: '' }, { validateOnBlur: true });Service Management
// Replace services at runtime
engine.setValidationService(new CustomValidationService());
engine.setCacheService(new CustomCacheService());
// Get service statistics
const stats = engine.getServiceStats();
console.log(stats);Form Operations
// Data operations
engine.set('email', '[email protected]');
const email = engine.get('email');
// Validation
engine.registerValidator('email', (value) => {
if (!value) return 'Email is required';
if (!value.includes('@')) return 'Invalid email';
return undefined;
});
engine.validateAll();
// State management
engine.touch('email');
engine.focus('email');
engine.blur();
// Events
engine.on('change', (data) => {
console.log('Form changed:', data);
});
// Submission
engine.submit((values) => {
console.log('Form submitted:', values);
});API Reference
FormEngine
The main form engine class that coordinates all services.
Constructor
new FormEngine(services)services(object): Service configurationvalidationService(ValidationService): Validation service instancecacheService(CacheService): Cache service instanceeventService(EventService): Event service instancebatchService(BatchService): Batch service instance
Methods
init(initialValues, config)- Initialize form with initial values and configurationreset()- Reset form to initial stateget(path)- Get value by pathset(path, value)- Set value by pathsetMany(updates)- Bulk update multiple valuesregisterValidator(path, validator)- Register validator for fieldvalidateAll()- Validate all fieldshasValidator(path)- Check if validator exists for fieldtouch(path)- Mark field as touchedfocus(path)- Focus on fieldblur()- Remove focuson(event, callback)- Subscribe to eventssubmit(onSubmit)- Submit formsetValidationService(service)- Replace validation servicesetCacheService(service)- Replace cache servicesetEventService(service)- Replace event servicesetBatchService(service)- Replace batch servicegetServiceStats()- Get all services statistics
Services
ValidationService
Handles field validation with debouncing support.
const service = new ValidationService({
debounceDelay: 300,
validateOnChange: false,
validateOnBlur: true
});CacheService
High-performance caching with automatic memory cleanup.
const service = new CacheService({
enableValueCache: true,
maxCacheSize: 1000
});EventService
Event system with automatic cleanup and context tracking.
const service = new EventService({
enableContextTracking: true,
enableErrorHandling: true
});BatchService
Efficient operation batching for performance optimization.
const service = new BatchService({
enableBatching: true,
batchDelay: 100
});License
MIT
