Add "reply" middleware, tighten up some any types

This commit is contained in:
Eric Hwang
2019-03-27 22:00:32 -07:00
parent e060dd0398
commit 9b7c019dc8
3 changed files with 41 additions and 9 deletions
+13 -7
View File
@@ -125,8 +125,9 @@ declare namespace middleware {
doc: DocContext; // Deprecated, use 'readSnapshots' instead.
op: OpContext;
query: QueryContext;
receive: ReceiveContext;
readSnapshots: ReadSnapshotsContext;
receive: ReceiveContext;
reply: ReplyContext;
submit: SubmitContext;
}
@@ -165,22 +166,27 @@ declare namespace middleware {
projection: Projection | undefined;
fields: ProjectionFields | undefined;
channel: string;
query: any;
options: any;
query: ShareDB.JSONObject;
options: ShareDB.JSONObject;
db: sharedb.DB | null;
snapshotProjection: Projection | null;
}
interface ReceiveContext extends BaseContext {
data: any;
}
interface ReadSnapshotsContext extends BaseContext {
collection: string;
snapshots: ShareDB.Snapshot[];
snapshotType: SnapshotType;
}
interface ReceiveContext extends BaseContext {
data: ShareDB.JSONObject; // ClientRequest, but before any validation
}
interface ReplyContext extends BaseContext {
request: ShareDB.ClientRequest;
reply: ShareDB.JSONObject;
}
type SnapshotType = 'current' | 'byVersion' | 'byTimestamp';
interface SubmitContext extends BaseContext, SubmitRequest {
+18 -2
View File
@@ -1,18 +1,25 @@
import { EventEmitter } from 'events';
export type JSONValue = string | number | boolean | null | JSONObject | JSONArray;
export interface JSONObject {
[name: string]: JSONValue;
}
export interface JSONArray extends Array<JSONValue> {}
export type Path = ReadonlyArray<string|number>;
export interface Snapshot {
id: string;
v: number;
type: string | null;
data: any; // JSON object, could be undefined
data: JSONObject | undefined;
m: SnapshotMeta | null;
}
export interface SnapshotMeta {
ctime: number;
mtime: number;
[key: string]: any;
// Users can use server middleware to add additional metadata to snapshots.
[key: string]: JSONValue;
}
export interface AddNumOp { p: Path; na: number; }
@@ -95,3 +102,12 @@ export class Query extends EventEmitter {
removeListener(event: QueryEvent, listener: (...args: any[]) => any): this;
destroy(): void;
}
export type RequestAction = 'qf' | 'qs' | 'qu' | 'bf' | 'bs' | 'bu' | 'f' | 's' | 'u' | 'op' | 'nf' | 'nt';
export interface ClientRequest {
/** Short name of the request's action */
a: RequestAction;
[propertyName: string]: JSONValue;
}
+10
View File
@@ -101,6 +101,16 @@ backend.use('receive', (context, callback) => {
);
callback();
});
backend.use('reply', (context, callback) => {
console.log(
context.action,
context.agent,
context.backend,
context.request,
context.reply,
);
callback();
});
backend.use('readSnapshots', (context, callback) => {
console.log(
context.action,