@@ -100,6 +100,9 @@ class QueryBuilder {
100
100
last : ?number ,
101
101
cursorComparator : ?CursorComparator ,
102
102
} ;
103
+ lockContext: {
104
+ queryBuilder : QueryBuilder ,
105
+ } ;
103
106
104
107
constructor (
105
108
options : QueryBuilderOptions = { } ,
@@ -182,6 +185,9 @@ class QueryBuilder {
182
185
this . lock ( "limit" ) ;
183
186
this . lock ( "offset" ) ;
184
187
} ) ;
188
+ this . lockContext = Object . freeze ( {
189
+ queryBuilder : this ,
190
+ } ) ;
185
191
}
186
192
187
193
// ----------------------------------------
@@ -702,14 +708,13 @@ class QueryBuilder {
702
708
}
703
709
lock ( type : string ) {
704
710
if ( this . locks [ type ] ) return ;
705
- const getContext = ( ) = > ( {
706
- queryBuilder : this ,
707
- } ) ;
708
- const beforeLocks = this . data . beforeLock [ type ] ;
709
- if ( beforeLocks && beforeLocks . length ) {
710
- this . data . beforeLock [ type ] = null ;
711
- for ( const fn of beforeLocks ) {
712
- fn ( ) ;
711
+ const context = this . lockContext ;
712
+ const { beforeLock } = this . data ;
713
+ let locks = beforeLock [ type ] ;
714
+ if ( locks ) {
715
+ beforeLock [ type ] = [ ] ;
716
+ for ( let i = 0 , l = locks . length ; i < l ; i ++ ) {
717
+ locks [ i ] ( ) ;
713
718
}
714
719
}
715
720
if ( type !== "select" ) {
@@ -720,7 +725,6 @@ class QueryBuilder {
720
725
this . compiledData [ type ] = this . data [ type ] ;
721
726
} else if ( type === "whereBound" ) {
722
727
// Handle properties separately
723
- const context = getContext ( ) ;
724
728
this . compiledData [ type ] . lower = callIfNecessaryArray (
725
729
this . data [ type ] . lower ,
726
730
context
@@ -739,7 +743,6 @@ class QueryBuilder {
739
743
// Assume that duplicate fields must be identical, don't output the same
740
744
// key multiple times
741
745
const seenFields = { } ;
742
- const context = getContext ( ) ;
743
746
const data = [ ] ;
744
747
const selects = this . data [ type ] ;
745
748
@@ -751,19 +754,18 @@ class QueryBuilder {
751
754
// $FlowFixMe
752
755
seenFields [ columnName ] = true ;
753
756
data . push ( [ callIfNecessary ( valueOrGenerator , context ) , columnName ] ) ;
754
- const newBeforeLocks = this . data . beforeLock [ type ] ;
755
- if ( newBeforeLocks && newBeforeLocks . length ) {
756
- this . data . beforeLock [ type ] = null ;
757
- for ( const fn of newBeforeLocks ) {
758
- fn ( ) ;
757
+ locks = beforeLock [ type ] ;
758
+ if ( locks ) {
759
+ beforeLock [ type ] = [ ] ;
760
+ for ( let i = 0 , l = locks . length ; i < l ; i ++ ) {
761
+ locks [ i ] ( ) ;
759
762
}
760
763
}
761
764
}
762
765
}
763
766
this . locks [ type ] = isDev ? new Error ( "Initally locked here" ) . stack : true ;
764
767
this . compiledData [ type ] = data ;
765
768
} else if ( type === "orderBy" ) {
766
- const context = getContext ( ) ;
767
769
this . compiledData [ type ] = this . data [ type ] . map ( ( [ a , b , c ] ) => [
768
770
callIfNecessary ( a , context ) ,
769
771
b ,
@@ -772,24 +774,19 @@ class QueryBuilder {
772
774
} else if ( type === "from" ) {
773
775
if ( this . data . from ) {
774
776
const f = this . data . from ;
775
- const context = getContext ( ) ;
776
777
this . compiledData . from = [ callIfNecessary ( f [ 0 ] , context ) , f [ 1 ] ] ;
777
778
}
778
779
} else if ( type === "join" || type === "where" ) {
779
- const context = getContext ( ) ;
780
780
this . compiledData [ type ] = callIfNecessaryArray ( this . data [ type ] , context ) ;
781
781
} else if ( type === "selectCursor" ) {
782
- const context = getContext ( ) ;
783
782
this . compiledData [ type ] = callIfNecessary ( this . data [ type ] , context ) ;
784
783
} else if ( type === "cursorPrefix" ) {
785
784
this . compiledData [ type ] = this . data [ type ] ;
786
785
} else if ( type === "orderIsUnique" ) {
787
786
this . compiledData [ type ] = this . data [ type ] ;
788
787
} else if ( type === "limit" ) {
789
- const context = getContext ( ) ;
790
788
this . compiledData [ type ] = callIfNecessary ( this . data [ type ] , context ) ;
791
789
} else if ( type === "offset" ) {
792
- const context = getContext ( ) ;
793
790
this . compiledData [ type ] = callIfNecessary ( this . data [ type ] , context ) ;
794
791
} else if ( type === "first" ) {
795
792
this . compiledData [ type ] = this . data [ type ] ;
0 commit comments