UNPKG

@eweser/db

Version:
51 lines 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.syncRegistry = void 0; const localStorageService_1 = require("../../utils/localStorageService"); const syncRegistry = (db) => /** sends the registry to the server to check for additions/subtractions on either side */ async () => { db.emit('registrySync', 'syncing'); const body = { token: db.getToken() ?? '', rooms: db.registry, }; if (!body.token) { return false; } const { data: syncResult, error } = await db.serverFetch('/access-grant/sync-registry', { method: 'POST', body }); if (error) { db.emit('registrySync', 'error', error); return false; } db.emit('registrySync', 'success'); db.info('syncResult', syncResult); const { rooms, token, userId } = syncResult ?? {}; if (userId && typeof userId === 'string') { db.debug('setting new userId', userId); db.userId = userId; } if (token && typeof token === 'string') { db.debug('setting new token', token); (0, localStorageService_1.setLocalAccessGrantToken)(db)(token); db.accessGrantToken = token; } else { return false; } if (rooms && typeof rooms === 'object' && Array.isArray(rooms) && rooms.length >= 2) { db.debug('setting new rooms', rooms); // TODO: if a new room was created locally before the sync finishes, this might overwrite it (0, localStorageService_1.setLocalRegistry)(db)(rooms); db.registry = rooms; } else { return false; } return true; }; exports.syncRegistry = syncRegistry; //# sourceMappingURL=syncRegistry.js.map