Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearnup code using prettier #52

Closed
wants to merge 2 commits into from
Closed
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
76 changes: 44 additions & 32 deletions src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,13 @@ export class Client implements Observable<ClientEvent> {
}
}

Promise.all(promises).then(() => {
setTimeout(doLoop, this.syncLoopDuration);
}).catch((err) => {
logger.error(`[SL] c:"${this.getKey()}" sync failed: ${err.message}`);
});
Promise.all(promises)
.then(() => {
setTimeout(doLoop, this.syncLoopDuration);
})
.catch((err) => {
logger.error(`[SL] c:"${this.getKey()}" sync failed: ${err.message}`);
});
};

logger.debug(`[SL] c:"${this.getKey()}" run sync loop`);
Expand Down Expand Up @@ -373,18 +375,22 @@ export class Client implements Observable<ClientEvent> {
name: ClientEventType.DocumentsWatchingPeerChanged,
value: keys.reduce((peersMap, key) => {
const attachment = this.attachmentMap.get(key.toIDString());
peersMap[key.toIDString()] = Array.from(attachment.peerClients.keys());
peersMap[key.toIDString()] = Array.from(
attachment.peerClients.keys(),
);
return peersMap;
}, {}),
});
return
return;
}

const watchEvent = resp.getEvent();
const respKeys = converter.fromDocumentKeys(watchEvent.getDocumentKeysList());
const respKeys = converter.fromDocumentKeys(
watchEvent.getDocumentKeysList(),
);
for (const key of respKeys) {
const attachment = this.attachmentMap.get(key.toIDString());
switch(watchEvent.getEventType()) {
switch (watchEvent.getEventType()) {
case WatchEventType.DOCUMENTS_WATCHED:
attachment.peerClients.set(watchEvent.getClientId(), true);
break;
Expand All @@ -410,7 +416,9 @@ export class Client implements Observable<ClientEvent> {
name: ClientEventType.DocumentsWatchingPeerChanged,
value: respKeys.reduce((peersMap, key) => {
const attachment = this.attachmentMap.get(key.toIDString());
peersMap[key.toIDString()] = Array.from(attachment.peerClients.keys());
peersMap[key.toIDString()] = Array.from(
attachment.peerClients.keys(),
);
return peersMap;
}, {}),
});
Expand Down Expand Up @@ -444,29 +452,33 @@ export class Client implements Observable<ClientEvent> {
req.setChangePack(converter.toChangePack(reqPack));

let isRejected = false;
this.client.pushPull(req, {}, (err, res) => {
if (err) {
logger.error(`[PP] c:"${this.getKey()}" err :"${err}"`);

isRejected = true;
reject(err);
return;
}

const respPack = converter.fromChangePack(res.getChangePack());
doc.applyChangePack(respPack);
this.client
.pushPull(req, {}, (err, res) => {
if (err) {
logger.error(`[PP] c:"${this.getKey()}" err :"${err}"`);

isRejected = true;
reject(err);
return;
}

const docKey = doc.getKey().toIDString();
const remoteSize = respPack.getChangeSize();
logger.info(
`[PP] c:"${this.getKey()}" sync d:"${docKey}", push:${localSize} pull:${remoteSize} cp:${respPack.getCheckpoint().getAnnotatedString()}`,
);
}).on('end', () => {
if (isRejected) {
return;
}
resolve(doc);
});
const respPack = converter.fromChangePack(res.getChangePack());
doc.applyChangePack(respPack);

const docKey = doc.getKey().toIDString();
const remoteSize = respPack.getChangeSize();
logger.info(
`[PP] c:"${this.getKey()}" sync d:"${docKey}", push:${localSize} pull:${remoteSize} cp:${respPack
.getCheckpoint()
.getAnnotatedString()}`,
);
})
.on('end', () => {
if (isRejected) {
return;
}
resolve(doc);
});
});
}
}
6 changes: 3 additions & 3 deletions src/document/change/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class Change {
}

