@@ -2,8 +2,9 @@ import WBEdit from 'wikibase-edit'
2
2
import { WBK } from 'wikibase-sdk'
3
3
4
4
export default class WikibaseRepository {
5
- constructor ( origin , opts = { } ) {
5
+ constructor ( origin , opts = { abortController : new AbortController ( ) } ) {
6
6
this . origin = origin
7
+ this . abortController = opts . abortController
7
8
8
9
this . read = new WBK ( {
9
10
instance : origin
@@ -19,8 +20,16 @@ export default class WikibaseRepository {
19
20
}
20
21
}
21
22
23
+ async withCancellationContext ( thunk ) {
24
+ this . abortController . signal . throwIfAborted ( )
25
+ return thunk ( )
26
+ }
27
+
22
28
getContentLanguages ( ) {
23
- return fetch ( `${ this . origin } /w/api.php?action=query&meta=wbcontentlanguages&format=json` )
29
+ return fetch (
30
+ `${ this . origin } /w/api.php?action=query&meta=wbcontentlanguages&format=json` ,
31
+ { signal : this . abortController . signal }
32
+ )
24
33
. then ( r => r . json ( ) )
25
34
. then ( body => Object . keys ( body . query . wbcontentlanguages ) )
26
35
}
@@ -30,7 +39,7 @@ export default class WikibaseRepository {
30
39
return Promise . reject ( new Error ( 'Cannot edit a read only instance.' ) )
31
40
}
32
41
return Promise . allSettled ( entities . map ( async entity => {
33
- return this . edit . entity . create ( entity )
42
+ return this . withCancellationContext ( ( ) => this . edit . entity . create ( entity ) )
34
43
} ) )
35
44
. then ( ( results ) => {
36
45
for ( const result of results ) {
@@ -46,14 +55,16 @@ export default class WikibaseRepository {
46
55
return Promise . all ( identifiers . map ( async identifier => {
47
56
const [ entityId , revision ] = identifier . split ( '@' )
48
57
const url = revision
49
- ? await this . read . getEntityRevision ( {
58
+ ? await this . withCancellationContext ( ( ) => this . read . getEntityRevision ( {
50
59
id : entityId ,
51
60
revision
52
- } )
53
- : await this . read . getEntities ( {
61
+ } ) )
62
+ : await this . withCancellationContext ( ( ) => this . read . getEntities ( {
54
63
ids : [ entityId ]
55
- } )
56
- const { entities } = await fetch ( url ) . then ( res => res . json ( ) )
64
+ } ) )
65
+ const { entities } = await fetch (
66
+ url , { signal : this . abortController . signal }
67
+ ) . then ( res => res . json ( ) )
57
68
return entities [ entityId ]
58
69
} ) )
59
70
}
0 commit comments