@oobleck/fluid-backend
Version:
Fluid Framework backend for nteract RTC
104 lines • 4.04 kB
JavaScript
import { EMPTY, fromEvent, of } from "rxjs";
import { filter, map, mergeMap } from "rxjs/operators";
import { DataObject, SharedMap, SharedString } from "@fluid-experimental/fluid-framework";
var PropertyKey;
(function (PropertyKey) {
PropertyKey["Metadata"] = "metadata";
PropertyKey["Source"] = "source";
})(PropertyKey || (PropertyKey = {}));
/**
* Fluid DataObject
*/
export class ProtoCellDDS extends DataObject {
//#region ISolidCell
get source() {
return this.sharedSource.getText();
}
get metadata() {
var _a;
const result = [];
(_a = this.metadataMap) === null || _a === void 0 ? void 0 : _a.forEach((value, key) => result.push({ key, value }));
return result;
}
applySourceEdit({ type, diff, start, len }) {
switch (type) {
case "insert":
this.sharedSource.insertText(start, diff !== null && diff !== void 0 ? diff : "");
break;
case "replace":
this.sharedSource.replaceText(start, start + (len !== null && len !== void 0 ? len : 0), diff !== null && diff !== void 0 ? diff : "");
break;
case "delete":
this.sharedSource.removeText(start, start + (len !== null && len !== void 0 ? len : 0));
break;
}
}
updateMetadata(key, value) {
this.metadataMap.set(key, value);
}
//#endregion
//#region DataObject
async initializingFirstTime(input) {
var _a;
const source = SharedString.create(this.runtime);
const metadata = SharedMap.create(this.runtime);
if (input) {
source.insertText(0, input.source);
(_a = input.metadata) === null || _a === void 0 ? void 0 : _a.forEach(({ key, value }) => {
metadata.set(key, value);
});
}
this.root.set(PropertyKey.Source, source.handle).set(PropertyKey.Metadata, metadata.handle);
}
async hasInitialized() {
this.sharedSource = await this.root.get(PropertyKey.Source).get();
this.metadataMap = await this.root.get(PropertyKey.Metadata).get();
this.setupObservables();
}
setupObservables() {
this.edits$ = fromEvent(this.sharedSource, "op").pipe(filter(([, local]) => !local), mergeMap(([{ contents: delta }]) => {
if (delta.type === 3 /* GROUP */) {
const { ops } = delta;
return of(...ops);
}
else {
return of(delta);
}
}), mergeMap((delta) => {
switch (delta.type) {
case 0 /* INSERT */: {
const { seg, pos1 } = delta;
return of({
__typename: "TextInserted",
id: this.id,
diff: seg !== null && seg !== void 0 ? seg : "",
start: pos1
});
}
case 1 /* REMOVE */: {
const { pos1, pos2 } = delta;
return of({
__typename: "TextDeleted",
id: this.id,
start: pos1,
len: pos2 - pos1
});
}
case 3 /* GROUP */:
console.log(delta);
break;
}
return EMPTY;
}), map((edit) => ({ source: this.sharedSource, edit })));
this.metadata$ = fromEvent(this.metadataMap, "op").pipe(filter(([op, local]) => !local && op.contents.type === "set"), map(([{ contents }]) => {
return {
id: this.id,
entry: {
key: contents.key,
value: contents.value.value
}
};
}));
}
}
//# sourceMappingURL=protoCell.js.map