UNPKG

@oobleck/fluid-backend

Version:

Fluid Framework backend for nteract RTC

163 lines 7.37 kB
import { Subject } from "rxjs"; import { filter, map } from "rxjs/operators"; import { eachValueFrom } from "rxjs-for-await"; import { createFluidContainer, getFluidContainer } from "./loader"; const QueryResolver = { notebook: (parent, args, context /*, info: any*/) => { var _a; return (_a = context.shell) === null || _a === void 0 ? void 0 : _a.model; } }; const MutationResolver = { upsertNotebook: async (parent, { input }, context) => { var _a, _b; const { id: containerId, createNew } = input; const resources = await (createNew ? createFluidContainer(containerId) : getFluidContainer(containerId)); context.initialize(resources); const model = createNew ? await ((_a = context.shell) === null || _a === void 0 ? void 0 : _a.upsertModel(input)) : (_b = context.shell) === null || _b === void 0 ? void 0 : _b.model; return { notebook: model }; }, deleteCell: (parent, { id: cellId }, { shell }) => { const model = shell === null || shell === void 0 ? void 0 : shell.model; model === null || model === void 0 ? void 0 : model.deleteCell(cellId); return true; }, insertCell: async (parent, { input }, { shell }) => { const { cell, insertAt } = input; const model = shell === null || shell === void 0 ? void 0 : shell.model; const newCell = await (model === null || model === void 0 ? void 0 : model.insertCell(cell, insertAt)); return newCell; }, moveCell: (parent, { input }, { shell }) => { const { id, insertAt } = input; const model = shell === null || shell === void 0 ? void 0 : shell.model; model === null || model === void 0 ? void 0 : model.moveCell(id, insertAt); return true; }, patchCellSource: async (parent, { input }, { shell }) => { const { id: cellId } = input; const model = shell === null || shell === void 0 ? void 0 : shell.model; const cell = await (model === null || model === void 0 ? void 0 : model.getCell(cellId)); cell === null || cell === void 0 ? void 0 : cell.applySourceEdit(input); return true; }, replaceCell: async (parent, { input }, { shell }) => { const { id: cellId, cell } = input; const model = shell === null || shell === void 0 ? void 0 : shell.model; const newCell = await (model === null || model === void 0 ? void 0 : model.replaceCell(cellId, cell)); return newCell; }, updatePresence: async (parent, { input }, { presence }) => { presence === null || presence === void 0 ? void 0 : presence.update(input); return true; }, updateNotebookMetadata: async (parent, { input }, { shell }) => { const { key, value } = input.metadata; const model = shell === null || shell === void 0 ? void 0 : shell.model; model === null || model === void 0 ? void 0 : model.updateMetadata(key, value); return true; }, updateCellMetadata: async (parent, { input }, { shell }) => { const { id: cellId, metadata } = input; const model = shell === null || shell === void 0 ? void 0 : shell.model; const cell = await (model === null || model === void 0 ? void 0 : model.getCell(cellId)); cell === null || cell === void 0 ? void 0 : cell.updateMetadata(metadata.key, metadata.value); return true; }, appendCellOutput: async (parent, { input }, { shell }) => { const { id: cellId, output } = input; const model = shell === null || shell === void 0 ? void 0 : shell.model; const cell = await (model === null || model === void 0 ? void 0 : model.getCell(cellId)); if ((cell === null || cell === void 0 ? void 0 : cell.cellType) === "CodeCell") { cell.appendOutput(output); } return true; }, clearCellOutputs: async (parent, { id: cellId }, { shell }) => { const model = shell === null || shell === void 0 ? void 0 : shell.model; await (model === null || model === void 0 ? void 0 : model.clearCellOutputs(cellId)); return true; } }; const SubscriptionResolver = { notebookEvents: { subscribe: (parent, args, { shell }) => { var _a; const model = shell === null || shell === void 0 ? void 0 : shell.model; const events$ = (_a = model === null || model === void 0 ? void 0 : model.events$) !== null && _a !== void 0 ? _a : new Subject(); return eachValueFrom(events$); }, resolve: (payload) => payload }, cellEvents: { subscribe: (parent, args, { shell }) => { var _a; const model = shell === null || shell === void 0 ? void 0 : shell.model; const events$ = (_a = model === null || model === void 0 ? void 0 : model.cellEvents$) !== null && _a !== void 0 ? _a : new Subject(); return eachValueFrom(events$); }, resolve: (payload) => payload }, cellEdits: { subscribe: (parent, { id: cellId }, { shell }) => { var _a; const model = shell === null || shell === void 0 ? void 0 : shell.model; const edits$ = (_a = model === null || model === void 0 ? void 0 : model.cellEdits$) !== null && _a !== void 0 ? _a : new Subject(); if (!cellId) { return eachValueFrom(edits$.pipe(map(({ source, edit }) => { return { __typename: "TextReplaced", id: edit.id, diff: source.getText(), start: 0, len: source.getLength() }; }))); } else { return eachValueFrom(edits$.pipe(filter((value) => value.edit.id === cellId), map(({ edit }) => edit))); } }, resolve: (payload) => payload }, presenceEvents: { subscribe: (parent, args, { presence }) => { var _a; const events$ = (_a = presence === null || presence === void 0 ? void 0 : presence.events$) !== null && _a !== void 0 ? _a : new Subject(); return eachValueFrom(events$); }, resolve: (payload) => payload } }; const NotebookResolver = { // async id(model: NotebookDDS) { // return model.id; // }, // eslint-disable-next-line @typescript-eslint/no-unused-vars async cells(solidModel, args /*, context: any, info: any*/) { const cells = await solidModel.getCells(); return { edges: [], nodes: cells, pageInfo: null, totalCount: cells.length }; }, async cell(solidModel, args) { const cell = await solidModel.getCell(args.id); return cell; } }; export const NotebookResolvers = { Query: QueryResolver, Mutation: MutationResolver, Subscription: SubscriptionResolver, Notebook: NotebookResolver, Cell: { __resolveType: ({ cellType }) => cellType }, CellOutput: { __resolveType: ({ type }) => type } }; //# sourceMappingURL=resolvers.js.map