Skip to content

Commit f9166ec

Browse files
committed
refactor: use a map for get-view-event-type
1 parent e926b1f commit f9166ec

4 files changed

Lines changed: 71 additions & 110 deletions

File tree

src/stores/view/helpers/get-document-event-type.ts

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,47 @@
11
import { DocumentStoreAction } from 'src/stores/document/document-store-actions';
22

3-
const contentEvents = new Set<ActionType>([
4-
'document/update-node-content',
5-
'document/format-headings',
6-
]);
7-
8-
const createAndDelete = new Set<ActionType>([
9-
'document/add-node',
10-
'document/delete-node',
11-
'document/merge-node',
12-
'document/file/load-from-disk',
13-
'document/extract-node',
14-
'document/split-node',
15-
]);
16-
17-
const dropAndMoveEvents = new Set<ActionType>([
18-
'document/drop-node',
19-
'document/move-node',
20-
'document/sort-direct-child-nodes',
21-
]);
22-
23-
const historyEvents = new Set<ActionType>([
24-
'document/history/select-next-snapshot',
25-
'document/history/select-previous-snapshot',
26-
'document/history/select-snapshot',
27-
]);
28-
const clipboardEvents = new Set<ActionType>([
29-
'document/paste-node',
30-
'document/cut-node',
31-
]);
32-
33-
const cachedResults: { [key: string]: DocumentEventType } = {};
34-
353
export type DocumentEventType = {
364
content?: boolean;
375
dropOrMove?: boolean;
386
createOrDelete?: boolean;
397
changeHistory?: boolean;
408
clipboard?: boolean;
419
};
10+
4211
type ActionType = DocumentStoreAction['type'];
43-
export const getDocumentEventType = (type: ActionType): DocumentEventType => {
44-
if (cachedResults[type]) {
45-
return cachedResults[type];
46-
}
4712

48-
let result: DocumentEventType | null = null;
49-
if (contentEvents.has(type)) result = { content: true };
50-
else if (createAndDelete.has(type)) result = { createOrDelete: true };
51-
else if (dropAndMoveEvents.has(type)) result = { dropOrMove: true };
52-
else if (historyEvents.has(type)) result = { changeHistory: true };
53-
else if (clipboardEvents.has(type)) result = { clipboard: true };
54-
if (!result) result = {};
13+
const eventTypesDictionary: Partial<Record<ActionType, DocumentEventType>> = {
14+
'document/update-node-content': { content: true },
15+
'document/format-headings': { content: true },
16+
17+
'document/add-node': { createOrDelete: true },
18+
'document/delete-node': { createOrDelete: true },
19+
'document/merge-node': { createOrDelete: true },
20+
'document/file/load-from-disk': { createOrDelete: true },
21+
'document/extract-node': { createOrDelete: true },
22+
'document/split-node': { createOrDelete: true },
23+
24+
'document/drop-node': { dropOrMove: true },
25+
'document/move-node': { dropOrMove: true },
26+
'document/sort-direct-child-nodes': { dropOrMove: true },
5527

56-
cachedResults[type] = result;
28+
'document/history/select-next-snapshot': { changeHistory: true },
29+
'document/history/select-previous-snapshot': { changeHistory: true },
30+
'document/history/select-snapshot': { changeHistory: true },
5731

58-
return result;
32+
'document/paste-node': { clipboard: true },
33+
'document/cut-node': { clipboard: true },
34+
} as const;
35+
36+
const documentEventTypes = new Map(Object.entries(eventTypesDictionary)) as Map<
37+
ActionType,
38+
DocumentEventType
39+
>;
40+
41+
const none = {};
42+
43+
export const getDocumentEventType = (type: ActionType): DocumentEventType => {
44+
return documentEventTypes.get(type) || none;
5945
};
6046

6147
export const STRUCTURE_AND_CONTENT = new Set<DocumentStoreAction['type']>([
Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,46 @@
11
import { ViewStoreAction } from 'src/stores/view/view-store-actions';
22

3-
type ActionType = ViewStoreAction['type'];
4-
const navigationEvents = new Set<ActionType>([
5-
'view/set-active-node/history/select-previous',
6-
'view/set-active-node/history/select-next',
7-
'view/set-active-node/sequential/select-next',
8-
]);
9-
10-
const searchEvents = new Set<ActionType>([
11-
'view/search/set-query',
12-
'view/search/set-results',
13-
'view/search/toggle-input',
14-
]);
15-
16-
const stateEvents = new Set<ActionType>([
17-
'view/set-active-node/document',
18-
'view/set-active-node/mouse',
19-
'view/set-active-node/mouse-silent',
20-
'view/set-active-node/search',
21-
'view/set-active-node/keyboard',
22-
'view/set-active-node/keyboard-jump',
23-
'view/selection/select-all',
24-
]);
25-
26-
const editMainSplitEvents = new Set<ActionType>([
27-
'view/editor/enable-main-editor',
28-
'view/editor/disable-main-editor',
29-
]);
30-
const editSidebarEvents = new Set<ActionType>([
31-
'view/editor/enable-sidebar-editor',
32-
'view/editor/disable-sidebar-editor',
33-
]);
34-
353
export type ViewEventType = {
364
activeNodeHistory?: boolean;
375
activeNode?: boolean;
386
search?: boolean;
39-
editMainSplit?: boolean;
40-
editSidebar?: boolean;
7+
mainEditor?: boolean;
8+
sidebarEditor?: boolean;
419
};
42-
const cachedResults: { [key: string]: ViewEventType } = {};
4310

44-
export const getViewEventType = (type: ActionType): ViewEventType => {
45-
if (cachedResults[type]) {
46-
return cachedResults[type];
47-
}
48-
49-
let result: ViewEventType | null = null;
50-
51-
if (navigationEvents.has(type)) result = { activeNodeHistory: true };
52-
else if (stateEvents.has(type)) result = { activeNode: true };
53-
else if (searchEvents.has(type)) result = { search: true };
54-
else if (editMainSplitEvents.has(type)) result = { editMainSplit: true };
55-
else if (editSidebarEvents.has(type)) result = { editSidebar: true };
56-
if (!result) result = {};
11+
type ActionType = ViewStoreAction['type'];
5712

58-
cachedResults[type] = result;
13+
const eventTypesDictionary: Partial<Record<ActionType, ViewEventType>> = {
14+
'view/set-active-node/history/select-previous': {
15+
activeNodeHistory: true,
16+
},
17+
'view/set-active-node/history/select-next': { activeNodeHistory: true },
18+
'view/set-active-node/sequential/select-next': {
19+
activeNodeHistory: true,
20+
},
21+
'view/search/set-query': { search: true },
22+
'view/search/set-results': { search: true },
23+
'view/search/toggle-input': { search: true },
24+
'view/set-active-node/document': { activeNode: true },
25+
'view/set-active-node/mouse': { activeNode: true },
26+
'view/set-active-node/mouse-silent': { activeNode: true },
27+
'view/set-active-node/search': { activeNode: true },
28+
'view/set-active-node/keyboard': { activeNode: true },
29+
'view/set-active-node/keyboard-jump': { activeNode: true },
30+
'view/selection/select-all': { activeNode: true },
31+
'view/editor/enable-main-editor': { mainEditor: true },
32+
'view/editor/disable-main-editor': { mainEditor: true },
33+
'view/editor/enable-sidebar-editor': { sidebarEditor: true },
34+
'view/editor/disable-sidebar-editor': { sidebarEditor: true },
35+
} as const;
36+
37+
const viewEventTypes = new Map(Object.entries(eventTypesDictionary)) as Map<
38+
ActionType,
39+
ViewEventType
40+
>;
41+
42+
const none = {};
5943

60-
return result;
44+
export const getViewEventType = (type: ActionType): ViewEventType => {
45+
return viewEventTypes.get(type) || none;
6146
};

src/stores/view/subscriptions/on-document-state-update.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { LineageView } from 'src/view/view';
22
import { DocumentStoreAction } from 'src/stores/document/document-store-actions';
3-
import {
4-
DocumentEventType,
5-
getDocumentEventType,
6-
} from 'src/stores/view/helpers/get-document-event-type';
3+
import { getDocumentEventType } from 'src/stores/view/helpers/get-document-event-type';
74
import { setActiveNode } from 'src/stores/view/subscriptions/actions/set-active-node';
85
import { enableEditMode } from 'src/stores/view/subscriptions/actions/enable-edit-mode';
96
import { removeObsoleteNavigationItems } from 'src/stores/view/subscriptions/actions/remove-obsolete-navigation-items';
@@ -25,9 +22,7 @@ export const onDocumentStateUpdate = (
2522
viewStore.setContext(documentState.document);
2623
const type = action.type;
2724

28-
const e: DocumentEventType | null = getDocumentEventType(
29-
type as DocumentStoreAction['type'],
30-
);
25+
const e = getDocumentEventType(type);
3126
if (type === 'document/file/load-from-disk') {
3227
// needed when the file was modified externally
3328
// to prevent saving a node with an obsolete node-id

src/stores/view/subscriptions/on-view-state-update.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { LineageView } from 'src/view/view';
22
import { ViewStoreAction } from 'src/stores/view/view-store-actions';
3-
import {
4-
getViewEventType,
5-
ViewEventType,
6-
} from 'src/stores/view/helpers/get-view-event-type';
3+
import { getViewEventType } from 'src/stores/view/helpers/get-view-event-type';
74
import { updateSearchResults } from 'src/stores/view/subscriptions/actions/update-search-results';
85
import { focusContainer } from 'src/stores/view/subscriptions/effects/focus-container';
96
import { persistActiveNodeInPluginSettings } from 'src/stores/view/subscriptions/actions/persist-active-node-in-plugin-settings';
@@ -23,9 +20,7 @@ export const onViewStateUpdate = (
2320

2421
const type = action.type;
2522

26-
const e: ViewEventType | null = getViewEventType(
27-
type as ViewStoreAction['type'],
28-
);
23+
const e = getViewEventType(type);
2924

3025
const activeNodeChange = e.activeNode || e.activeNodeHistory;
3126
const activeNodeHasChanged =
@@ -55,7 +50,7 @@ export const onViewStateUpdate = (
5550
// effects
5651
if (
5752
e.search ||
58-
e.editMainSplit ||
53+
e.mainEditor ||
5954
action.type === 'view/update-active-branch?source=document' ||
6055
(activeNodeChange && activeNodeHasChanged)
6156
) {

0 commit comments

Comments
 (0)