🤖 Merge PR #44842 sharedb – Fix missing & incorrect types by @Calyhre

* fix(sharedb): Add missing types in client lib

* fix(sharedb): Use correct type for `doc.type`

* fix(sharedb): Add missing attributes on doc

* fix(sharedb): Add complete interface for types
This commit is contained in:
Charley DAVID 2020-06-02 09:09:09 +02:00 committed by GitHub
parent cedfab2b23
commit aaf0e95733
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 5 deletions

View File

@ -53,9 +53,7 @@ declare class sharedb {
action: A,
fn: (context: sharedb.middleware.ActionContextMap[A], callback: (err?: any) => void) => void,
): void;
static types: {
register: (type: { name?: string, uri?: string, [key: string]: any}) => void;
};
static types: ShareDB.Types;
}
declare namespace sharedb {

View File

@ -32,3 +32,5 @@ export type SubtypeOp = ShareDB.SubtypeOp;
export type Path = ShareDB.Path;
export type ShareDBSourceOptions = ShareDB.ShareDBSourceOptions;
export const types: ShareDB.Types;

View File

@ -1,5 +1,7 @@
import { EventEmitter } from 'events';
import { Connection } from './client';
export type JSONValue = string | number | boolean | null | JSONObject | JSONArray;
export interface JSONObject {
[name: string]: JSONValue;
@ -49,7 +51,27 @@ export interface RawOp {
c: string;
d: string;
}
export type OTType = 'ot-text' | 'ot-json0' | 'ot-text-tp2' | 'rich-text';
export type OTType = 'ot-text' | 'ot-json0' | 'ot-json1' | 'ot-text-tp2' | 'rich-text';
export interface Type {
name?: string;
uri?: string;
create(initialData?: any): any;
apply(snapshot: any, op: any): any;
transform(op1: any, op2: any, side: 'left' | 'right'): any;
compose(op1: any, op2: any): any;
invert?(op: any): any;
normalize?(op: any): any;
transformCursor?(cursor: any, op: any, isOwnOp: boolean): any;
serialize?(snapshot: any): any;
deserialize?(data: any): any;
[key: string]: any;
}
export interface Types {
register: (type: Type) => void;
map: { [key: string]: Type };
}
export interface Error {
code: number;
message: string;
@ -64,9 +86,13 @@ export type Callback = (err: Error) => any;
export type DocEvent = 'load' | 'create' | 'before op' | 'op' | 'del' | 'error' | 'no write pending' | 'nothing pending';
export class Doc extends EventEmitter {
type: string;
connection: Connection;
type: Type | null;
id: string;
collection: string;
data: any;
version: number | null;
fetch: (callback: (err: Error) => void) => void;
subscribe: (callback: (err: Error) => void) => void;

View File

@ -198,6 +198,25 @@ function startServer() {
});
}
const richTextType = {
name: 'rich-text',
uri: 'http://sharejs.org/types/rich-text/v1',
create() {},
apply() {},
transform() {},
compose() {},
};
ShareDBClient.types.register(richTextType);
console.log(ShareDBClient.types.map);
console.log(ShareDBClient.types.map['rich-text'].name);
console.log(ShareDBClient.types.map['rich-text'].uri);
const op1 = [{ insert: 'Hello' }];
const op2 = [{ retain: 5 }, { insert: ' world!' }];
const op3 = ShareDBClient.types.map['rich-text'].compose(op1, op2);
function startClient(callback) {
const socket = new WebSocket('ws://localhost:8080');
const connection = new ShareDBClient.Connection(socket);
@ -214,4 +233,13 @@ function startClient(callback) {
connection.createSubscribeQuery('examples', 'numClicks >= 5', null, (err, results) => {
console.log(err, results);
});
const anotherDoc = doc.connection.get('examples', 'another-counter');
console.log(anotherDoc.collection);
if (anotherDoc.type !== null) {
console.log(anotherDoc.type.name);
}
if (anotherDoc.version !== null) {
Math.round(anotherDoc.version);
}
}