Skip to content

Commit 6168b86

Browse files
committed
perf: use object instead of Map to store dialogs data
Because Maps are not reactive in Vue 2, we had to recreate Map instead of mutate it. But now, with object we can just set or delete value by key
1 parent 02cead1 commit 6168b86

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

src/close.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export function closeAllDialogs(reason: unknown, unmountDelay?: number) {
55
if (!wrapper) {
66
throw new Error('PromiseDialogsWrapper instance not found');
77
}
8-
wrapper.dialogsData.forEach((_, id) => wrapper.reject(id, reason, unmountDelay));
8+
Object.getOwnPropertySymbols(wrapper.dialogsData).forEach(id => wrapper.reject(id, reason, unmountDelay));
99
}

src/dialogs-wrapper/PromiseDialogsWrapper.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default Vue.extend({
2121
},
2222
data() {
2323
return {
24-
dialogsData: new Map<symbol, DialogData<any, any>>(),
24+
dialogsData: {} as Record<symbol, DialogData<any, any>>,
2525
};
2626
},
2727
created() {
@@ -38,36 +38,26 @@ export default Vue.extend({
3838
methods: {
3939
add<P, R>(component: RegularComponent, params: P, unmountDelay?: number): Promise<R> {
4040
return new Promise<R>((resolve, reject) => {
41-
this.dialogsData = new Map([
42-
...this.dialogsData,
43-
[
44-
// eslint-disable-next-line symbol-description
45-
Symbol(),
46-
{
47-
component,
48-
params,
49-
promiseResolve: resolve,
50-
promiseReject: reject,
51-
unmountDelay,
52-
},
53-
],
54-
]);
41+
// eslint-disable-next-line symbol-description
42+
this.$set(this.dialogsData, Symbol() as any /* symbol really can be used as a key */, {
43+
component,
44+
params,
45+
promiseResolve: resolve,
46+
promiseReject: reject,
47+
unmountDelay,
48+
});
5549
});
5650
},
5751
resolve(id: symbol, result: unknown, unmountDelay?: number) {
58-
this.dialogsData.get(id)?.promiseResolve(result);
52+
this.dialogsData[id].promiseResolve(result);
5953
this.unmountDialog(id, unmountDelay);
6054
},
6155
reject(id: symbol, error: unknown, unmountDelay?: number) {
62-
this.dialogsData.get(id)?.promiseReject(error);
56+
this.dialogsData[id].promiseReject(error);
6357
this.unmountDialog(id, unmountDelay);
6458
},
6559
unmountDialog(id: symbol, delay?: number) {
66-
const unmount = () => {
67-
this.dialogsData = new Map(
68-
[...this.dialogsData].filter(([key]) => key !== id),
69-
);
70-
};
60+
const unmount = () => this.$delete(this.dialogsData, id as any);
7161
if (delay) {
7262
setTimeout(unmount, delay);
7363
} else if (this.unmountDelay) {
@@ -80,8 +70,9 @@ export default Vue.extend({
8070
render(createElement): VNode {
8171
return createElement(
8272
'div',
83-
[...this.dialogsData].map(
84-
([id, value]) => createElement(value.component, {
73+
Object.getOwnPropertySymbols(this.dialogsData).map((id) => {
74+
const value = this.dialogsData[id];
75+
return createElement(value.component, {
8576
key: id as any,
8677
props: {
8778
params: value.params,
@@ -94,8 +85,8 @@ export default Vue.extend({
9485
this.reject(id, error, unmountDelay || value.unmountDelay);
9586
},
9687
},
97-
}),
98-
),
88+
});
89+
}),
9990
);
10091
},
10192
});

src/dialogs-wrapper/store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface OptionalValueWrapper<T> {
55
}
66

77
interface PromiseDialogsWrapper {
8-
dialogsData: Map<symbol, unknown>;
8+
dialogsData: Record<symbol, unknown>;
99

1010
add<P, R>(component: RegularComponent, params: P, unmountDelay?: number): Promise<R>;
1111

0 commit comments

Comments
 (0)