|
47 | 47 | if (!port) return; |
48 | 48 | $syncStatus = "uploading"; |
49 | 49 |
|
| 50 | + const layoutChanges = $overlay.layout.reduce( |
| 51 | + (acc, layer) => acc + layer.size, |
| 52 | + 0, |
| 53 | + ); |
| 54 | + const settingChanges = $overlay.settings.size; |
| 55 | + const chordChanges = $overlay.chords.size; |
| 56 | + const needsCommit = settingChanges > 0 && layoutChanges > 0; |
| 57 | + const progressMax = layoutChanges + settingChanges + chordChanges; |
| 58 | +
|
| 59 | + let progressCurrent = 0; |
| 60 | +
|
| 61 | + syncProgress.set({ |
| 62 | + max: progressMax, |
| 63 | + current: progressCurrent, |
| 64 | + }); |
| 65 | +
|
50 | 66 | for (const [id, chord] of $overlay.chords) { |
51 | 67 | if (!chord.deleted) { |
52 | 68 | if (id !== JSON.stringify(chord.actions)) { |
|
83 | 99 | } else { |
84 | 100 | await port.deleteChord({ actions: chord.actions }); |
85 | 101 | } |
| 102 | + syncProgress.set({ |
| 103 | + max: progressMax, |
| 104 | + current: progressCurrent++, |
| 105 | + }); |
86 | 106 | } |
87 | 107 |
|
88 | 108 | for (const [layer, actions] of $overlay.layout.entries()) { |
89 | 109 | for (const [id, action] of actions) { |
90 | 110 | await port.setLayoutKey(layer + 1, id, action); |
| 111 | + syncProgress.set({ |
| 112 | + max: progressMax, |
| 113 | + current: progressCurrent++, |
| 114 | + }); |
91 | 115 | } |
92 | 116 | } |
93 | 117 |
|
94 | 118 | for (const [id, setting] of $overlay.settings) { |
95 | 119 | await port.setSetting(id, setting); |
| 120 | + syncProgress.set({ |
| 121 | + max: progressMax, |
| 122 | + current: progressCurrent++, |
| 123 | + }); |
96 | 124 | } |
97 | 125 |
|
98 | 126 | // Yes, this is a completely arbitrary and unnecessary delay. |
|
102 | 130 | // would be if they click it every time they change a setting. |
103 | 131 | // Because of that, we don't need to show a fearmongering message such as |
104 | 132 | // "Your device will break after you click this 10,000 times!" |
105 | | - const virtualWriteTime = 1000; |
106 | | - const startStamp = performance.now(); |
107 | | - await new Promise<void>((resolve) => { |
108 | | - function animate() { |
109 | | - const delta = performance.now() - startStamp; |
110 | | - syncProgress.set({ |
111 | | - max: virtualWriteTime, |
112 | | - current: delta, |
113 | | - }); |
114 | | - if (delta >= virtualWriteTime) { |
115 | | - resolve(); |
116 | | - } else { |
117 | | - requestAnimationFrame(animate); |
118 | | - } |
119 | | - } |
120 | | - requestAnimationFrame(animate); |
121 | | - }); |
122 | | - await port.commit(); |
| 133 | + if (needsCommit) { |
| 134 | + await port.commit(); |
| 135 | + } |
123 | 136 |
|
124 | 137 | $deviceLayout = $layout.map((layer) => |
125 | 138 | layer.map<number>(({ action }) => action), |
|
0 commit comments