Skip to content

Commit

Permalink
feat: tag webWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
zovomat committed Mar 28, 2022
1 parent 293586b commit a4a7944
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/store/network/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ export const handleSync = ({ refresh, notify }: SoapContext): Promise<void> =>
const { seq } = useNetworkStore.getState();
if (refresh) {
tagWorker.postMessage({
type: 'refresh',
op: 'refresh',
tags: refresh.tags?.tag ?? []
});
}
if (notify?.length) {
forEach(notify, (item) => {
if (item.seq > seq) {
tagWorker.postMessage({
type: 'notify',
op: 'notify',
notify: item,
state: useTagStore.getState()
state: useTagStore.getState().tags
});
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/store/tags/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ export const useTagStore = create<TagState>(() => ({
tags: {}
})) as UseBoundStore<TagState, StoreApi<TagState>>;

tagWorker.onmessage = ({ data }): void => useTagStore.setState(data);
tagWorker.onmessage = ({ data }): void => {
useTagStore.setState(data);
};
7 changes: 3 additions & 4 deletions src/workers/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ export const handleTagDeleted = (tags: Tags, deleted: string[]): Tags =>
delete acc[val];
return acc;
}, tags);
export const handleTagNotify = (notify: SoapNotify, state: Tags): void => {
export const handleTagNotify = (notify: SoapNotify, state: Tags): Tags =>
handleTagDeleted(
handleTagModified(
handleTagCreated(state, notify.created?.tag ?? []),
notify.modified?.tag ?? []
),
notify.deleted ?? []
);
};

self.onmessage = ({ data }: TagMessage): void => {
if (data.op === 'refresh' && data.tags) self.postMessage(handleTagRefresh(data.tags));
if (data.op === 'notify') self.postMessage(handleTagNotify(data.notify, data.state));
if (data.op === 'refresh' && data.tags) self.postMessage({ tags: handleTagRefresh(data.tags) });
if (data.op === 'notify') self.postMessage({ tags: handleTagNotify(data.notify, data.state) });
};

0 comments on commit a4a7944

Please sign in to comment.