1- import { ApolloLink , Observable } from "apollo-link" ;
1+ import {
2+ ApolloLink ,
3+ Observable ,
4+ Operation ,
5+ FetchResult ,
6+ NextLink
7+ } from "apollo-link" ;
8+ import { Observer } from "zen-observable-ts" ;
29
3- const toRequestKey = operation => {
10+ interface OperationQueueEntry {
11+ operation : Operation ;
12+ forward : NextLink ;
13+ observer : Observer < FetchResult > ;
14+ subscription ?: { unsubscribe : ( ) => void } ;
15+ }
16+
17+ const toRequestKey = ( operation : Operation ) => {
418 return operation . operationName ;
519} ;
620
@@ -10,23 +24,25 @@ const toRequestKey = operation => {
1024 * To skip the queue pass `{ context: { skipQueue: true } }` to your mutation.
1125 */
1226export default class MutationQueueLink extends ApolloLink {
27+ private opQueue : OperationQueueEntry [ ] = [ ] ;
28+ private inProcess : Boolean = false ;
29+ private debug : Boolean = false ;
30+
1331 /**
14- * @param {Boolean } props. debug - set to true to enable logging
32+ * @param {Boolean } debug - set to true to enable logging
1533 */
16- constructor ( { debug = true } = { } ) {
34+ constructor ( { debug = false } = { } ) {
1735 super ( ) ;
18- this . opQueue = [ ] ;
19- this . inProcess = false ;
2036 this . debug = debug ;
2137 }
2238
23- log ( message , ...rest ) {
39+ private log ( message : String , ...rest : String [ ] ) {
2440 if ( this . debug ) {
2541 console . log ( message , ...rest ) ;
2642 }
2743 }
2844
29- processOperation ( entry ) {
45+ private processOperation ( entry : OperationQueueEntry ) {
3046 const { operation, forward, observer } = entry ;
3147 this . inProcess = true ;
3248 this . log ( "[PROCESSING] -" , toRequestKey ( operation ) ) ;
@@ -53,7 +69,16 @@ export default class MutationQueueLink extends ApolloLink {
5369 } ) ;
5470 }
5571
56- request ( operation , forward ) {
72+ private cancelOperation ( entry : OperationQueueEntry ) {
73+ this . opQueue = this . opQueue . filter ( e => e !== entry ) ;
74+ }
75+
76+ private enqueue ( entry : OperationQueueEntry ) {
77+ this . log ( "[ENQUEUE] -" , toRequestKey ( entry . operation ) ) ;
78+ this . opQueue . push ( entry ) ;
79+ }
80+
81+ public request ( operation : Operation , forward : NextLink ) {
5782 // Enqueue all mutations unless manually skipped.
5883 if (
5984 operation . toKey ( ) . includes ( '"operation":"mutation"' ) &&
@@ -64,21 +89,12 @@ export default class MutationQueueLink extends ApolloLink {
6489 if ( ! this . inProcess ) {
6590 this . processOperation ( operationEntry ) ;
6691 } else {
67- this . log ( "[ENQUEUE] -" , toRequestKey ( operation ) ) ;
68- this . opQueue . push ( operationEntry ) ;
92+ this . enqueue ( operationEntry ) ;
6993 }
7094 return ( ) => this . cancelOperation ( operationEntry ) ;
7195 } ) ;
7296 } else {
7397 return forward ( operation ) ;
7498 }
7599 }
76-
77- cancelOperation ( entry ) {
78- this . opQueue = this . opQueue . filter ( e => e !== entry ) ;
79- }
80-
81- enqueue ( entry ) {
82- this . opQueue . push ( entry ) ;
83- }
84100}
0 commit comments