Skip to content
Merged
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
106 changes: 67 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"lodash.orderby": "^4.6.0",
"mocha": "^5.2.0",
"nyc": "^13.1.0",
"sinon": "^4.0.1",
"sinon": "^7.2.3",
"source-map-support": "^0.5.9",
"tslint": "^5.12.0",
"tslint-eslint-rules": "^5.4.0",
Expand All @@ -79,9 +79,9 @@
"change-case": "^3.0.2",
"deprecate": "^1.1.0",
"md5-hex": "^2.0.0",
"moment": "^2.23.0",
"object.entries": "^1.0.4",
"object.values": "^1.0.4",
"moment": "^2.23.0",
"pluralize": "^4.0.0",
"qs": "^6.6.0",
"readable-stream": "^3.1.0",
Expand Down
101 changes: 58 additions & 43 deletions src/Documents/Changes/DatabaseChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,23 @@ export class DatabaseChanges implements IDatabaseChanges {
return new WebSocket(url, options);
}

private _onConnectionStatusChanged() {
private async _onConnectionStatusChanged() {
const acquiredSemContext = acquireSemaphore(this._semaphore);

BluebirdPromise.resolve(acquiredSemContext.promise)
.then(() => {
if (this.connected) {
this._tcs.resolve(this);
return;
}
try {
await acquiredSemContext.promise;

if (this.connected) {
this._tcs.resolve(this);
return;
}

if (this._tcs.promise.isFulfilled()) {
this._tcs = PromiseUtil.defer<IDatabaseChanges>();
}
})
.finally(() => {
acquiredSemContext.dispose();
});
if (this._tcs.promise.isFulfilled()) {
this._tcs = PromiseUtil.defer<IDatabaseChanges>();
}
} finally {
acquiredSemContext.dispose();
}
}

public get connected() {
Expand Down Expand Up @@ -201,7 +201,9 @@ export class DatabaseChanges implements IDatabaseChanges {
}

public dispose(): void {
Array.from(this._confirmations.values()).forEach(confirmation => confirmation.reject());
for (const confirmation of this._confirmations.values()) {
confirmation.reject();
}

this._isCancelled = true;
if (this._client) {
Expand Down Expand Up @@ -258,37 +260,42 @@ export class DatabaseChanges implements IDatabaseChanges {
}

private _send(command: string, value: string, values: string[]): Promise<void> {
return new Promise<void>(((resolve, reject) => {
return new Promise<void>((async (resolve, reject) => {
let currentCommandId: number;

const acquiredSemContext = acquireSemaphore(this._semaphore);
const acquiredSemContext = acquireSemaphore(this._semaphore, {
timeout: 15000,
contextName: "DatabaseChanges._send()"
});

BluebirdPromise.resolve(acquiredSemContext.promise)
.then(() => {
currentCommandId = ++this._commandId;
try {
await acquiredSemContext.promise;

const payload = {
CommandId: currentCommandId,
Command: command,
Param: value
};
currentCommandId = ++this._commandId;

if (values && values.length) {
payload["Params"] = values;
}
const payload = {
CommandId: currentCommandId,
Command: command,
Param: value
};

this._confirmations.set(currentCommandId, { resolve, reject });
const payloadAsString = JSON.stringify(payload, null, 0);
if (values && values.length) {
payload["Params"] = values;
}

this._client.send(payloadAsString);
})
.catch((err) => {
if (!this._isCancelled) {
throw err;
}
})
.timeout(15000)
.finally(() => acquiredSemContext.dispose());
this._confirmations.set(currentCommandId, { resolve, reject });
const payloadAsString = JSON.stringify(payload, null, 0);

this._client.send(payloadAsString);
} catch (err) {
if (!this._isCancelled) {
throw err;
}
} finally {
if (acquiredSemContext) {
acquiredSemContext.dispose();
}
}
}));
}

Expand Down Expand Up @@ -336,7 +343,10 @@ export class DatabaseChanges implements IDatabaseChanges {
setTimeout(() => this._doWorkInternal(url), 1000);
}

Array.from(this._confirmations.values()).forEach(v => v.reject());
for (const confirm of this._confirmations.values()) {
confirm.reject();
}

this._confirmations.clear();
});

Expand Down Expand Up @@ -365,8 +375,13 @@ export class DatabaseChanges implements IDatabaseChanges {
const payloadParsed = JSON.parse(data) as any[];

try {
for (const message of (Array.isArray(payloadParsed) ? payloadParsed : [payloadParsed])) {
const messages = Array.isArray(payloadParsed) ? payloadParsed : [payloadParsed];
for (const message of messages) {
const type = message.Type;
if (!type) {
continue;
}

switch (type) {
case "Error":
const exceptionAsString = message.Exception;
Expand Down Expand Up @@ -419,9 +434,9 @@ export class DatabaseChanges implements IDatabaseChanges {

this._emitter.emit("error", e);

Array.from(this._counters.values()).forEach(state => {
for (const state of this._counters.values()) {
state.error(e);
});
}
}

public forAllCounters(): IChangesObservable<CounterChange> {
Expand Down
4 changes: 2 additions & 2 deletions src/Documents/Identity/HiloIdGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as semaphore from "semaphore";

import { IDocumentStore } from "../../Documents/IDocumentStore";
import { DateUtil } from "../../Utility/DateUtil";
import { acquireSemaphore, AcquiredSemaphoreContext } from "../../Utility/SemaphoreUtil";
import { acquireSemaphore, SemaphoreAcquisitionContext } from "../../Utility/SemaphoreUtil";
import { StringUtil } from "../../Utility/StringUtil";
import { HiloReturnCommand } from "./Commands/HiloReturnCommand";
import { NextHiloCommand, HiLoResult } from "./Commands/NextHiloCommand";
Expand Down Expand Up @@ -54,7 +54,7 @@ export class HiloIdGenerator {
return id;
}

let acquiredSemContext: AcquiredSemaphoreContext;
let acquiredSemContext: SemaphoreAcquisitionContext;
try {
//local range is exhausted , need to get a new range
acquiredSemContext = acquireSemaphore(this._generatorLock, {
Expand Down
Loading