dgraph-js-native
Advanced tools
Comparing version
export declare const ERR_BEST_EFFORT_REQUIRED_READ_ONLY: Error; | ||
export declare const READ_ONLY_TXN: Error; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ERR_BEST_EFFORT_REQUIRED_READ_ONLY = new Error('Best effort only works for read-only queries'); | ||
exports.READ_ONLY_TXN = new Error('Txn is read-only'); | ||
//# sourceMappingURL=errors.js.map |
@@ -8,3 +8,8 @@ import { QueryTxn, Mutation, Response } from '../native'; | ||
private txn; | ||
private responses; | ||
private finished; | ||
private immediate; | ||
constructor(txn: QueryTxn); | ||
private loop; | ||
private startPolling; | ||
query(query: string): Promise<Response>; | ||
@@ -15,5 +20,5 @@ queryWithVars(query: string, vars: { | ||
mutate(mutation: Mutation): Promise<Response>; | ||
commit(): Promise<void>; | ||
discard(): Promise<void>; | ||
commit(): Promise<Response>; | ||
discard(): Promise<Response>; | ||
private isMutated; | ||
} |
@@ -12,16 +12,49 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const errors_1 = require("./errors"); | ||
class Txn { | ||
constructor(txn) { | ||
this.txn = txn; | ||
this.responses = {}; | ||
this.finished = false; | ||
this.startPolling(); | ||
} | ||
loop() { | ||
if (this.finished) { | ||
return; | ||
} | ||
this.txn.poll((err, event) => { | ||
if (err) { | ||
if (err.message.indexOf('Poll Timeout Error') > -1) { | ||
this.startPolling(); | ||
return; | ||
} | ||
throw err; | ||
} | ||
if (this.responses[event.id]) { | ||
if (event.error) { | ||
this.responses[event.id][1](new Error(event.error)); | ||
} | ||
else { | ||
if (event.response) { | ||
this.responses[event.id][0](event.response); | ||
} | ||
} | ||
delete this.responses[event.id]; | ||
} | ||
this.startPolling(); | ||
}); | ||
} | ||
startPolling() { | ||
if (!this.finished) { | ||
this.immediate = setImmediate(this.loop.bind(this)); | ||
} | ||
else { | ||
clearImmediate(this.immediate); | ||
} | ||
} | ||
query(query) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve, reject) => { | ||
this.txn.query(query, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(result); | ||
}); | ||
const id = this.txn.query(query); | ||
this.responses[id] = [resolve, reject]; | ||
}); | ||
@@ -33,9 +66,4 @@ }); | ||
return new Promise((resolve, reject) => { | ||
this.txn.queryWithVars(query, vars, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(result); | ||
}); | ||
const id = this.txn.queryWithVars(query, vars); | ||
this.responses[id] = [resolve, reject]; | ||
}); | ||
@@ -49,13 +77,8 @@ }); | ||
return new Promise((resolve, reject) => { | ||
txn.mutate(mutation, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(result); | ||
}); | ||
const id = txn.mutate(mutation); | ||
this.responses[id] = [resolve, reject]; | ||
}); | ||
} | ||
else { | ||
return Promise.reject(new Error('txn is read-only')); | ||
return Promise.reject(errors_1.READ_ONLY_TXN); | ||
} | ||
@@ -69,13 +92,11 @@ }); | ||
return new Promise((resolve, reject) => { | ||
txn.commit((err) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(); | ||
}); | ||
const id = txn.commit(); | ||
this.responses[id] = [resolve, reject]; | ||
}).then((response) => { | ||
this.finished = true; | ||
return response; | ||
}); | ||
} | ||
else { | ||
return Promise.reject(new Error('txn is read-only')); | ||
return Promise.reject(errors_1.READ_ONLY_TXN); | ||
} | ||
@@ -89,13 +110,11 @@ }); | ||
return new Promise((resolve, reject) => { | ||
txn.discard((err) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(); | ||
}); | ||
const id = txn.discard(); | ||
this.responses[id] = [resolve, reject]; | ||
}).then((response) => { | ||
this.finished = true; | ||
return response; | ||
}); | ||
} | ||
else { | ||
return Promise.reject(new Error('txn is read-only')); | ||
return Promise.reject(errors_1.READ_ONLY_TXN); | ||
} | ||
@@ -102,0 +121,0 @@ }); |
declare namespace DgraphJsNative { | ||
export interface ResponseEvent { | ||
id: string; | ||
response?: Response; | ||
error?: string; | ||
} | ||
export class Response { | ||
@@ -19,10 +25,11 @@ getJson(): any; | ||
export interface QueryTxn { | ||
query(query: string, cb: (err: Error, result: Response) => void): void; | ||
queryWithVars(query: string, vars: { [key: string]: string }, cb: (err: Error, result: Response) => void): void; | ||
query(query: string): string; | ||
queryWithVars(query: string, vars: { [key: string]: string }): string; | ||
poll(cb: (err: Error, resp: ResponseEvent) => void): void; | ||
} | ||
export interface MutateTxn extends QueryTxn { | ||
mutate(mutation: Mutation, cb: (err: Error, result: Response) => void): void; | ||
commit(cb: (err: Error) => void): void; | ||
discard(cb: (err: Error) => void): void; | ||
mutate(mutation: Mutation): string; | ||
commit(): string; | ||
discard(): string; | ||
} | ||
@@ -29,0 +36,0 @@ |
{ | ||
"name": "dgraph-js-native", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "dGraph JS Native Client", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -23,3 +23,3 @@ # dgraph-js-native | ||
TODO. I just got the benchmark test going, and I am going to post a benchmark repo that works soon. | ||
[Benchmark Test Repo](https://github.com/xanthous-tech/dgraph-js-native-benchmarks) | ||
@@ -26,0 +26,0 @@ # Sponsor |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
16902
-18.25%16
-27.27%241
-23%