Skip to content

Commit a4a2384

Browse files
committed
added an finished check in txn, since it stops polling for results when finished
1 parent 284591d commit a4a2384

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const ERR_BEST_EFFORT_REQUIRED_READ_ONLY = new Error('Best effort only works for read-only queries');
22
export const READ_ONLY_TXN = new Error('Txn is read-only');
3+
export const ALREADY_FINISHED = new Error('Txn already finished');

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { Mutation, Operation };
44
export { DgraphClient } from './client';
55
export { Txn } from './txn';
66
export { Response } from './response';
7+
export * from './errors';

src/txn.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import debug from 'debug';
22
import { QueryTxn, MutateTxn, Mutation, ResponseEvent, Response as NativeResponse } from '../native';
33
import { Response } from './response';
4-
import { READ_ONLY_TXN } from './errors';
4+
import { READ_ONLY_TXN, ALREADY_FINISHED } from './errors';
55

66
const log = debug('dgraph-js-native:txn');
77

@@ -67,6 +67,9 @@ export class Txn {
6767

6868
public async query(query: string): Promise<Response> {
6969
log('query', query);
70+
71+
await this.checkIsFinished();
72+
7073
return new Promise((resolve, reject) => {
7174
const id = this.txn.query(query);
7275
this.responses[id] = [resolve, reject];
@@ -75,6 +78,9 @@ export class Txn {
7578

7679
public async queryWithVars(query: string, vars: { [key: string]: any } = {}): Promise<Response> {
7780
log('queryWithVars', query, vars);
81+
82+
await this.checkIsFinished();
83+
7884
return new Promise((resolve, reject) => {
7985
const id = this.txn.queryWithVars(query, vars);
8086
this.responses[id] = [resolve, reject];
@@ -83,6 +89,9 @@ export class Txn {
8389

8490
public async mutate(mutation: Mutation): Promise<Response> {
8591
log('mutate', mutation);
92+
93+
await this.checkIsFinished();
94+
8695
const txn = this.txn;
8796
if (this.isMutated(txn)) {
8897
return new Promise((resolve, reject) => {
@@ -96,6 +105,9 @@ export class Txn {
96105

97106
public async upsert(query: string, mutation: Mutation, commitNow?: boolean): Promise<Response> {
98107
log('upsert', query, mutation);
108+
109+
await this.checkIsFinished();
110+
99111
const txn = this.txn;
100112
if (this.isMutated(txn)) {
101113
return new Promise((resolve, reject) => {
@@ -109,6 +121,9 @@ export class Txn {
109121

110122
public async upsertWithVars(query: string, mutation: Mutation, vars: { [key: string]: any } = {}, commitNow?: boolean): Promise<Response> {
111123
log('upsertWithVars', query, mutation, vars);
124+
125+
await this.checkIsFinished();
126+
112127
const txn = this.txn;
113128
if (this.isMutated(txn)) {
114129
return new Promise((resolve, reject) => {
@@ -122,6 +137,9 @@ export class Txn {
122137

123138
public async commit(): Promise<Response> {
124139
log('commit');
140+
141+
await this.checkIsFinished();
142+
125143
const txn = this.txn;
126144
if (this.isMutated(txn)) {
127145
return new Promise((resolve, reject) => {
@@ -138,6 +156,9 @@ export class Txn {
138156

139157
public async discard(): Promise<Response> {
140158
log('discard');
159+
160+
await this.checkIsFinished();
161+
141162
const txn = this.txn;
142163
if (this.isMutated(txn)) {
143164
return new Promise((resolve, reject) => {
@@ -152,6 +173,13 @@ export class Txn {
152173
}
153174
}
154175

176+
private checkIsFinished(): Promise<void> {
177+
if (this.finished) {
178+
return Promise.reject(ALREADY_FINISHED);
179+
}
180+
return Promise.resolve();
181+
}
182+
155183
private isMutated(txn: QueryTxn | MutateTxn): txn is MutateTxn {
156184
return (
157185
typeof (txn as MutateTxn).mutate === 'function' &&

0 commit comments

Comments
 (0)