33 * @typedef {import("../../types/new-tab.js").ActivityConfig } ActivityConfig
44 * @typedef {import("../../types/new-tab.js").UrlInfo } UrlInfo
55 * @typedef {import("../../types/new-tab.js").PatchData } PatchData
6+ * @typedef {import('../../types/new-tab.js').DomainActivity } DomainActivity
67 */
78import { Service } from '../service.js' ;
89
9- export class BatchedActivity {
10- SIZE = 5 ;
10+ export class BatchedActivityService {
11+ INITIAL = 5 ;
12+ CHUNK_SIZE = 10 ;
13+ isFetchingNext = false ;
1114 /**
1215 * @param {import("../../src/index.js").NewTabPage } ntp - The internal data feed, expected to have a `subscribe` method.
16+ * @param {boolean } batched
1317 * @internal
1418 */
15- constructor ( ntp ) {
19+ constructor ( ntp , batched = false ) {
1620 this . ntp = ntp ;
21+ this . batched = batched ;
1722
1823 /** @type {Service<import('../../types/new-tab.js').UrlInfo> } */
1924 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+ } ,
2137 subscribe : ( cb ) => ntp . messaging . subscribe ( 'activity_onDataPatch' , cb ) ,
2238 } ) ;
2339
2440 /** @type {Service<ActivityData> } */
2541 this . dataService = new Service ( {
2642 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+ }
2948 } ,
3049 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 ;
3155 } ) ;
3256
3357 /** @type {Service<ActivityConfig> } */
@@ -42,6 +66,7 @@ export class BatchedActivity {
4266 this . burnUnsub = this . ntp . messaging . subscribe ( 'activity_onBurnComplete' , ( ) => {
4367 this . burns ?. dispatchEvent ( new CustomEvent ( 'activity_onBurnComplete' ) ) ;
4468 } ) ;
69+ this . patchesSub = this . onPatchData ( ) ;
4570 }
4671
4772 name ( ) {
@@ -53,11 +78,19 @@ export class BatchedActivity {
5378 * @internal
5479 */
5580 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+ }
6194 }
6295
6396 /**
@@ -67,6 +100,7 @@ export class BatchedActivity {
67100 this . configService . destroy ( ) ;
68101 this . dataService . destroy ( ) ;
69102 this . burnUnsub ( ) ;
103+ this . patchesSub ( ) ;
70104 this . burns = null ;
71105 }
72106
@@ -75,10 +109,8 @@ export class BatchedActivity {
75109 */
76110 next ( urls ) {
77111 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 ;
82114 this . dataService . triggerFetch ( { urls } ) ;
83115 }
84116
@@ -87,19 +119,39 @@ export class BatchedActivity {
87119 * @internal
88120 */
89121 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+ } ) ;
91136 }
92137
93138 /**
94139 * @param {(evt: {data: ActivityData, source: 'manual' | 'subscription'}) => void } cb
95140 * @internal
96141 */
97142 onData ( cb ) {
98- return this . dataService . onData ( cb ) ;
143+ return this . dataService . onData ( ( data ) => {
144+ this . isFetchingNext = false ;
145+ cb ( data ) ;
146+ } ) ;
99147 }
100148
101149 triggerDataFetch ( ) {
102- return this . dataService . triggerFetch ( ) ;
150+ if ( this . batched ) {
151+ this . urlService . triggerFetch ( ) ;
152+ } else {
153+ this . dataService . triggerFetch ( ) ;
154+ }
103155 }
104156
105157 /**
@@ -172,6 +224,18 @@ export class BatchedActivity {
172224 } ) ,
173225 } ;
174226 } ) ;
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 ) {
175239 this . ntp . messaging . notify ( 'activity_removeItem' , { url } ) ;
176240 }
177241 /**
0 commit comments