Skip to content

Commit b296ef0

Browse files
committed
feat: remove cache
1 parent b7ecced commit b296ef0

File tree

4 files changed

+224
-136
lines changed

4 files changed

+224
-136
lines changed

frameworks/keyed/million/src/main.js

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ const random = (max) => Math.round(Math.random() * 1000) % max;
6464

6565
let nextId = 1;
6666
let list = [];
67-
let oldCache = {};
6867
let selected = 0;
6968
let main;
7069

@@ -132,29 +131,29 @@ const select = (id) => {
132131
if (prevBlock) {
133132
if (prevBlock.props.id === id) return;
134133
const { id: prevId, label } = prevBlock.props;
135-
const row = Row(
136-
{
137-
id: prevId,
138-
label,
139-
className: '',
140-
},
141-
String(id)
134+
prevBlock.patch(
135+
Row(
136+
{
137+
id: prevId,
138+
label,
139+
className: '',
140+
},
141+
String(id)
142+
)
142143
);
143-
row.memo = [label, false];
144-
prevBlock.patch(row);
145144
}
146145
const block = main.children.find((block) => block.props.id === id);
147146
const { label } = block.props;
148-
const row = Row(
149-
{
150-
id,
151-
label,
152-
className: 'danger',
153-
},
154-
String(id)
147+
block.patch(
148+
Row(
149+
{
150+
id,
151+
label,
152+
className: 'danger',
153+
},
154+
String(id)
155+
)
155156
);
156-
row.memo = [label, true];
157-
block.patch(row);
158157

159158
prevBlock = block;
160159
};
@@ -216,36 +215,6 @@ const Row = (() => {
216215
};
217216
})();
218217

219-
function render(oldCache, newCache) {
220-
return fragment(
221-
list.map((item) => {
222-
const isSelected = selected === item.id;
223-
const id = String(item.id);
224-
const cachedItem = oldCache[item.id];
225-
if (cachedItem) {
226-
if (
227-
cachedItem.memo[0] === item.label &&
228-
cachedItem.memo[1] === isSelected
229-
) {
230-
return (newCache[item.id] = cachedItem);
231-
}
232-
}
233-
234-
const row = Row(
235-
{
236-
id: item.id,
237-
label: item.label,
238-
className: isSelected ? 'danger' : '',
239-
},
240-
id
241-
);
242-
row.memo = [item.label, isSelected];
243-
newCache[item.id] = row;
244-
return row;
245-
})
246-
);
247-
}
248-
249218
(() => {
250219
new Block(
251220
stringToDOM(
@@ -354,7 +323,18 @@ function render(oldCache, newCache) {
354323
})();
355324

356325
function update() {
357-
let newCache = {};
358-
main.patch(render(oldCache, newCache));
359-
oldCache = newCache;
326+
main.patch(
327+
fragment(
328+
list.map((item) =>
329+
Row(
330+
{
331+
id: item.id,
332+
label: item.label,
333+
className: selected === item.id ? 'danger' : '',
334+
},
335+
String(item.id)
336+
)
337+
)
338+
)
339+
);
360340
}

0 commit comments

Comments
 (0)