11import debug from 'debug' ;
22import { QueryTxn , MutateTxn , Mutation , ResponseEvent , Response as NativeResponse } from '../native' ;
33import { Response } from './response' ;
4- import { READ_ONLY_TXN } from './errors' ;
4+ import { READ_ONLY_TXN , ALREADY_FINISHED } from './errors' ;
55
66const 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