Skip to content

Commit

Permalink
batching
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Jan 31, 2025
1 parent d53f6f0 commit fc27552
Show file tree
Hide file tree
Showing 14 changed files with 783 additions and 498 deletions.
300 changes: 149 additions & 151 deletions special-pages/pages/new-tab/app/activity/ActivityProvider.js

Large diffs are not rendered by default.

166 changes: 0 additions & 166 deletions special-pages/pages/new-tab/app/activity/activity.service.js

This file was deleted.

100 changes: 82 additions & 18 deletions special-pages/pages/new-tab/app/activity/batched-activity.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,55 @@
* @typedef {import("../../types/new-tab.js").ActivityConfig} ActivityConfig
* @typedef {import("../../types/new-tab.js").UrlInfo} UrlInfo
* @typedef {import("../../types/new-tab.js").PatchData} PatchData
* @typedef {import('../../types/new-tab.js').DomainActivity} DomainActivity
*/
import { Service } from '../service.js';

export class BatchedActivity {
SIZE = 5;
export class BatchedActivityService {
INITIAL = 5;
CHUNK_SIZE = 10;
isFetchingNext = false;
/**
* @param {import("../../src/index.js").NewTabPage} ntp - The internal data feed, expected to have a `subscribe` method.
* @param {boolean} batched
* @internal
*/
constructor(ntp) {
constructor(ntp, batched = false) {
this.ntp = ntp;
this.batched = batched;

/** @type {Service<import('../../types/new-tab.js').UrlInfo>} */
this.urlService = new Service({
initial: () => this.ntp.messaging.request('activity_getUrls'),
initial: () => {
if (this.batched) {
return this.ntp.messaging.request('activity_getUrls');
} else {
/** @type {UrlInfo} */
const next = {
urls: [],
totalTrackersBlocked: 0,
};
return Promise.resolve(next);
}
},
subscribe: (cb) => ntp.messaging.subscribe('activity_onDataPatch', cb),
});

/** @type {Service<ActivityData>} */
this.dataService = new Service({
initial: (params) => {
if (!this.urlService.data) throw new Error('unreachable');
return this.ntp.messaging.request('activity_getDataForUrls', { urls: params.urls });
if (this.batched) {
return this.ntp.messaging.request('activity_getDataForUrls', { urls: params.urls });
} else {
return this.ntp.messaging.request('activity_getData');
}
},
subscribe: (cb) => ntp.messaging.subscribe('activity_onDataUpdate', cb),
}).withUpdater((old, next) => {
if (this.batched) {
return { activity: old.activity.concat(next.activity) };
}
return next;
});

/** @type {Service<ActivityConfig>} */
Expand All @@ -42,6 +66,7 @@ export class BatchedActivity {
this.burnUnsub = this.ntp.messaging.subscribe('activity_onBurnComplete', () => {
this.burns?.dispatchEvent(new CustomEvent('activity_onBurnComplete'));
});
this.patchesSub = this.onPatchData();
}

name() {
Expand All @@ -53,11 +78,19 @@ export class BatchedActivity {
* @internal
*/
async getInitial() {
const configPromise = this.configService.fetchInitial();
const urlsPromise = this.urlService.fetchInitial();
const [config, urlData] = await Promise.all([configPromise, urlsPromise]);
const data = await this.dataService.fetchInitial({ urls: urlData.urls.slice(0, this.SIZE) });
return { config, data };
if (this.batched) {
const configPromise = this.configService.fetchInitial();
const urlsPromise = this.urlService.fetchInitial();
const [config, urlData] = await Promise.all([configPromise, urlsPromise]);
const data = await this.dataService.fetchInitial({ urls: urlData.urls.slice(0, this.INITIAL) });
return { config, data };
} else {
const configPromise = this.configService.fetchInitial();
const dataPromise = this.dataService.fetchInitial();
const urlInfoPromise = this.urlService.fetchInitial();
const [config, data] = await Promise.all([configPromise, dataPromise, urlInfoPromise]);
return { config, data };
}
}

/**
Expand All @@ -67,6 +100,7 @@ export class BatchedActivity {
this.configService.destroy();
this.dataService.destroy();
this.burnUnsub();
this.patchesSub();
this.burns = null;
}

Expand All @@ -75,10 +109,8 @@ export class BatchedActivity {
*/
next(urls) {
if (!this.urlService.data) throw new Error('unreachable');
this.dataService.triggerFetch({ urls });
}

triggerFetchChunk(urls) {
if (urls.length === 0) return;
this.isFetchingNext = true;
this.dataService.triggerFetch({ urls });
}

Expand All @@ -87,19 +119,39 @@ export class BatchedActivity {
* @internal
*/
onUrlData(cb) {
return this.urlService.onData(cb);
return this.urlService.onData((params) => {
if ('patch' in params.data) return console.log('ignoring patch');
cb(params);
});
}

/**
* @internal
*/
onPatchData() {
return this.urlService.onData((params) => {
if (!('patch' in params.data)) return console.log('ignoring none-patch');
this.dataService.publish({ activity: [/** @type {DomainActivity} */ (params.data.patch)] });
});
}

/**
* @param {(evt: {data: ActivityData, source: 'manual' | 'subscription'}) => void} cb
* @internal
*/
onData(cb) {
return this.dataService.onData(cb);
return this.dataService.onData((data) => {
this.isFetchingNext = false;
cb(data);
});
}

triggerDataFetch() {
return this.dataService.triggerFetch();
if (this.batched) {
this.urlService.triggerFetch();
} else {
this.dataService.triggerFetch();
}
}

/**
Expand Down Expand Up @@ -172,6 +224,18 @@ export class BatchedActivity {
}),
};
});
this.urlService.update((old) => {
return {
...old,
urls: old.urls.filter((x) => x !== url),
};
});
this.ntp.messaging.notify('activity_removeItem', { url });
}
/**
* @param {string} url
*/
removeOnly(url) {
this.ntp.messaging.notify('activity_removeItem', { url });
}
/**
Expand Down
Loading

0 comments on commit fc27552

Please sign in to comment.