Skip to content

Commit

Permalink
feat: ensure push latest table records
Browse files Browse the repository at this point in the history
  • Loading branch information
siam-ese committed Jan 10, 2025
1 parent 99c1bb8 commit 76bc1fa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
7 changes: 2 additions & 5 deletions packages/scraper/lib/tools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PushStorageMessage } from '@univer-clipsheet-core/shared';
import { ClipsheetMessageTypeEnum, isFunction, promisifyMessage } from '@univer-clipsheet-core/shared';
import { ClipsheetMessageTypeEnum, isFunction, promisifyMessage, sendGetStorageMessage } from '@univer-clipsheet-core/shared';
import { AutoExtractionMode, type IScraper } from './scraper';
import { ScraperStorageKeyEnum } from './scraper-service';

Expand All @@ -26,10 +26,7 @@ export const setCurrentScraper = (setScraper: IScraper | ((old: IScraper) => ISc
});
};
if (isFunction(setScraper)) {
chrome.runtime.sendMessage({
type: ClipsheetMessageTypeEnum.GetStorage,
payload: ScraperStorageKeyEnum.CurrentScraper,
});
sendGetStorageMessage(ScraperStorageKeyEnum.CurrentScraper);

promisifyMessage<PushStorageMessage<IScraper>>((msg) => msg.type === ClipsheetMessageTypeEnum.PushStorage && msg.payload.key === ScraperStorageKeyEnum.CurrentScraper)
.then((res) => {
Expand Down
11 changes: 10 additions & 1 deletion packages/shared/lib/tools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { nanoid } from 'nanoid';
import type { ChannelConnectedMessage, ConnectChannelMessage } from './channel.message';
import { ChannelMessageTypeEnum } from './channel.message';
import type { GetDataSourceMessage, PushDataSourceMessage } from './common.message';
import type { GetDataSourceMessage, GetStorageMessage, PushDataSourceMessage } from './common.message';
import { ClipsheetMessageTypeEnum } from './common.message';

export const generateRandomId = () => nanoid();
Expand Down Expand Up @@ -86,6 +86,15 @@ export function sendSetStorageMessage(key: string, payload: any) {
});
}

export function sendGetStorageMessage(key: string) {
const msg: GetStorageMessage = {
type: ClipsheetMessageTypeEnum.GetStorage,
payload: key,
};

chrome.runtime.sendMessage(msg);
}

export function requestDataSource<T, P = Record<string, any>>(key: string, params: P, filter: (message: PushDataSourceMessage<T>) => boolean = () => true) {
const getMsg: GetDataSourceMessage<string> = {
type: ClipsheetMessageTypeEnum.GetDataSource,
Expand Down
16 changes: 14 additions & 2 deletions packages/table/lib/table.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { inProgressTableRecordId, TableDataSourceKeyEnum, TableMessageTypeEnum,
import type { ITableRecord } from './table';

export class TableService {
private _latestPushSignal: string = '';
private _latestParams: IGetTableRecordsParams = {
page: 1,
pageSize: defaultPageSize,
Expand Down Expand Up @@ -52,7 +53,16 @@ export class TableService {
this._latestParams = _params;
}

return pushDataSource(TableDataSourceKeyEnum.TableRecords, await this._tableDataSource.getList(this._latestParams), tabId);
const signal = generateRandomId();
this._latestPushSignal = signal;

return this._tableDataSource.getList(this._latestParams).then((res) => {
if (this._latestPushSignal !== signal) {
return;
}

return pushDataSource(TableDataSourceKeyEnum.TableRecords, res, tabId);
});
}

listenMessage() {
Expand Down Expand Up @@ -93,11 +103,13 @@ export class TableService {
res.payload.success = true;
res.payload.id = id;
senderTabId && chrome.tabs.sendMessage(senderTabId, res);
this.pushTableRecords();
})
.catch(() => {
console.error('TableService:ScrapTables', 'Failed to add table');
senderTabId && chrome.tabs.sendMessage(senderTabId, res);
})
.finally(() => {
this.pushTableRecords();
});
break;
}
Expand Down
10 changes: 3 additions & 7 deletions packages/ui/views/workflow-panel/WorkflowPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { CloseGraySvg, PlusSvg } from '@components/icons';
import { type IMessageRef, Message } from '@components/message';
import { useSyncIframeRectEffect } from '@lib/hooks';
import { t } from '@univer-clipsheet-core/locale';
import type { GetStorageMessage, PushStorageMessage } from '@univer-clipsheet-core/shared';
import { ClipsheetMessageTypeEnum, IframeViewTypeEnum, promisifyMessage, sendSetIframeViewMessage } from '@univer-clipsheet-core/shared';
import type { PushStorageMessage } from '@univer-clipsheet-core/shared';
import { ClipsheetMessageTypeEnum, IframeViewTypeEnum, promisifyMessage, sendGetStorageMessage, sendSetIframeViewMessage } from '@univer-clipsheet-core/shared';
import type { IWorkflow } from '@univer-clipsheet-core/workflow';
import { TimerRepeatMode, WorkflowFilterMode, WorkflowMessageTypeEnum, WorkflowRuleName, WorkflowStorageKeyEnum } from '@univer-clipsheet-core/workflow';
import clsx from 'clsx';
Expand Down Expand Up @@ -130,12 +130,8 @@ const InnerWorkflowPanel = (props: {
}, [originTableId, boundDataSource, workflow, service]);

useEffect(() => {
const request: GetStorageMessage = {
type: ClipsheetMessageTypeEnum.GetStorage,
payload: WorkflowStorageKeyEnum.CurrentWorkflow,
};
sendGetStorageMessage(WorkflowStorageKeyEnum.CurrentWorkflow);

chrome.runtime.sendMessage(request);
const responsePromise = promisifyMessage<PushStorageMessage<IWorkflow>>((msg) => msg.type === ClipsheetMessageTypeEnum.PushStorage && msg.payload.key === WorkflowStorageKeyEnum.CurrentWorkflow);

responsePromise.then(({ payload }) => {
Expand Down

0 comments on commit 76bc1fa

Please sign in to comment.