1
1
import debug from 'debug' ;
2
2
import { QueryTxn , MutateTxn , Mutation , ResponseEvent , Response as NativeResponse } from '../native' ;
3
3
import { Response } from './response' ;
4
- import { READ_ONLY_TXN } from './errors' ;
4
+ import { READ_ONLY_TXN , ALREADY_FINISHED } from './errors' ;
5
5
6
6
const log = debug ( 'dgraph-js-native:txn' ) ;
7
7
@@ -67,6 +67,9 @@ export class Txn {
67
67
68
68
public async query ( query : string ) : Promise < Response > {
69
69
log ( 'query' , query ) ;
70
+
71
+ await this . checkIsFinished ( ) ;
72
+
70
73
return new Promise ( ( resolve , reject ) => {
71
74
const id = this . txn . query ( query ) ;
72
75
this . responses [ id ] = [ resolve , reject ] ;
@@ -75,6 +78,9 @@ export class Txn {
75
78
76
79
public async queryWithVars ( query : string , vars : { [ key : string ] : any } = { } ) : Promise < Response > {
77
80
log ( 'queryWithVars' , query , vars ) ;
81
+
82
+ await this . checkIsFinished ( ) ;
83
+
78
84
return new Promise ( ( resolve , reject ) => {
79
85
const id = this . txn . queryWithVars ( query , vars ) ;
80
86
this . responses [ id ] = [ resolve , reject ] ;
@@ -83,6 +89,9 @@ export class Txn {
83
89
84
90
public async mutate ( mutation : Mutation ) : Promise < Response > {
85
91
log ( 'mutate' , mutation ) ;
92
+
93
+ await this . checkIsFinished ( ) ;
94
+
86
95
const txn = this . txn ;
87
96
if ( this . isMutated ( txn ) ) {
88
97
return new Promise ( ( resolve , reject ) => {
@@ -96,6 +105,9 @@ export class Txn {
96
105
97
106
public async upsert ( query : string , mutation : Mutation , commitNow ?: boolean ) : Promise < Response > {
98
107
log ( 'upsert' , query , mutation ) ;
108
+
109
+ await this . checkIsFinished ( ) ;
110
+
99
111
const txn = this . txn ;
100
112
if ( this . isMutated ( txn ) ) {
101
113
return new Promise ( ( resolve , reject ) => {
@@ -109,6 +121,9 @@ export class Txn {
109
121
110
122
public async upsertWithVars ( query : string , mutation : Mutation , vars : { [ key : string ] : any } = { } , commitNow ?: boolean ) : Promise < Response > {
111
123
log ( 'upsertWithVars' , query , mutation , vars ) ;
124
+
125
+ await this . checkIsFinished ( ) ;
126
+
112
127
const txn = this . txn ;
113
128
if ( this . isMutated ( txn ) ) {
114
129
return new Promise ( ( resolve , reject ) => {
@@ -122,6 +137,9 @@ export class Txn {
122
137
123
138
public async commit ( ) : Promise < Response > {
124
139
log ( 'commit' ) ;
140
+
141
+ await this . checkIsFinished ( ) ;
142
+
125
143
const txn = this . txn ;
126
144
if ( this . isMutated ( txn ) ) {
127
145
return new Promise ( ( resolve , reject ) => {
@@ -138,6 +156,9 @@ export class Txn {
138
156
139
157
public async discard ( ) : Promise < Response > {
140
158
log ( 'discard' ) ;
159
+
160
+ await this . checkIsFinished ( ) ;
161
+
141
162
const txn = this . txn ;
142
163
if ( this . isMutated ( txn ) ) {
143
164
return new Promise ( ( resolve , reject ) => {
@@ -152,6 +173,13 @@ export class Txn {
152
173
}
153
174
}
154
175
176
+ private checkIsFinished ( ) : Promise < void > {
177
+ if ( this . finished ) {
178
+ return Promise . reject ( ALREADY_FINISHED ) ;
179
+ }
180
+ return Promise . resolve ( ) ;
181
+ }
182
+
155
183
private isMutated ( txn : QueryTxn | MutateTxn ) : txn is MutateTxn {
156
184
return (
157
185
typeof ( txn as MutateTxn ) . mutate === 'function' &&
0 commit comments