UNPKG

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
// 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);