3
3
* @typedef {import("../../types/new-tab.js").ActivityConfig } ActivityConfig
4
4
* @typedef {import("../../types/new-tab.js").UrlInfo } UrlInfo
5
5
* @typedef {import("../../types/new-tab.js").PatchData } PatchData
6
+ * @typedef {import('../../types/new-tab.js').DomainActivity } DomainActivity
6
7
*/
7
8
import { Service } from '../service.js' ;
8
9
9
- export class BatchedActivity {
10
- SIZE = 5 ;
10
+ export class BatchedActivityService {
11
+ INITIAL = 5 ;
12
+ CHUNK_SIZE = 10 ;
13
+ isFetchingNext = false ;
11
14
/**
12
15
* @param {import("../../src/index.js").NewTabPage } ntp - The internal data feed, expected to have a `subscribe` method.
16
+ * @param {boolean } batched
13
17
* @internal
14
18
*/
15
- constructor ( ntp ) {
19
+ constructor ( ntp , batched = false ) {
16
20
this . ntp = ntp ;
21
+ this . batched = batched ;
17
22
18
23
/** @type {Service<import('../../types/new-tab.js').UrlInfo> } */
19
24
this . urlService = new Service ( {
20
- initial : ( ) => this . ntp . messaging . request ( 'activity_getUrls' ) ,
25
+ initial : ( ) => {
26
+ if ( this . batched ) {
27
+ return this . ntp . messaging . request ( 'activity_getUrls' ) ;
28
+ } else {
29
+ /** @type {UrlInfo } */
30
+ const next = {
31
+ urls : [ ] ,
32
+ totalTrackersBlocked : 0 ,
33
+ } ;
34
+ return Promise . resolve ( next ) ;
35
+ }
36
+ } ,
21
37
subscribe : ( cb ) => ntp . messaging . subscribe ( 'activity_onDataPatch' , cb ) ,
22
38
} ) ;
23
39
24
40
/** @type {Service<ActivityData> } */
25
41
this . dataService = new Service ( {
26
42
initial : ( params ) => {
27
- if ( ! this . urlService . data ) throw new Error ( 'unreachable' ) ;
28
- return this . ntp . messaging . request ( 'activity_getDataForUrls' , { urls : params . urls } ) ;
43
+ if ( this . batched ) {
44
+ return this . ntp . messaging . request ( 'activity_getDataForUrls' , { urls : params . urls } ) ;
45
+ } else {
46
+ return this . ntp . messaging . request ( 'activity_getData' ) ;
47
+ }
29
48
} ,
30
49
subscribe : ( cb ) => ntp . messaging . subscribe ( 'activity_onDataUpdate' , cb ) ,
50
+ } ) . withUpdater ( ( old , next ) => {
51
+ if ( this . batched ) {
52
+ return { activity : old . activity . concat ( next . activity ) } ;
53
+ }
54
+ return next ;
31
55
} ) ;
32
56
33
57
/** @type {Service<ActivityConfig> } */
@@ -42,6 +66,7 @@ export class BatchedActivity {
42
66
this . burnUnsub = this . ntp . messaging . subscribe ( 'activity_onBurnComplete' , ( ) => {
43
67
this . burns ?. dispatchEvent ( new CustomEvent ( 'activity_onBurnComplete' ) ) ;
44
68
} ) ;
69
+ this . patchesSub = this . onPatchData ( ) ;
45
70
}
46
71
47
72
name ( ) {
@@ -53,11 +78,19 @@ export class BatchedActivity {
53
78
* @internal
54
79
*/
55
80
async getInitial ( ) {
56
- const configPromise = this . configService . fetchInitial ( ) ;
57
- const urlsPromise = this . urlService . fetchInitial ( ) ;
58
- const [ config , urlData ] = await Promise . all ( [ configPromise , urlsPromise ] ) ;
59
- const data = await this . dataService . fetchInitial ( { urls : urlData . urls . slice ( 0 , this . SIZE ) } ) ;
60
- return { config, data } ;
81
+ if ( this . batched ) {
82
+ const configPromise = this . configService . fetchInitial ( ) ;
83
+ const urlsPromise = this . urlService . fetchInitial ( ) ;
84
+ const [ config , urlData ] = await Promise . all ( [ configPromise , urlsPromise ] ) ;
85
+ const data = await this . dataService . fetchInitial ( { urls : urlData . urls . slice ( 0 , this . INITIAL ) } ) ;
86
+ return { config, data } ;
87
+ } else {
88
+ const configPromise = this . configService . fetchInitial ( ) ;
89
+ const dataPromise = this . dataService . fetchInitial ( ) ;
90
+ const urlInfoPromise = this . urlService . fetchInitial ( ) ;
91
+ const [ config , data ] = await Promise . all ( [ configPromise , dataPromise , urlInfoPromise ] ) ;
92
+ return { config, data } ;
93
+ }
61
94
}
62
95
63
96
/**
@@ -67,6 +100,7 @@ export class BatchedActivity {
67
100
this . configService . destroy ( ) ;
68
101
this . dataService . destroy ( ) ;
69
102
this . burnUnsub ( ) ;
103
+ this . patchesSub ( ) ;
70
104
this . burns = null ;
71
105
}
72
106
@@ -75,10 +109,8 @@ export class BatchedActivity {
75
109
*/
76
110
next ( urls ) {
77
111
if ( ! this . urlService . data ) throw new Error ( 'unreachable' ) ;
78
- this . dataService . triggerFetch ( { urls } ) ;
79
- }
80
-
81
- triggerFetchChunk ( urls ) {
112
+ if ( urls . length === 0 ) return ;
113
+ this . isFetchingNext = true ;
82
114
this . dataService . triggerFetch ( { urls } ) ;
83
115
}
84
116
@@ -87,19 +119,39 @@ export class BatchedActivity {
87
119
* @internal
88
120
*/
89
121
onUrlData ( cb ) {
90
- return this . urlService . onData ( cb ) ;
122
+ return this . urlService . onData ( ( params ) => {
123
+ if ( 'patch' in params . data ) return console . log ( 'ignoring patch' ) ;
124
+ cb ( params ) ;
125
+ } ) ;
126
+ }
127
+
128
+ /**
129
+ * @internal
130
+ */
131
+ onPatchData ( ) {
132
+ return this . urlService . onData ( ( params ) => {
133
+ if ( ! ( 'patch' in params . data ) ) return console . log ( 'ignoring none-patch' ) ;
134
+ this . dataService . publish ( { activity : [ /** @type {DomainActivity } */ ( params . data . patch ) ] } ) ;
135
+ } ) ;
91
136
}
92
137
93
138
/**
94
139
* @param {(evt: {data: ActivityData, source: 'manual' | 'subscription'}) => void } cb
95
140
* @internal
96
141
*/
97
142
onData ( cb ) {
98
- return this . dataService . onData ( cb ) ;
143
+ return this . dataService . onData ( ( data ) => {
144
+ this . isFetchingNext = false ;
145
+ cb ( data ) ;
146
+ } ) ;
99
147
}
100
148
101
149
triggerDataFetch ( ) {
102
- return this . dataService . triggerFetch ( ) ;
150
+ if ( this . batched ) {
151
+ this . urlService . triggerFetch ( ) ;
152
+ } else {
153
+ this . dataService . triggerFetch ( ) ;
154
+ }
103
155
}
104
156
105
157
/**
@@ -172,6 +224,18 @@ export class BatchedActivity {
172
224
} ) ,
173
225
} ;
174
226
} ) ;
227
+ this . urlService . update ( ( old ) => {
228
+ return {
229
+ ...old ,
230
+ urls : old . urls . filter ( ( x ) => x !== url ) ,
231
+ } ;
232
+ } ) ;
233
+ this . ntp . messaging . notify ( 'activity_removeItem' , { url } ) ;
234
+ }
235
+ /**
236
+ * @param {string } url
237
+ */
238
+ removeOnly ( url ) {
175
239
this . ntp . messaging . notify ( 'activity_removeItem' , { url } ) ;
176
240
}
177
241
/**
0 commit comments