Skip to content

Commit ff263c7

Browse files
committed
update cache
1 parent 3cc6591 commit ff263c7

File tree

1 file changed

+75
-66
lines changed

1 file changed

+75
-66
lines changed

frameworks/keyed/million/src/main.jsx

Lines changed: 75 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ const random = (max) => Math.round(Math.random() * 1000) % max;
6363

6464
let nextId = 1;
6565
let list = [];
66-
let main;
6766
let selected = 0;
67+
let main;
6868

6969
const clear = () => {
7070
list = [];
@@ -85,44 +85,44 @@ const buildData = (count) => {
8585
return data;
8686
};
8787

88-
const create1k = (event) => {
89-
event.stopPropagation();
88+
const create1k = () => {
9089
if (list.length) clear();
9190
list = buildData(1000);
9291
update();
92+
return false;
9393
};
9494

95-
const create10k = (event) => {
96-
event.stopPropagation();
95+
const create10k = () => {
9796
if (list.length) clear();
9897
list = buildData(10000);
9998
update();
99+
return false;
100100
};
101101

102-
const append1k = (event) => {
103-
event.stopPropagation();
102+
const append1k = () => {
104103
list = list.concat(buildData(1000));
105104
update();
105+
return false;
106106
};
107107

108-
const updateEvery10 = (event) => {
109-
event.stopPropagation();
108+
const updateEvery10 = () => {
110109
let i = 0;
111110
while (i < list.length) {
112111
list[i].label = `${list[i].label} !!!`;
113112
i += 10;
114113
}
115114
update();
115+
return false;
116116
};
117117

118-
const swapRows = (event) => {
119-
event.stopPropagation();
118+
const swapRows = () => {
120119
if (list.length > 998) {
121120
const item = list[1];
122121
list[1] = list[998];
123122
list[998] = item;
124123
}
125124
update();
125+
return false;
126126
};
127127

128128
const select = (id) => {
@@ -132,19 +132,12 @@ const select = (id) => {
132132

133133
const remove = (id) => {
134134
list.splice(
135-
list.findIndex((z) => z.id === id),
135+
list.findIndex((item) => item.id === id),
136136
1
137137
);
138138
update();
139139
};
140140

141-
const shouldUpdate = (oldProps, newProps) => {
142-
return (
143-
oldProps.label !== newProps.label ||
144-
oldProps.className !== newProps.className
145-
);
146-
};
147-
148141
const Main = createBlock(({ rows }) => (
149142
<div class="container">
150143
<div class="jumbotron">
@@ -199,10 +192,10 @@ const Main = createBlock(({ rows }) => (
199192
type="button"
200193
class="btn btn-primary btn-block"
201194
id="clear"
202-
onClick={(event) => {
203-
event.stopPropagation();
195+
onClick={() => {
204196
clear();
205197
update();
198+
return false;
206199
}}
207200
>
208201
Clear
@@ -232,66 +225,82 @@ const Main = createBlock(({ rows }) => (
232225
</div>
233226
));
234227

235-
const Row = createBlock(({ className, id, select, remove, label }) => {
236-
return (
237-
<tr class={className}>
238-
<td class="col-md-1">{id}</td>
239-
<td class="col-md-4">
240-
<a onClick={select}>{label}</a>
241-
</td>
242-
<td class="col-md-1">
243-
<a onClick={remove}>
244-
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
245-
</a>
246-
</td>
247-
<td class="col-md-6"></td>
248-
</tr>
249-
);
250-
});
228+
const Row = createBlock(({ className, id, select, remove, label }) => (
229+
<tr class={className}>
230+
<td class="col-md-1">{id}</td>
231+
<td class="col-md-4">
232+
<a onClick={select}>{label}</a>
233+
</td>
234+
<td class="col-md-1">
235+
<a onClick={remove}>
236+
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
237+
</a>
238+
</td>
239+
<td class="col-md-6"></td>
240+
</tr>
241+
));
242+
243+
const shouldUpdate = (oldProps, newProps) => {
244+
return oldProps.valueKey !== newProps.valueKey;
245+
};
251246

252-
function Rows({ oldCache, newCache }) {
247+
const cache = (map, key, listener) => {
248+
if (has$.call(map, key)) {
249+
return get$.call(selectCache, key);
250+
} else {
251+
set$.call(map, key, listener);
252+
return listener;
253+
}
254+
};
255+
256+
const removeCache = new Map();
257+
const selectCache = new Map();
258+
259+
const Map$ = Map.prototype;
260+
const has$ = Map$.has;
261+
const get$ = Map$.get;
262+
const set$ = Map$.set;
263+
264+
function render(oldCache, newCache) {
253265
return fragment(
254266
list.map((item) => {
255267
const isSelected = selected === item.id;
256-
const cachedItem = oldCache[item.id];
257-
if (cachedItem) {
258-
const [cachedLabel, cachedIsSelected] = cachedItem._data;
259-
if (cachedLabel === item.label && cachedIsSelected === isSelected) {
260-
return (newCache[item.id] = cachedItem);
261-
}
268+
const key = String(item.id);
269+
const cachedItem = oldCache[key];
270+
const valueKey = `${item.label}${isSelected}`;
271+
if (cachedItem?.props.valueKey === valueKey) {
272+
return (newCache[key] = cachedItem);
262273
}
263-
const row = (
264-
<Row
265-
id={item.id}
266-
label={item.label}
267-
className={isSelected ? 'danger' : ''}
268-
remove={(event) => {
269-
event.stopPropagation();
274+
275+
const row = Row(
276+
{
277+
id: item.id,
278+
label: item.label,
279+
className: isSelected ? 'danger' : '',
280+
remove: cache(removeCache, key, () => {
270281
remove(item.id);
271-
}}
272-
select={(event) => {
273-
event.stopPropagation();
282+
return false;
283+
}),
284+
select: cache(selectCache, key, () => {
274285
select(item.id);
275-
}}
276-
/>
286+
return false;
287+
}),
288+
valueKey,
289+
},
290+
key,
291+
shouldUpdate
277292
);
278-
row._data = [item.label, isSelected];
279-
row.key = String(item.id);
280-
row.shouldUpdate = shouldUpdate;
281-
newCache[item.id] = row;
293+
newCache[key] = row;
282294
return row;
283295
})
284296
);
285297
}
286298

287-
function render(oldCache, newCache) {
288-
return <Rows oldCache={oldCache} newCache={newCache} />;
289-
}
290-
291299
let oldCache = {};
292300

293-
main = render({}, oldCache);
294-
(<Main rows={main} />).mount(document.getElementById('main'));
301+
Main({ rows: (main = render({}, oldCache)) }).mount(
302+
document.getElementById('main')
303+
);
295304

296305
function update() {
297306
let newCache = {};

0 commit comments

Comments
 (0)