json-crdt-repo
Version:
JSON CRDT server and syncing local-first browser client
69 lines (68 loc) • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DemoServerRemoteHistory = void 0;
const shareByKey_1 = require("../util/rx/shareByKey");
class DemoServerRemoteHistory {
constructor(client, clientId = 0) {
this.client = client;
this.clientId = clientId;
this.listen = (0, shareByKey_1.shareByKey)((id) => this.client.call$('block.listen', { id, clientId: this.clientId }));
}
async read(id) {
return await this.client.call('block.get', { id });
}
async pull(id, seq = -1, create = false) {
return await this.client.call('block.pull', { id, seq, create });
}
async create(id, batch) {
const res = await this.client.call('block.new', batch ? { id, batch } : { id });
return {
snapshot: {
seq: res.snapshot.seq,
},
batch: {
seq: res.snapshot.seq,
ts: res.snapshot.ts,
},
};
}
async update(id, batch, seq) {
const res = await this.client.call('block.upd', {
create: true,
id,
batch,
seq,
clientId: this.clientId,
});
return {
batch: res.batch,
pull: res.pull,
};
}
async delete(id) {
await this.client.call('block.del', { id });
}
async scanFwd(id, seq) {
const limit = 100;
const res = await this.client.call('block.scan', {
id,
seq: seq + 1,
limit,
});
return res;
}
async scanBwd(id, seq, snapshot) {
if (seq <= 0)
throw new Error('INV_SEQ');
const startSeq = Math.max(0, seq - 100);
const limit = seq - startSeq;
const res = await this.client.call('block.scan', {
id,
seq: startSeq,
limit,
snapshot: !!snapshot,
});
return res;
}
}
exports.DemoServerRemoteHistory = DemoServerRemoteHistory;