Skip to content

Commit 05f16d9

Browse files
committed
Demo optimisation: only recalculate QR code if relevant config changes
1 parent 8588ad7 commit 05f16d9

1 file changed

Lines changed: 44 additions & 28 deletions

File tree

web/index.mjs

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,60 +52,76 @@ const modes = [
5252
];
5353

5454
let latestCode = null;
55+
let latestOptions = '';
5556

5657
function regenerate() {
57-
latestCode = null;
58-
5958
try {
60-
const code = generate(getValue('message'), {
59+
const options = {
60+
message: getValue('message'),
6161
minVersion: getInt('min-version', 1, 40),
6262
maxVersion: getInt('max-version', 1, 40),
63-
minCorrectionLevel: correction[getValue('min-correction')],
64-
maxCorrectionLevel: correction[getValue('max-correction')],
63+
minCorrectionLevel: getValue('min-correction'),
64+
maxCorrectionLevel: getValue('max-correction'),
6565
mask: getMask(),
6666
trailer: getData('trailer', 0x0000, 0xffff),
67-
modes: modes.filter(({ input }) => input.checked).map(({ mode }) => mode),
68-
});
69-
latestCode = code;
67+
modes: modes
68+
.map(({ input }, i) => (input.checked ? i : -1))
69+
.filter((i) => i !== -1),
70+
};
71+
if (JSON.stringify(options) !== latestOptions) {
72+
latestCode = generate(options.message, {
73+
...options,
74+
minCorrectionLevel: correction[options.minCorrectionLevel],
75+
maxCorrectionLevel: correction[options.maxCorrectionLevel],
76+
modes: options.modes.map((i) => modes[i].mode),
77+
});
78+
latestOptions = JSON.stringify(options);
79+
}
7080
downloadPng.setAttribute('href', '#');
7181
downloadSvg.setAttribute('href', '#');
7282

73-
code.toCanvas(qrCanvas, {
83+
latestCode.toCanvas(qrCanvas, {
7484
on: getColour('on'),
7585
off: getColour('off'),
7686
});
7787
document.body.className = 'success';
7888
} catch (e) {
7989
err.innerText = readError(e);
8090
document.body.className = 'error';
91+
latestCode = null;
92+
latestOptions = '';
8193
}
8294
}
8395

8496
downloadPng.addEventListener('click', (e) => {
85-
if (!latestCode) {
86-
e.preventDefault();
87-
return;
97+
if (e.currentTarget.getAttribute('href') === '#') {
98+
if (!latestCode) {
99+
e.preventDefault();
100+
return;
101+
}
102+
const url = latestCode.toDataURL({
103+
type: 'image/png',
104+
on: getColour('on'),
105+
off: getColour('off'),
106+
scale: getInt('scale', 1, Number.POSITIVE_INFINITY),
107+
});
108+
e.currentTarget.setAttribute('href', url);
88109
}
89-
const url = latestCode.toDataURL({
90-
type: 'image/png',
91-
on: getColour('on'),
92-
off: getColour('off'),
93-
scale: getInt('scale', 1, Number.POSITIVE_INFINITY),
94-
});
95-
downloadPng.setAttribute('href', url);
96110
});
97111

98112
downloadSvg.addEventListener('click', (e) => {
99-
if (!latestCode) {
100-
e.preventDefault();
101-
return;
113+
if (e.currentTarget.getAttribute('href') === '#') {
114+
if (!latestCode) {
115+
e.preventDefault();
116+
return;
117+
}
118+
const url = toSvgDataURL(latestCode, {
119+
on: getValue('on'),
120+
off: getValue('off'),
121+
scale: getInt('scale', 1, Number.POSITIVE_INFINITY),
122+
});
123+
e.currentTarget.setAttribute('href', url);
102124
}
103-
const url = toSvgDataURL(latestCode, {
104-
on: getValue('on'),
105-
off: getValue('off'),
106-
scale: getInt('scale', 1, Number.POSITIVE_INFINITY),
107-
});
108-
downloadSvg.setAttribute('href', url);
109125
});
110126

111127
if (

0 commit comments

Comments
 (0)