json-crdt-server
Version:
JSON CRDT server and syncing local-first browser client
31 lines (30 loc) • 1.4 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.listen = void 0;
const rxjs_1 = require("rxjs");
const schema_1 = require("../schema");
const listen = ({ t, services }) => (r) => {
const Request = t.Object(t.prop('id', schema_1.BlockIdRef).options({
title: 'Block ID',
description: 'The ID of the block to subscribe to.',
}), t.propOpt('clientId', t.Number({ format: 'u32' })).options({
title: 'Client ID',
description: 'Browser (client) ID, a unique ID of a specific browser instance. When provided events originating by this client will be filtered out. The client ID is a 32-bit unsigned integer, which should be randomly generated on the client and re-used for all API calls.',
}));
const Response = t.Object(t.prop('event', schema_1.BlockEventRef));
const Func = t.Function$(Request, Response).options({
title: 'Listen for block changes',
description: 'Subscribe to a block to receive updates when it changes.',
});
return r.prop('block.listen', Func, (req$) => {
const response = req$.pipe((0, rxjs_1.switchMap)(({ id, clientId = 0 }) => {
return services.blocks.listen(id, clientId);
}), (0, rxjs_1.map)((event) => {
return {
event,
};
}));
return response;
});
};
exports.listen = listen;
;