Skip to content

Commit 565e244

Browse files
committed
[webapp] Use useStore
1 parent 24c35c7 commit 565e244

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+210
-336
lines changed

.eslintrc.js

+8
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,12 @@ module.exports = {
4949
},
5050
],
5151
},
52+
overrides: [
53+
{
54+
files: ['packages/zombie/**/*'],
55+
rules: {
56+
'no-console': 'off',
57+
},
58+
},
59+
],
5260
};

packages/toolbox/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
},
1212
"dependencies": {
1313
"@mantou/ecs": "^0.0.1",
14-
"@mantou/gem": "^1.7.5",
15-
"duoyun-ui": "^1.1.5",
14+
"@mantou/gem": "^1.7.9",
15+
"duoyun-ui": "^1.1.9",
1616
"jszip": "^3.10.0",
1717
"qoijs": "^1.0.0"
1818
},

packages/toolbox/src/pages/audio.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { GemElement, html, adoptedStyle, customElement, createCSSSheet, css } from '@mantou/gem';
22
import { theme } from 'duoyun-ui/lib/theme';
33
import { utf8ToB64 } from 'duoyun-ui/lib/encode';
4-
import { throttle } from 'duoyun-ui/lib/utils';
4+
import { throttle } from 'duoyun-ui/lib/timer';
55
import { getInputItemType, getInputItemValue, normalizeFilename, sampleToChart, saveFile } from 'src/utils';
66
import QOI from 'qoijs';
77
import JSZip from 'jszip';
@@ -223,7 +223,7 @@ export class PAudioElement extends GemElement<State> {
223223
</dy-action-text>
224224
`,
225225
)}
226-
</dy-file-picer>
226+
</dy-file-picker>
227227
</dy-drop-area>
228228
<dy-form @itemchange=${this.#onArgChange} .inline=${true}>
229229
${Object.entries(args).map(([k, v]) =>

packages/toolbox/src/pages/font.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { theme } from 'duoyun-ui/lib/theme';
33
import { getCorSrc, getInputItemType, getInputItemValue, normalizeFilename, saveFile } from 'src/utils';
44
import QOI from 'qoijs';
55
import { Font, encodeFont } from '@mantou/ecs';
6-
import { sleep } from 'duoyun-ui/lib/utils';
6+
import { sleep } from 'duoyun-ui/lib/timer';
77

88
import type { DuoyunFormItemElement } from 'duoyun-ui/elements/form';
99
import type { DuoyunSelectElement } from 'duoyun-ui/elements/select';

packages/webapp/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"@nesbox/fceux": "^0.0.1",
2020
"@nesbox/mtapp": "^0.0.1",
2121
"@nesbox/wasm4": "^0.0.1",
22-
"duoyun-ui": "^1.1.8",
22+
"duoyun-ui": "^1.1.9",
2323
"front-matter": "^4.0.2",
2424
"graphql": "^16.2.0",
2525
"jszip": "^3.10.0",

packages/webapp/src/app.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import {
1616
import { hotkeys } from 'duoyun-ui/lib/hotkeys';
1717
import { Loadbar } from 'duoyun-ui/elements/page-loadbar';
1818
import { createPath } from '@mantou/gem/elements/route';
19-
import { routes, locationStore } from 'src/routes';
2019
import { DuoyunRouteElement } from 'duoyun-ui/elements/route';
2120
import { mediaQuery } from '@mantou/gem/helper/mediaquery';
21+
import { routes, locationStore } from 'src/routes';
2222

2323
import { preventDefault } from 'src/utils/common';
2424
import { paramKeys, queryKeys, viewTransitionName } from 'src/constants';
@@ -239,7 +239,7 @@ export class AppRootElement extends GemElement {
239239
.open=${!!configure.settingsState}
240240
@close=${toggleSettingsState}
241241
>
242-
<m-settings slot="body"></m-settings>
242+
<m-settings></m-settings>
243243
</dy-modal>
244244
245245
<dy-modal

packages/webapp/src/configure.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { createStore, updateStore } from '@mantou/gem';
1+
import { useStore } from '@mantou/gem';
22
import { isMac, getDisplayKey } from 'duoyun-ui/lib/hotkeys';
33
import { Modify } from 'duoyun-ui/lib/types';
4-
import { createCacheStore } from 'duoyun-ui/lib/utils';
4+
import { useCacheStore } from 'duoyun-ui/lib/utils';
55

66
import {
77
dispatchGlobalEvent,
@@ -177,14 +177,14 @@ interface Configure {
177177
}
178178

179179
addEventListener('focus', () => {
180-
updateStore(configure, { windowHasFocus: true });
180+
updateConfigureStore({ windowHasFocus: true });
181181
});
182182

183183
addEventListener('blur', () => {
184-
updateStore(configure, { windowHasFocus: false });
184+
updateConfigureStore({ windowHasFocus: false });
185185
});
186186

187-
export const [configure] = createCacheStore<Configure>(
187+
export const [configure, updateConfigureStore] = useCacheStore<Configure>(
188188
localStorageKeys.CONFIGURE_LOCAL_STORAGE_KEY,
189189
{
190190
windowHasFocus: document.hasFocus(),
@@ -201,41 +201,41 @@ export function getShortcut(command: keyof typeof defaultShortcuts, isDisplay =
201201
}
202202

203203
export const deleteUser = () => {
204-
updateStore(configure, { user: undefined, profile: undefined });
204+
updateConfigureStore({ user: undefined, profile: undefined });
205205
};
206206

207207
export const toggleScreencastMode = () => {
208-
updateStore(configure, { screencastMode: !configure.screencastMode });
208+
updateConfigureStore({ screencastMode: !configure.screencastMode });
209209
};
210210

211211
export const toggleFriendListState = () => {
212-
updateStore(configure, { friendListState: !configure.friendListState });
212+
updateConfigureStore({ friendListState: !configure.friendListState });
213213
};
214214

215215
export const toggleSettingsState = () => {
216-
updateStore(configure, { settingsState: !configure.settingsState });
216+
updateConfigureStore({ settingsState: !configure.settingsState });
217217
if (!configure.settingsState) dispatchGlobalEvent(globalEvents.CLOSE_SETTINGS, null);
218218
};
219219

220220
export const toggleSideNavState = (sideNavState = !configure.sideNavState) => {
221-
updateStore(configure, { sideNavState });
221+
updateConfigureStore({ sideNavState });
222222
};
223223

224224
export const toggleSearchState = () => {
225-
updateStore(configure, {
225+
updateConfigureStore({
226226
searchState: !configure.searchState,
227227
searchCommand: configure.searchState ? undefined : configure.searchCommand,
228228
});
229229
};
230230

231231
export const setSearchCommand = (command: SearchCommand | null) => {
232-
updateStore(configure, { searchCommand: command || undefined, searchState: true });
232+
updateConfigureStore({ searchCommand: command || undefined, searchState: true });
233233
};
234234

235235
export const setNesFile = (file?: File) => {
236-
updateStore(configure, { openNesFile: file });
236+
updateConfigureStore({ openNesFile: file });
237237
};
238238

239-
export const navStore = createStore({
239+
export const [navStore, updateNavStore] = useStore({
240240
room: false,
241241
});

packages/webapp/src/elements/canvas.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
import { BaseDirectory } from '@tauri-apps/api/fs';
1414
import { Time } from 'duoyun-ui/lib/time';
1515

16+
import { logger } from 'src/logger';
1617
import { saveFile } from 'src/utils/common';
1718
import { VideoFilter } from 'src/constants';
18-
import { logger } from 'src/logger';
1919
import normalVert from 'src/shaders/normal.vert?raw';
2020

2121
import 'duoyun-ui/elements/reflect';

packages/webapp/src/elements/fps.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import {
55
customElement,
66
createCSSSheet,
77
css,
8-
createStore,
98
connectStore,
10-
updateStore,
9+
useStore,
1110
} from '@mantou/gem';
1211

1312
import { theme } from 'src/theme';
@@ -20,7 +19,7 @@ export const fpsStyle = createCSSSheet(css`
2019
}
2120
`);
2221

23-
const store = createStore({
22+
const [store, update] = useStore({
2423
min: 0,
2524
max: 0,
2625
fps: 0,
@@ -53,12 +52,7 @@ const tick = () => {
5352
});
5453
const avgFps = Math.round(sum / frames.length);
5554

56-
updateStore(store, {
57-
fps,
58-
avgFps,
59-
min,
60-
max,
61-
});
55+
update({ fps, avgFps, min, max });
6256

6357
timer = requestAnimationFrame(tick);
6458
};

packages/webapp/src/elements/license.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import {
55
customElement,
66
createCSSSheet,
77
css,
8-
createStore,
98
connectStore,
10-
updateStore,
9+
useStore,
1110
} from '@mantou/gem';
1211

1312
import { getCorSrc } from 'src/utils/common';
1413

1514
import 'duoyun-ui/elements/loading';
1615

17-
const store = createStore({ license: '' });
16+
const [store, update] = useStore({ license: '' });
1817

1918
const style = createCSSSheet(css`
2019
:host {
@@ -32,7 +31,7 @@ export class NesboxLicenseElement extends GemElement {
3231
fetch(getCorSrc('https://raw.githubusercontent.com/mantou132/nesbox/dev/LICENSE'))
3332
.then((res) => res.text())
3433
.then((license) => {
35-
updateStore(store, {
34+
update({
3635
license: license
3736
.split(/\n{2,}/)
3837
.map((line) => line.replaceAll('\n', ' '))

packages/webapp/src/gamepad.ts

+34-37
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function readGamepad() {
9999
}
100100
}
101101

102-
export const listener = () => {
102+
export const listenerGamepad = () => {
103103
if ([...navigator.getGamepads()].find((e) => e?.connected)) {
104104
readGamepad();
105105
} else {
@@ -111,46 +111,43 @@ export const listener = () => {
111111
};
112112

113113
export const startKeyboardSimulation = () => {
114+
const map = Object.entries({
115+
// scroll
116+
w: GamepadBtnIndex.Up,
117+
a: GamepadBtnIndex.Left,
118+
s: GamepadBtnIndex.Down,
119+
d: GamepadBtnIndex.Right,
120+
// select/start
121+
f: GamepadBtnIndex.Select,
122+
h: GamepadBtnIndex.Start,
123+
// detail/switch
124+
j: GamepadBtnIndex.B,
125+
k: GamepadBtnIndex.A,
126+
space: GamepadBtnIndex.A,
127+
// exit
128+
4: GamepadBtnIndex.FrontLeftTop,
129+
// settings
130+
5: GamepadBtnIndex.FrontRightTop,
131+
// page/tab navigation
132+
6: GamepadBtnIndex.FrontLeftBottom,
133+
7: GamepadBtnIndex.FrontRightBottom,
134+
});
114135
addEventListener(
115136
'keydown',
116-
hotkeys({
117-
// scroll
118-
w: () => dispatchPressEvent(GamepadBtnIndex.Up),
119-
a: () => dispatchPressEvent(GamepadBtnIndex.Left),
120-
s: () => dispatchPressEvent(GamepadBtnIndex.Down),
121-
d: () => dispatchPressEvent(GamepadBtnIndex.Right),
122-
// select/start
123-
f: () => dispatchPressEvent(GamepadBtnIndex.Select),
124-
h: () => dispatchPressEvent(GamepadBtnIndex.Start),
125-
// detail/switch
126-
j: () => dispatchPressEvent(GamepadBtnIndex.B),
127-
k: () => dispatchPressEvent(GamepadBtnIndex.A),
128-
space: () => dispatchPressEvent(GamepadBtnIndex.A),
129-
// exit
130-
4: () => dispatchPressEvent(GamepadBtnIndex.FrontLeftTop),
131-
// settings
132-
5: () => dispatchPressEvent(GamepadBtnIndex.FrontRightTop),
133-
// page/tab navigation
134-
6: () => dispatchPressEvent(GamepadBtnIndex.FrontLeftBottom),
135-
7: () => dispatchPressEvent(GamepadBtnIndex.FrontRightBottom),
136-
}),
137+
hotkeys(
138+
map.reduce(
139+
(p, [key, btn]) => Object.assign(p, { [key]: () => dispatchPressEvent(btn) }),
140+
{} as Record<string, () => void>,
141+
),
142+
),
137143
);
138144
addEventListener(
139145
'keyup',
140-
hotkeys({
141-
w: () => dispatchReleaseEvent(GamepadBtnIndex.Up),
142-
a: () => dispatchReleaseEvent(GamepadBtnIndex.Left),
143-
s: () => dispatchReleaseEvent(GamepadBtnIndex.Down),
144-
d: () => dispatchReleaseEvent(GamepadBtnIndex.Right),
145-
f: () => dispatchReleaseEvent(GamepadBtnIndex.Select),
146-
h: () => dispatchReleaseEvent(GamepadBtnIndex.Start),
147-
j: () => dispatchReleaseEvent(GamepadBtnIndex.B),
148-
k: () => dispatchReleaseEvent(GamepadBtnIndex.A),
149-
space: () => dispatchReleaseEvent(GamepadBtnIndex.A),
150-
4: () => dispatchReleaseEvent(GamepadBtnIndex.FrontLeftTop),
151-
5: () => dispatchReleaseEvent(GamepadBtnIndex.FrontRightTop),
152-
6: () => dispatchReleaseEvent(GamepadBtnIndex.FrontLeftBottom),
153-
7: () => dispatchReleaseEvent(GamepadBtnIndex.FrontRightBottom),
154-
}),
146+
hotkeys(
147+
map.reduce(
148+
(p, [key, btn]) => Object.assign(p, { [key]: () => dispatchReleaseEvent(btn) }),
149+
{} as Record<string, () => void>,
150+
),
151+
),
155152
);
156153
};

0 commit comments

Comments
 (0)