public getAnnotatedString(): string {
return `${this.operations.map((operation) =>
operation.getAnnotatedString()
).join(',')}`;
return `${this.operations
.map((operation) => operation.getAnnotatedString())
.join(',')}`;
}
}
13 changes: 10 additions & 3 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,16 @@ export class Document implements Observable<DocEvent> {
logger.debug(`trying to apply ${changes.length} remote changes`);

if (logger.isEnabled(LogLevel.Trivial)) {
logger.trivial(changes.map((change) =>
`${change.getID().getAnnotatedString()}\t${change.getAnnotatedString()}`,
).join('\n'));
logger.trivial(
changes
.map(
(change) =>
`${change
.getID()
.getAnnotatedString()}\t${change.getAnnotatedString()}`,
)
.join('\n'),
);
}

this.ensureClone();
Expand Down
28 changes: 16 additions & 12 deletions src/document/proxy/array_proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,25 @@ export class ArrayProxy {
return ArrayProxy.pushInternal(context, target, value);
};
} else if (method === 'filter') {
return (callback: (
elem: JSONElement,
idx: number,
arr: Array<JSONElement>
) => Array<JSONElement>): Array<JSONElement> => {
return Array.from(target).map((e) => toProxy(context, e)).filter(callback);
return (
callback: (
elem: JSONElement,
idx: number,
arr: Array<JSONElement>,
) => Array<JSONElement>,
): Array<JSONElement> => {
return Array.from(target)
.map((e) => toProxy(context, e))
.filter(callback);
};
} else if (method === 'reduce') {
return (callback: (
return (
callback: (accumulator: any, curr: JSONElement) => any,
accumulator: any,
curr: JSONElement
) => any, accumulator: any) => {
return Array.from(target).map((e) =>
toProxy(context, e)
).reduce(callback, accumulator);
) => {
return Array.from(target)
.map((e) => toProxy(context, e))
.reduce(callback, accumulator);
};
} else if (method === 'length') {
return target.length;
Expand Down
16 changes: 12 additions & 4 deletions src/util/heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,19 @@ export class Heap<K, V> {
const leftChildIndex = this.getLeftChildIndex(index);
const rightChildIndex = this.getRightChildIndex(index);

const smallerChildIndex = rightChildIndex < count &&
this.comparator(this.nodes[leftChildIndex].getKey(), this.nodes[rightChildIndex].getKey()) < 0 ?
rightChildIndex : leftChildIndex;
const smallerChildIndex =
rightChildIndex < count &&
this.comparator(
this.nodes[leftChildIndex].getKey(),
this.nodes[rightChildIndex].getKey(),
) < 0
? rightChildIndex
: leftChildIndex;

if (this.comparator(this.nodes[smallerChildIndex].getKey(), node.getKey()) < 0) {
if (
this.comparator(this.nodes[smallerChildIndex].getKey(), node.getKey()) <
0
) {
break;
}

Expand Down
12 changes: 7 additions & 5 deletions src/util/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ class ObserverProxy<T> implements Observer<T> {

constructor(executor: Executor<T>, onNoObservers?: Executor<T>) {
this.onNoObservers = onNoObservers;
this.task.then(() => {
executor(this);
}).catch((error) => {
this.error(error);
});
this.task
.then(() => {
executor(this);
})
.catch((error) => {
this.error(error);
});
}

public next(value: T): void {
Expand Down
8 changes: 5 additions & 3 deletions src/util/splay_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,11 @@ export class SplayTree<V> {
public getAnnotatedString(): string {
const metaString = [];
this.traverseInorder(this.root, metaString);
return metaString.map((node) =>
`[${node.getWeight()},${node.getLength()}]${node.getValue()}`
).join('');
return metaString
.map(
(node) => `[${node.getWeight()},${node.getLength()}]${node.getValue()}`,
)
.join('');
}

private getMaximum(): SplayNode<V> {
Expand Down