UNPKG

@syncable/core

Version:
76 lines 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("../utils"); function createSyncableCreationRef(type) { return { type, create: { id: utils_1.generateUniqueId(), }, }; } exports.createSyncableCreationRef = createSyncableCreationRef; function createSyncable(type, data) { let id; if (typeof type === 'string') { id = utils_1.generateUniqueId(); } else { ({ type, create: { id }, } = type); } return { _id: id, _type: type, _clock: 0, _createdAt: 0, _updatedAt: 0, ...data, }; } exports.createSyncable = createSyncable; function getSyncableRef(source) { let type; let id; if (typeof source === 'string') { [type, id] = source.split(':'); } else if ('_type' in source) { ({ _type: type, _id: id } = source); } else { ({ type, create: { id }, } = source); } if (typeof type !== 'string' || typeof id !== 'string') { throw new Error('Invalid source'); } return { type, id }; } exports.getSyncableRef = getSyncableRef; function getSyncableKey(source) { let type; let id; if ('_type' in source) { ({ _type: type, _id: id } = source); } else if ('create' in source) { ({ type, create: { id }, } = source); } else { ({ type, id } = source); } if (typeof type !== 'string' || typeof id !== 'string') { throw new Error('Invalid source'); } return `${type}:${id}`; } exports.getSyncableKey = getSyncableKey; //# sourceMappingURL=syncable.js.map