Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[plugin-data-harbor]: add pause / resume / reharbor method #110

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/page-spy-browser/src/plugins/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export class StoragePlugin implements PageSpyPlugin {
this.listenRefreshEvent();
this.onceInitPublicData();
this.initStorageProxy();
socketStore.addListener('harbor-clear', () => {
this.onceInitPublicData();
});
}

public onReset() {
Expand Down
3 changes: 3 additions & 0 deletions packages/page-spy-browser/src/plugins/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export default class SystemPlugin implements PageSpyPlugin {
reply(info);
}
});
socketStore.addListener('harbor-clear', () => {
this.onceInitPublicData();
});
}

public async onceInitPublicData() {
Expand Down
47 changes: 33 additions & 14 deletions packages/page-spy-plugin-data-harbor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export default class DataHarborPlugin implements PageSpyPlugin {

public apiBase: string = '';

public isPaused = false;

public $socketStore: SocketStoreBase | null = null;

public $pageSpyConfig: InitConfigBase | null = null;
Expand Down Expand Up @@ -118,7 +120,7 @@ export default class DataHarborPlugin implements PageSpyPlugin {
}

this.$socketStore.addListener('public-data', (message) => {
if (!this.isCaredPublicData(message)) return;
if (this.isPaused || !this.isCaredPublicData(message)) return;

const data = makeData(message.type, message.data);

Expand All @@ -135,16 +137,11 @@ export default class DataHarborPlugin implements PageSpyPlugin {

if (isBrowser()) {
const downloadBtn = buttonBindWithDownload(async () => {
const params = await this.getParams('download');
startDownload(params);
await this.onOfflineLog('download', false);
});
const uploadBtn = buttonBindWithUpload(async () => {
const params = await this.getParams('upload');
const result = await startUpload(params);
const debugUrl = this.getDebugUrl(result);
if (debugUrl) {
psLog.info(`${UPLOAD_TIPS.success}: ${debugUrl}`);
}
const debugUrl = await this.onOfflineLog('upload', false);
psLog.info(`${UPLOAD_TIPS.success}: ${debugUrl}`);
return debugUrl;
});

Expand Down Expand Up @@ -184,9 +181,12 @@ export default class DataHarborPlugin implements PageSpyPlugin {
}
}

onOfflineLog(type: 'download'): Promise<void>;
onOfflineLog(type: 'upload'): Promise<string>;
async onOfflineLog(type: FileAction): Promise<void | string> {
onOfflineLog(type: 'download', clearCache: boolean): Promise<void>;
onOfflineLog(type: 'upload', clearCache: boolean): Promise<string>;
async onOfflineLog(
type: FileAction,
clearCache = true,
): Promise<void | string> {
try {
let result;
if (type === 'download') {
Expand All @@ -198,8 +198,10 @@ export default class DataHarborPlugin implements PageSpyPlugin {
result = await startUpload(uploadArgs);
}

this.harbor.clear();
this.$socketStore?.dispatchEvent('harbor-clear', null);
if (clearCache) {
this.harbor.clear();
this.$socketStore?.dispatchEvent('harbor-clear', null);
}

if (result) {
return this.getDebugUrl(result);
Expand All @@ -219,6 +221,23 @@ export default class DataHarborPlugin implements PageSpyPlugin {
}
}

public pause() {
this.isPaused = true;
}

public resume() {
this.isPaused = false;
}

// Drop data in harbor and re-record
public reharbor() {
this.harbor.clear();
this.$socketStore?.dispatchEvent('harbor-clear', null);
if (this.isPaused) {
this.isPaused = false;
}
}

public isCaredPublicData(message: SpyMessage.MessageItem | MetricMessage) {
if (!message) return false;
const { type } = message;
Expand Down
Loading