|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | import { Box, type Mutation, type Store } from 'amos-core';
|
7 |
| -import { fromJS, type ID, isArray, must, nextSerialTicker, tryFinally } from 'amos-utils'; |
| 7 | +import { fromJS, type ID, isArray, must, nextSerialTicker } from 'amos-utils'; |
8 | 8 | import { persistBox } from './state';
|
9 | 9 | import type { PersistEntry, PersistKey, PersistOptions } from './types';
|
10 | 10 | import { fromKey, toKey, toPersistOptions } from './utils';
|
@@ -70,63 +70,55 @@ export const createHydrate = (store: Store, finalOptions: PersistOptions) => {
|
70 | 70 | exactKeys.length ? state.storage.getMulti(exactKeys.map(([k, i]) => toKey(k, i))) : [],
|
71 | 71 | ]);
|
72 | 72 |
|
73 |
| - tryFinally( |
74 |
| - () => { |
75 |
| - state.selecting = true; |
76 |
| - const tablePrefixMap: Record<string, PersistEntry[]> = {}; |
77 |
| - const exactEntries: Mutation[] = []; |
78 |
| - exacts.forEach((value, index) => { |
79 |
| - if (value === null) { |
80 |
| - return; |
81 |
| - } |
82 |
| - const [key, rowId] = exactKeys[index]; |
83 |
| - const box = Box.get(key); |
84 |
| - if (rowId !== void 0) { |
85 |
| - if (!tablePrefixMap[key]) { |
86 |
| - tablePrefixMap[key] = []; |
87 |
| - prefixBoxes.push(key); |
88 |
| - prefixes.push(tablePrefixMap[key]); |
| 73 | + const tablePrefixMap: Record<string, PersistEntry[]> = {}; |
| 74 | + const exactEntries: Mutation[] = []; |
| 75 | + exacts.forEach((value, index) => { |
| 76 | + if (value === null) { |
| 77 | + return; |
| 78 | + } |
| 79 | + const [key, rowId] = exactKeys[index]; |
| 80 | + const box = Box.get(key); |
| 81 | + if (rowId !== void 0) { |
| 82 | + if (!tablePrefixMap[key]) { |
| 83 | + tablePrefixMap[key] = []; |
| 84 | + prefixBoxes.push(key); |
| 85 | + prefixes.push(tablePrefixMap[key]); |
| 86 | + } |
| 87 | + tablePrefixMap[key].push([toKey(key, rowId), ...value]); |
| 88 | + } else if (state.select(box) === state.getInitial(box)) { |
| 89 | + const js = migrate(box, value[0], '', value[1]); |
| 90 | + if (js !== void 0) { |
| 91 | + exactEntries.push(box.setState(fromJS(state.getInitial(box), js))); |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + store.dispatch( |
| 97 | + prefixes |
| 98 | + .map((p, i): Mutation[] => { |
| 99 | + const box = Box.get(prefixBoxes[i]); |
| 100 | + const curr = state.select(box); |
| 101 | + const data: Record<string, any> = {}; |
| 102 | + for (let [k, v, d] of p) { |
| 103 | + const id = fromKey(k); |
| 104 | + if (id === void 0) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + if (box.table!.getRow(curr, id) !== box.table!.getRow(state.getInitial(box), id)) { |
| 108 | + continue; |
89 | 109 | }
|
90 |
| - tablePrefixMap[key].push([toKey(key, rowId), ...value]); |
91 |
| - } else if (store.select(box) === state.getInitial(box)) { |
92 |
| - const js = migrate(box, value[0], '', value[1]); |
93 |
| - if (js !== void 0) { |
94 |
| - exactEntries.push(box.setState(fromJS(box.getInitialState(), js))); |
| 110 | + d = migrate(box, v, id, d); |
| 111 | + if (d !== void 0) { |
| 112 | + data[id] = d; |
95 | 113 | }
|
96 | 114 | }
|
97 |
| - }); |
98 |
| - |
99 |
| - store.dispatch( |
100 |
| - prefixes |
101 |
| - .map((p, i): Mutation[] => { |
102 |
| - const box = Box.get(prefixBoxes[i]); |
103 |
| - const curr = store.select(box); |
104 |
| - const data: Record<string, any> = {}; |
105 |
| - for (let [k, v, d] of p) { |
106 |
| - const id = fromKey(k); |
107 |
| - if (id === void 0) { |
108 |
| - continue; |
109 |
| - } |
110 |
| - if (box.table!.getRow(curr, id) !== box.table!.getRow(state.getInitial(box), id)) { |
111 |
| - continue; |
112 |
| - } |
113 |
| - d = migrate(box, v, id, d); |
114 |
| - if (d !== void 0) { |
115 |
| - data[id] = d; |
116 |
| - } |
117 |
| - } |
118 |
| - if (Object.keys(data).length === 0) { |
119 |
| - return []; |
120 |
| - } |
121 |
| - return [box.setState(box.table!.hydrate(curr, data))]; |
122 |
| - }) |
123 |
| - .flat() |
124 |
| - .concat(exactEntries), |
125 |
| - ); |
126 |
| - }, |
127 |
| - () => { |
128 |
| - state.selecting = false; |
129 |
| - }, |
| 115 | + if (Object.keys(data).length === 0) { |
| 116 | + return []; |
| 117 | + } |
| 118 | + return [box.setState(box.table!.hydrate(curr, data))]; |
| 119 | + }) |
| 120 | + .flat() |
| 121 | + .concat(exactEntries), |
130 | 122 | );
|
131 | 123 | }, finalOptions.onError);
|
132 | 124 | };
|
0 commit comments