The codebase currently has:
- Client (
src/client.ts) - Regular lock client, usesmaxonly - RWLockWritePrefClient (
src/rw-write-preferred-client.ts) - RW lock client, usesmaxRead/maxWrite - Broker (
src/broker.ts) - Standard broker - Broker1 (
src/broker-1.ts) - Alternative broker implementation
Both brokers handle both regular locks and RW locks on the same server instance.
Goal: Reduce code duplication between Client and RW client classes.
Proposed Structure:
BaseClient (abstract or base class)
├── Common functionality:
│ ├── Connection management
│ ├── Socket handling
│ ├── Lock request/response handling
│ ├── Error handling
│ └── Event emission
│
├── Client extends BaseClient
│ └── Regular lock methods (uses `max` only)
│
└── RWLockWritePrefClient extends BaseClient
└── RW lock methods (uses `maxRead`/`maxWrite`)
Benefits:
- Eliminate code duplication in connection/socket handling
- Centralize common client logic
- Easier to maintain and extend
- Clear separation of concerns
Considerations:
- Need to ensure backward compatibility
- May require careful handling of different lock types
- Should maintain current API surface
Goal: Reduce code duplication between Broker and Broker1.
Proposed Structure:
BaseBroker (abstract or base class)
├── Common functionality:
│ ├── Lock storage and management
│ ├── Socket handling
│ ├── Lock granting logic
│ ├── Max limit enforcement (handles both `max` and `maxRead`/`maxWrite`)
│ └── Event emission
│
├── Broker extends BaseBroker
│ └── Standard broker implementation
│
└── Broker1 extends BaseBroker
└── Alternative broker implementation (if still needed)
Benefits:
- Eliminate ~1700+ lines of duplicated code
- Single source of truth for lock management logic
- Easier to maintain and test
- Consistent behavior across broker implementations
Considerations:
- Large refactoring effort
- Need to ensure both broker types still work correctly
- May want to deprecate one broker type if they're functionally equivalent
- Should maintain backward compatibility
Decision: Keep current structure for now.
Rationale:
- Current code works correctly
- Both broker types handle regular and RW locks properly
- No immediate need for refactoring
- Can be done as a future improvement
- Create
BaseClientclass with common functionality - Refactor
Clientto extendBaseClient - Refactor
RWLockWritePrefClientto extendBaseClient - Update all tests to ensure compatibility
- Update documentation
- Analyze differences between
BrokerandBroker1 - Create
BaseBrokerclass with shared functionality - Refactor
Brokerto extendBaseBroker - Refactor
Broker1to extendBaseBroker(or deprecate if redundant) - Update all tests to ensure compatibility
- Update documentation
- Consider consolidating to single broker type if possible
- Both brokers currently handle both regular locks (
max) and RW locks (maxRead/maxWrite) correctly - The broker detects lock type via
beginRead !== undefined(RW locks) vsundefined(regular locks) - Regular clients send
maxonly - RW clients send
maxReadandmaxWrite - This separation works well and doesn't require immediate refactoring