UNPKG

rhine-var

Version:

Variables that support multi-user collaboration and persistence, making collaboration and variable operations as simple as possible, with strict and well-defined type hints.

40 lines (39 loc) 1.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const logger_1 = require("../../../utils/logger"); class SyncHandshakeCheck { static wait(baseMap) { return new Promise((resolve) => { const waitSync = (syncMap) => { syncMap.set('role', 'client'); syncMap.set('timestamp', Date.now()); (0, logger_1.log)('Sync handshake start', syncMap.toJSON()); setTimeout(() => { const syncObserver = () => { if (syncMap.get('role') === 'server') { (0, logger_1.log)('Sync handshake successfully', syncMap.toJSON()); syncMap.unobserve(syncObserver); resolve(); } }; syncMap.observe(syncObserver); syncObserver(); }, 1); }; if (baseMap.has('sync')) { waitSync(baseMap.get('sync')); } else { (0, logger_1.log)('Sync handshake waiting...'); const mapObserver = () => { if (baseMap.has('sync')) { baseMap.unobserve(mapObserver); waitSync(baseMap.get('sync')); } }; baseMap.observe(mapObserver); } }); } } exports.default = SyncHandshakeCheck;