Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion types/sharedb/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { EventEmitter } from "events";
import { Duplex } from "stream";
import Agent = require("./lib/agent");
import { Connection } from "./lib/client";
import { Connection, Snapshot } from "./lib/client";
import * as ShareDB from "./lib/sharedb";

interface PubSubOptions {
Expand Down Expand Up @@ -56,6 +56,27 @@ declare class sharedb extends EventEmitter {
* or session info.
*/
listen(stream: Duplex, request?: any): Agent;
fetch<T = unknown>(
agent: Agent,
index: string,
id: string,
callback: (error: Error | null, snapshot?: Snapshot<T>) => void,
): void;
fetch<T = unknown>(
agent: Agent,
index: string,
id: string,
options: ShareDB.BackendFetchOptions | null,
callback: (error: Error | null, snapshot?: Snapshot<T>) => void,
): void;
submit(
agent: Agent,
index: string,
id: string,
op: SubmitRequest["op"],
options: ShareDB.BackendSubmitOptions | null,
callback: (error: Error | null, ops: any[], request?: SubmitRequest) => void,
): void;
close(callback?: BasicCallback): void;
/**
* Registers a server middleware function.
Expand Down
12 changes: 9 additions & 3 deletions types/sharedb/lib/sharedb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export interface RawOp {
d: DocumentID;
}

export type CreateOp = RawOp & { create: { type: string; data: any }; del: undefined; op: undefined };
export type DeleteOp = RawOp & { del: boolean; create: undefined; op: undefined };
export type EditOp = RawOp & { op: any[]; create: undefined; del: undefined };
export type CreateOp = Partial<RawOp> & { create: { type: string; data: any } };
export type DeleteOp = Partial<RawOp> & { del: boolean };
export type EditOp = Partial<RawOp> & { op: any[] };

export type OTType = "ot-text" | "ot-json0" | "ot-json1" | "ot-text-tp2" | "rich-text";

Expand Down Expand Up @@ -154,6 +154,12 @@ export interface ShareDBSourceOptions {
// interface ShareDBCreateOptions extends ShareDBSourceOptions {}
// interface ShareDBDelOptions extends ShareDBSourceOptions {}
// interface ShareDBSubmitOpOptions extends ShareDBSourceOptions {}
export interface BackendFetchOptions {
snapshotOptions?: Record<string, unknown>;
}
export interface BackendSubmitOptions {
[key: string]: unknown;
}

export type Callback = (err: Error) => any;

Expand Down
23 changes: 20 additions & 3 deletions types/sharedb/sharedb-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ console.log(backend.extraDbs);
backend.addProjection("notes_minimal", "notes", { title: true, creator: true, lastUpdateTime: true });
const readonlyProjection = backend.projections["notes_minimal"];
console.log(readonlyProjection.target, readonlyProjection.fields);
backend.submit({} as Agent, "notes_minimal", "doc1", { create: { data: {}, type: "json uri type" } }, {
customField: true,
anotherOption: { nested: "value" },
}, (error, ops, request) => {
if (error) {
console.error(error.message);
}
console.log(ops, request && request.collection);
});
backend.fetch({} as Agent, "notes_minimal", "doc1", {
snapshotOptions: { foo: "bar" },
}, (error, snapshot) => {
if (error) {
console.error(error.message);
}
console.log(snapshot && snapshot.data);
});
// backend.projections is used by sharedb internally, so they shouldn't be messed with.
// Test that marking as readonly in API prevents external modification.
// @ts-expect-error
Expand Down Expand Up @@ -135,9 +152,9 @@ for (const action of submitRelatedActions) {
request.snapshot,
request.ops,
request.channels,
request.op.op,
request.op.create,
request.op.del,
(request.op as ShareDB.EditOp).op,
(request.op as ShareDB.CreateOp).create,
(request.op as ShareDB.DeleteOp).del,
request.extra.source,
);
callback();
Expand Down