Split client-side definition into separate file

This commit is contained in:
Steve Oney
2018-08-22 17:50:20 -04:00
parent a0ce865606
commit c6f149bbdf
6 changed files with 135 additions and 141 deletions
+51 -135
View File
@@ -1,147 +1,63 @@
// Type definitions for sharedb v1.0.0-beta.11
// Type definitions for sharedb 1.0
// Project: https://github.com/share/sharedb
// Definitions by: Steve Oney <http://from.so/>
// Definitions by: Steve Oney <https://github.com/soney>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace ShareDB {
type DocumentID = string;
type Path = ReadonlyArray<string|number>;
type Snapshot = number;
/// <reference path="lib/sharedb.d.ts" />
interface AddNumOp { p: Path; na: number; }
import { Doc, Query, Error, Action, RawOp, Op } from './lib/sharedb';
interface ListInsertOp { p: Path; li: any; }
interface ListDeleteOp { p: Path; ld: any; }
interface ListReplaceOp { p: Path; li: any; ld: any; }
interface ListMoveOp { p: Path; lm: any; }
type UseCallback = ((request: {action: Action, agent: any, req: any, collection: string, id: string, query: any, op: RawOp}, callback: () => void) => void);
interface ObjectInsertOp { p: Path; oi: any; }
interface ObjectDeleteOp { p: Path; od: any; }
interface ObjectReplaceOp { p: Path; oi: any; od: any; }
interface StringInsertOp { p: Path; si: string; }
interface StringDeleteOp { p: Path; sd: string; }
interface SubtypeOp { p: Path; t: string; o: any; }
type Op = AddNumOp | ListInsertOp | ListDeleteOp | ListReplaceOp | ListMoveOp | ObjectInsertOp | ObjectDeleteOp | ObjectReplaceOp | StringInsertOp | StringDeleteOp | SubtypeOp;
interface RawOp {
src: string;
seq: number;
v: number;
op: Op[];
m: any;
c: string;
d: string;
}
type OTType = 'ot-text' | 'ot-json0' | 'ot-text-tp2' | 'rich-text';
type Action = 'connect'|'op'|'doc'|'query'|'submit'|'apply'|'commit'|'after submit'|'receive';
interface Error {
code: number;
message: string;
}
interface ShareDBSourceOptions { source?: boolean; }
// interface ShareDBCreateOptions extends ShareDBSourceOptions {}
// interface ShareDBDelOptions extends ShareDBSourceOptions {}
// interface ShareDBSubmitOpOptions extends ShareDBSourceOptions {}
type Callback = (err: Error) => any;
class Doc {
type: string;
id: DocumentID;
data: any;
fetch: (callback: (err: Error) => void) => void;
subscribe: (callback: (err: Error) => void) => void;
on: (event: 'load'|'create'|'before op'|'op'|'del'|'error', callback: (...args: any[]) => any) => void;
ingestSnapshot: (snapshot: Snapshot, callback: Callback) => void;
destroy: () => void;
create(data: any, callback?: Callback): void;
create(data: any, type?: OTType, callback?: Callback): void;
create(data: any, type?: OTType, options?: ShareDBSourceOptions, callback?: Callback): void;
submitOp: (data: ReadonlyArray<Op>, options?: ShareDBSourceOptions, callback?: Callback) => void;
del: (options: ShareDBSourceOptions, callback: (err: Error) => void) => void;
removeListener: (eventName: string, listener: () => any) => void;
}
class Query {
ready: boolean;
results: Doc[];
extra: any;
on: (event: 'ready'|'error'|'changed'|'insert'|'move'|'remove'|'extra', callback: (...args: any[]) => any) => any;
destroy: () => void;
}
interface PubSubOptions {
prefix?: string;
}
interface Stream {
id: string;
}
declare module 'sharedb/lib/client' {
type Doc = ShareDB.Doc;
type Op = ShareDB.Op;
type Action = ShareDB.Action;
export = sharedb;
declare class sharedb {
constructor(options?: {db?: any, pubsub?: sharedb.PubSub, disableDocAction?: boolean, disableSpaceDelimitedActions?: boolean});
connect: () => sharedb.Connection;
addProjection(name: string, collection: string, fields: {}): any;
listen(stream: any): void;
close(callback?: (err: Error) => any): void;
use(action: Action, fn: UseCallback);
types: {
register: () => void
};
}
declare namespace sharedb {
abstract class PubSub {
private static shallowCopy(obj: any);
protected prefix?: string;
protected nextStreamId: number;
protected streamsCount: number;
protected streams: {
[channel: string]: Stream;
};
protected subscribed: {
[channel: string]: boolean;
};
protected constructor(options?: PubSubOptions);
close(callback?: (err: Error|null) => void): void;
publish(channels: string[], data: {[k: string]: any}, callback: (err: Error | null) => void): void;
subscribe(channel: string, callback: (err: Error | null, stream?: Stream) => void): void;
protected abstract _subscribe(channel: string, callback: (err: Error | null) => void): void;
protected abstract _unsubscribe(channel: string, callback: (err: Error | null) => void): void;
protected abstract _publish(channels: string[], data: any, callback: (err: Error | null) => void): void;
protected _emit(channel: string, data: {[k: string]: any}): void;
private _createStream(channel);
private _removeStream(channel, stream);
}
class Connection {
constructor(ws: WebSocket);
get(collectionName: string, documentID: ShareDB.DocumentID): ShareDB.Doc;
createFetchQuery(collectionName: string, query: string, options: {results?: ShareDB.Query[]}, callback: (err: ShareDB.Error, results: any) => any): ShareDB.Query;
createSubscribeQuery(collectionName: string, query: string, options: {results?: ShareDB.Query[]}, callback: (err: ShareDB.Error, results: any) => any): ShareDB.Query;
get(collectionName: string, documentID: string): Doc;
createFetchQuery(collectionName: string, query: string, options: {results?: Query[]}, callback: (err: Error, results: any) => any): Query;
createSubscribeQuery(collectionName: string, query: string, options: {results?: Query[]}, callback: (err: Error, results: any) => any): Query;
}
}
declare module 'sharedb' {
class ShareDBServer {
constructor(options?: {db?: any, pubsub?: ShareDBServer.PubSub, disableDocAction?: boolean, disableSpaceDelimitedActions?: boolean});
connect: () => ShareDBServer.Connection;
addProjection(name: string, collection: string, fields: {}): any;
listen(stream: any): void;
close(callback?: (err: ShareDBServer.Error) => any): void;
use(action: ShareDB.Action, fn: ShareDBServer.UseCallback);
types: {
register: () => void
};
}
namespace ShareDBServer {
type UseCallback = ((request: {action: ShareDB.Action, agent: any, req: any, collection: string, id: string, query: any, op: ShareDB.RawOp}, callback: () => void) => void);
interface Error {
code: number;
message: string;
}
interface PubSubOptions {
prefix?: string;
}
interface Stream {
id: string;
}
abstract class PubSub {
private static shallowCopy(obj: any);
protected prefix?: string;
protected nextStreamId: number;
protected streamsCount: number;
protected streams: {
[channel: string]: Stream;
};
protected subscribed: {
[channel: string]: boolean;
};
protected constructor(options?: PubSubOptions);
close(callback?: (err: Error|null) => void): void;
publish(channels: string[], data: {[k: string]: any}, callback: (err: Error | null) => void): void;
subscribe(channel: string, callback: (err: Error | null, stream?: Stream) => void): void;
protected abstract _subscribe(channel: string, callback: (err: Error | null) => void): void;
protected abstract _unsubscribe(channel: string, callback: (err: Error | null) => void): void;
protected abstract _publish(channels: string[], data: any, callback: (err: Error | null) => void): void;
protected _emit(channel: string, data: {[k: string]: any}): void;
private _createStream(channel);
private _removeStream(channel, stream);
}
type Doc = ShareDB.Doc;
type Op = ShareDB.Op;
type Action = ShareDB.Action;
class Connection {
constructor(ws: WebSocket);
get(collectionName: string, documentID: ShareDB.DocumentID): ShareDB.Doc;
createFetchQuery(collectionName: string, query: string, options: {results?: ShareDB.Query[]}, callback: (err: ShareDB.Error, results: any) => any): ShareDB.Query;
createSubscribeQuery(collectionName: string, query: string, options: {results?: ShareDB.Query[]}, callback: (err: ShareDB.Error, results: any) => any): ShareDB.Query;
}
}
export = ShareDBServer;
}
+12
View File
@@ -0,0 +1,12 @@
/// <reference path="sharedb.d.ts" />
import * as WebSocket from 'ws';
import * as ShareDB from './sharedb';
export class Connection {
constructor(ws: WebSocket);
get(collectionName: string, documentID: string): Doc;
createFetchQuery(collectionName: string, query: string, options: {results?: Query[]}, callback: (err: Error, results: any) => any): Query;
createSubscribeQuery(collectionName: string, query: string, options: {results?: Query[]}, callback: (err: Error, results: any) => any): Query;
}
export type Doc = ShareDB.Doc;
export type Query = ShareDB.Query;
+67
View File
@@ -0,0 +1,67 @@
export type Path = ReadonlyArray<string|number>;
export type Snapshot = number;
export interface AddNumOp { p: Path; na: number; }
export interface ListInsertOp { p: Path; li: any; }
export interface ListDeleteOp { p: Path; ld: any; }
export interface ListReplaceOp { p: Path; li: any; ld: any; }
export interface ListMoveOp { p: Path; lm: any; }
export interface ObjectInsertOp { p: Path; oi: any; }
export interface ObjectDeleteOp { p: Path; od: any; }
export interface ObjectReplaceOp { p: Path; oi: any; od: any; }
export interface StringInsertOp { p: Path; si: string; }
export interface StringDeleteOp { p: Path; sd: string; }
export interface SubtypeOp { p: Path; t: string; o: any; }
export type Op = AddNumOp | ListInsertOp | ListDeleteOp | ListReplaceOp | ListMoveOp | ObjectInsertOp | ObjectDeleteOp | ObjectReplaceOp | StringInsertOp | StringDeleteOp | SubtypeOp;
export interface RawOp {
src: string;
seq: number;
v: number;
op: Op[];
m: any;
c: string;
d: string;
}
export type OTType = 'ot-text' | 'ot-json0' | 'ot-text-tp2' | 'rich-text';
export type Action = 'connect'|'op'|'doc'|'query'|'submit'|'apply'|'commit'|'after submit'|'receive';
export interface Error {
code: number;
message: string;
}
export interface ShareDBSourceOptions { source?: boolean; }
// interface ShareDBCreateOptions extends ShareDBSourceOptions {}
// interface ShareDBDelOptions extends ShareDBSourceOptions {}
// interface ShareDBSubmitOpOptions extends ShareDBSourceOptions {}
export type Callback = (err: Error) => any;
export class Doc {
type: string;
id: string;
data: any;
fetch: (callback: (err: Error) => void) => void;
subscribe: (callback: (err: Error) => void) => void;
on: (event: 'load'|'create'|'before op'|'op'|'del'|'error', callback: (...args: any[]) => any) => void;
ingestSnapshot: (snapshot: Snapshot, callback: Callback) => void;
destroy: () => void;
create(data: any, callback?: Callback): void;
create(data: any, type?: OTType, callback?: Callback): void;
create(data: any, type?: OTType, options?: ShareDBSourceOptions, callback?: Callback): void;
submitOp: (data: ReadonlyArray<Op>, options?: ShareDBSourceOptions, callback?: Callback) => void;
del: (options: ShareDBSourceOptions, callback: (err: Error) => void) => void;
removeListener: (eventName: string, listener: () => any) => void;
}
export class Query {
ready: boolean;
results: Doc[];
extra: any;
on: (event: 'ready'|'error'|'changed'|'insert'|'move'|'remove'|'extra', callback: (...args: any[]) => any) => any;
destroy: () => void;
}
+2 -2
View File
@@ -3,6 +3,7 @@ import * as ShareDB from 'sharedb';
import * as http from 'http';
import * as WebSocket from 'ws';
import { Duplex } from 'stream';
import * as ShareDBClient from 'sharedb/lib/client';
// Adapted from https://github.com/avital/websocket-json-stream
class WebSocketJSONStream extends Duplex {
@@ -60,9 +61,8 @@ function startServer() {
}
function startClient(callback) {
const sharedb = require('sharedb/lib/client');
const socket = new WebSocket('ws://localhost:8080');
const connection = new sharedb.Connection(socket);
const connection = new ShareDBClient.Connection(socket);
const doc = connection.get('examples', 'counter');
doc.subscribe(() => {
doc.submitOp([{p: ['numClicks'], na: 1}]);
+2
View File
@@ -20,7 +20,9 @@
"forceConsistentCasingInFileNames": true
},
"files": [
"lib/sharedb.d.ts",
"index.d.ts",
"lib/client.d.ts",
"sharedb-tests.ts"
]
}
+1 -4
View File
@@ -1,7 +1,4 @@
{
"extends": "dtslint/dt.json",
"rules": {
"dt-header": false,
"no-declare-current-package": false
}
"rules": { }
}