jw-gate
Version:
Creates a "gate" with "locks." When all locks are open, the gate is open, useful for dealing with semi-random async events.
120 lines (100 loc) ⢠3.89 kB
JavaScript
// Simple jw-gate Demo - Smart Device Startup System
const Gate = require('./jw-gate');
console.log('šŖ jw-gate Demo: Smart Device Startup System\n');
console.log('ā'.repeat(60));
console.log('GATE STATUS: Monitoring 3 conditions for device startup');
console.log('ā'.repeat(60));
// Create gate with 3 conditions - all start locked (true)
const deviceGate = new Gate(['network', 'safety', 'power'], true);
// Track state for display
let statusCount = 0;
function displayStatus() {
statusCount++;
const state = deviceGate.getState();
console.log(`\nš STATUS UPDATE #${statusCount}`);
console.log('ā'.repeat(40));
// Show overall gate status
const gateStatus = state.isLocked ? 'š“ LOCKED' : 'š¢ UNLOCKED';
console.log(`Gate Status: ${gateStatus}`);
// Show individual lock states
console.log('Individual Conditions:');
Object.entries(state.locks).forEach(([name, locked]) => {
const status = locked ? 'š LOCKED' : 'š UNLOCKED';
const icon = name === 'network' ? 'š' : name === 'safety' ? 'š”ļø' : 'ā”';
console.log(` ${icon} ${name.padEnd(8)}: ${status}`);
});
console.log(`Progress: ${deviceGate.getTotalLocks() - deviceGate.getLockedCount()}/${deviceGate.getTotalLocks()} conditions met`);
}
// Event handlers with clear messaging
deviceGate.on('unlocked', () => {
console.log('\nš DEVICE ACTIVATED!');
console.log('ā
All conditions met - device is now operational');
displayStatus();
});
deviceGate.on('locked', () => {
console.log('\nā ļø DEVICE PAUSED');
console.log('ā One or more conditions failed - device stopped for safety');
displayStatus();
});
// Show initial state
console.log('\nā° INITIAL STATE:');
displayStatus();
// Simulate conditions changing over time
console.log('\nš Starting condition monitoring...\n');
// Network connects once and stays connected (unlock once)
setTimeout(() => {
console.log('š Network connection established');
deviceGate.setLock('network', false);
displayStatus();
}, 2000);
// Safety check passes once and stays safe (unlock once)
setTimeout(() => {
console.log('š”ļø Safety systems verified');
deviceGate.setLock('safety', false);
displayStatus();
}, 4000);
// Power starts stable (unlock once)
setTimeout(() => {
console.log('ā” Power supply stabilized');
deviceGate.setLock('power', false);
displayStatus();
}, 6000);
// Power becomes unstable (lock again) - dynamic condition
setTimeout(() => {
console.log('ā ļø Power fluctuation detected!');
deviceGate.setLock('power', true);
displayStatus();
}, 10000);
// Power stabilizes again (unlock again)
setTimeout(() => {
console.log('ā” Power supply restored');
deviceGate.setLock('power', false);
displayStatus();
}, 14000);
// Another power issue (lock again)
setTimeout(() => {
console.log('ā ļø Power spike detected!');
deviceGate.setLock('power', true);
displayStatus();
}, 18000);
// Final power restoration (unlock again)
setTimeout(() => {
console.log('ā” Power fully stabilized');
deviceGate.setLock('power', false);
displayStatus();
}, 22000);
// Summary
setTimeout(() => {
console.log('\nā'.repeat(60));
console.log('š DEMO SUMMARY:');
console.log('ā'.repeat(60));
console.log('⢠Network: Connected once, stayed connected');
console.log('⢠Safety: Verified once, stayed safe');
console.log('⢠Power: Fluctuated multiple times (dynamic condition)');
console.log('');
console.log('š KEY INSIGHT:');
console.log('The gate only unlocks when ALL conditions are met.');
console.log('It immediately locks again when ANY condition fails.');
console.log('Perfect for systems that need all conditions satisfied!');
console.log('ā'.repeat(60));
}, 24000);