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.

68 lines (67 loc) 2.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const provider_1 = require("@hocuspocus/provider"); const config_1 = __importDefault(require("../../../config/config")); const connector_abstract_1 = __importDefault(require("../connector.abstract")); const sync_handshake_check_class_1 = __importDefault(require("../websocket/sync-handshake-check.class")); const index_1 = require("../../../index"); const logger_1 = require("../../../utils/logger"); class HocuspocusConnector extends connector_abstract_1.default { constructor() { super(...arguments); this.url = ''; this.name = ''; this.token = undefined; this.provider = null; } async connect(text, token) { const li = text.lastIndexOf('/'); if (li == -1 || li == text.length - 1 || !text.startsWith('ws')) { (0, logger_1.error)('HocuspocusConnector: UnSupport URL to connect room'); return; } this.name = text.substring(li + 1); this.url = text.substring(0, li); this.token = token; this.yDoc = new index_1.YDoc(); this.yBaseMap = this.yDoc.getMap(); return new Promise((resolve, reject) => { this.provider = new provider_1.HocuspocusProvider({ url: this.url, name: this.name, document: this.yDoc, token: this.token, onStatus: ({ status }) => { this.status = status.toUpperCase(); (0, logger_1.log)('HocuspocusProvider.event status:', status); }, onSynced: async () => { (0, logger_1.log)('HocuspocusProvider.event synced'); if (config_1.default.ENABLE_SYNC_HANDSHAKE_CHECK) { await sync_handshake_check_class_1.default.wait(this.yBaseMap); } (0, logger_1.log)('HocuspocusProvider.event base map:', this.yBaseMap.toJSON()); this.synced = true; this.clientId = this.yDoc.clientID; this.emitSynced(true); resolve(); }, }); }); } async disconnect() { throw new Error('Disconnecting and switching connections are not supported at the moment.'); /* if (this.provider) { this.provider.disconnect() } this.provider = null this.synced = false this.websocketStatus = ConnectorStatus.DISCONNECTED */ } } exports.default = HocuspocusConnector;