From c6f149bbdf50d77387c47485cc716f8e32a15b7b Mon Sep 17 00:00:00 2001 From: Steve Oney Date: Wed, 22 Aug 2018 17:50:20 -0400 Subject: [PATCH] Split client-side definition into separate file --- types/sharedb/index.d.ts | 186 +++++++++------------------------ types/sharedb/lib/client.d.ts | 12 +++ types/sharedb/lib/sharedb.d.ts | 67 ++++++++++++ types/sharedb/sharedb-tests.ts | 4 +- types/sharedb/tsconfig.json | 2 + types/sharedb/tslint.json | 5 +- 6 files changed, 135 insertions(+), 141 deletions(-) create mode 100644 types/sharedb/lib/client.d.ts create mode 100644 types/sharedb/lib/sharedb.d.ts diff --git a/types/sharedb/index.d.ts b/types/sharedb/index.d.ts index f610dbe55d..7b34c85583 100644 --- a/types/sharedb/index.d.ts +++ b/types/sharedb/index.d.ts @@ -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 +// Definitions by: Steve Oney // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare namespace ShareDB { - type DocumentID = string; - type Path = ReadonlyArray; - type Snapshot = number; +/// - 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, 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; -} diff --git a/types/sharedb/lib/client.d.ts b/types/sharedb/lib/client.d.ts new file mode 100644 index 0000000000..ad8328dc83 --- /dev/null +++ b/types/sharedb/lib/client.d.ts @@ -0,0 +1,12 @@ +/// +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; diff --git a/types/sharedb/lib/sharedb.d.ts b/types/sharedb/lib/sharedb.d.ts new file mode 100644 index 0000000000..1b9b7ee5df --- /dev/null +++ b/types/sharedb/lib/sharedb.d.ts @@ -0,0 +1,67 @@ +export type Path = ReadonlyArray; +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, 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; +} diff --git a/types/sharedb/sharedb-tests.ts b/types/sharedb/sharedb-tests.ts index 83f5e1c7f6..55c3dc797d 100644 --- a/types/sharedb/sharedb-tests.ts +++ b/types/sharedb/sharedb-tests.ts @@ -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}]); diff --git a/types/sharedb/tsconfig.json b/types/sharedb/tsconfig.json index fcbe6c741a..8d2c0915c0 100644 --- a/types/sharedb/tsconfig.json +++ b/types/sharedb/tsconfig.json @@ -20,7 +20,9 @@ "forceConsistentCasingInFileNames": true }, "files": [ + "lib/sharedb.d.ts", "index.d.ts", + "lib/client.d.ts", "sharedb-tests.ts" ] } \ No newline at end of file diff --git a/types/sharedb/tslint.json b/types/sharedb/tslint.json index c47337c7a0..2914fa23e8 100644 --- a/types/sharedb/tslint.json +++ b/types/sharedb/tslint.json @@ -1,7 +1,4 @@ { "extends": "dtslint/dt.json", - "rules": { - "dt-header": false, - "no-declare-current-package": false - } + "rules": { } }