From fc34a7c985e5b001e19635e638598fe5231543c3 Mon Sep 17 00:00:00 2001 From: mishamyrt Date: Sun, 10 Mar 2024 23:30:01 +0300 Subject: [PATCH] chore: migrate HID effects to `effector-queue` --- frontend/package.json | 1 + frontend/package.json.md5 | 2 +- frontend/pnpm-lock.yaml | 11 +++ frontend/src/entities/device/model/store.ts | 18 ++-- .../src/entities/keys/model/stores/keymap.ts | 5 +- .../src/entities/keys/model/stores/static.ts | 5 +- frontend/src/entities/lights/model/store.ts | 20 ++++- frontend/src/shared/lib/index.ts | 1 - frontend/src/shared/lib/sequential-effect.ts | 87 ------------------- frontend/src/shared/model/hid-effects.ts | 4 +- 10 files changed, 51 insertions(+), 103 deletions(-) delete mode 100644 frontend/src/shared/lib/sequential-effect.ts diff --git a/frontend/package.json b/frontend/package.json index 89c165dc..4971a54d 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -53,6 +53,7 @@ "colorjs.io": "0.5.0", "deep-equal": "2.2.3", "effector": "23.2.0", + "effector-queue": "1.0.2", "patronum": "2.2.0", "svelte-awesome-color-picker": "2.4.8" } diff --git a/frontend/package.json.md5 b/frontend/package.json.md5 index e07b6183..2e131c66 100755 --- a/frontend/package.json.md5 +++ b/frontend/package.json.md5 @@ -1 +1 @@ -8116f2f1fa673af023647151fc26e960 \ No newline at end of file +7874e274a99b328242098e3f01e1d9d1 \ No newline at end of file diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml index 38f56e59..405effcd 100644 --- a/frontend/pnpm-lock.yaml +++ b/frontend/pnpm-lock.yaml @@ -17,6 +17,9 @@ dependencies: effector: specifier: 23.2.0 version: 23.2.0 + effector-queue: + specifier: 1.0.2 + version: 1.0.2(effector@23.2.0) patronum: specifier: 2.2.0 version: 2.2.0(effector@23.2.0) @@ -1416,6 +1419,14 @@ packages: just-debounce-it: 1.5.0 dev: true + /effector-queue@1.0.2(effector@23.2.0): + resolution: {integrity: sha512-6g84iRCNE7ifqC9ljFGwk6yVaXwK1TtexzKb4VQOkSaoW7bmYPIxxZVIAipW8Hs9dJc2EQaUK1lfUeq5xHrFTA==} + peerDependencies: + effector: ^23 + dependencies: + effector: 23.2.0 + dev: false + /effector@23.2.0: resolution: {integrity: sha512-TqR/e5BSfAk4ZkHOykmqPmT9Nn4fLE/BhuBiVmy2cb7Vp6in8N+xMDFhWWZAgOdQjehgt/dIVQyXuWpXh3EoZA==} engines: {node: '>=11.0.0'} diff --git a/frontend/src/entities/device/model/store.ts b/frontend/src/entities/device/model/store.ts index 777b8db6..4bf06fe1 100644 --- a/frontend/src/entities/device/model/store.ts +++ b/frontend/src/entities/device/model/store.ts @@ -21,12 +21,18 @@ export const firmwareVersionStore = createStore('dev', { export const getFirmwareFx = createEffect('getFirmware', { handler: getFirmware }) export const getSupportsFx = createEffect('getSupports', { handler: getSupports }) -export const saveStateFx = createHIDEffect('saveState', saveState) -export const restoreUserStateFx = createHIDEffect('restoreUserStateFx', restoreState) -export const restoreDefaultStateFx = createHIDEffect( - 'restoreDefaultState', - restoreDefaultState -) +export const saveStateFx = createHIDEffect({ + name: 'saveStateFx', + handler: saveState +}) +export const restoreUserStateFx = createHIDEffect({ + name: 'restoreUserStateFx', + handler: restoreState +}) +export const restoreDefaultStateFx = createHIDEffect({ + name: 'restoreDefaultStateFx', + handler: restoreDefaultState +}) sample({ clock: getFirmwareFx.doneData, diff --git a/frontend/src/entities/keys/model/stores/keymap.ts b/frontend/src/entities/keys/model/stores/keymap.ts index 087f724a..155958cb 100644 --- a/frontend/src/entities/keys/model/stores/keymap.ts +++ b/frontend/src/entities/keys/model/stores/keymap.ts @@ -33,7 +33,10 @@ export const secondaryActionStore = combine(keyMapStore, selectedKeyStore, (keyM }) export const hasSecondaryActionStore = not(empty(secondaryActionStore)) -const getKeysFx = createHIDEffect('getKeys', getKeys) +const getKeysFx = createHIDEffect({ + name: 'getKeysFx', + handler: getKeys +}) const setKeysFx = createEffect('setKeys', { handler: setKeys }) // Report that supported device is connected diff --git a/frontend/src/entities/keys/model/stores/static.ts b/frontend/src/entities/keys/model/stores/static.ts index 043221a9..6e61580a 100644 --- a/frontend/src/entities/keys/model/stores/static.ts +++ b/frontend/src/entities/keys/model/stores/static.ts @@ -13,7 +13,10 @@ export const keyGroupsStore = createStore([], { name: 'keyGroups' }) export const keyNamesStore = createStore({}, { name: 'keyNames' }) const getGroupsFx = createEffect('getGroupsFx', { handler: getGroups }) -const getDefaultKeysFx = createHIDEffect('getDefaultKeysFx', getDefaultKeys) +const getDefaultKeysFx = createHIDEffect({ + name: 'getDefaultKeysFx', + handler: getDefaultKeys +}) sample({ clock: keysInitiated, diff --git a/frontend/src/entities/lights/model/store.ts b/frontend/src/entities/lights/model/store.ts index 96eeb16b..36e3fedd 100644 --- a/frontend/src/entities/lights/model/store.ts +++ b/frontend/src/entities/lights/model/store.ts @@ -33,10 +33,22 @@ const { tick } = interval({ // HID effects // This effect is using for connection check -export const getStateFx = createHIDEffect('getState', getLightState) -export const setStateFx = createHIDEffect('setState', setLightState) -export const setBacklightColorFx = createHIDEffect('setBacklightColorFx', setBacklightColor) -export const getBacklightColorsFx = createHIDEffect('getBacklightColors', getBacklightColors) +export const getStateFx = createHIDEffect({ + name: 'getStateFx', + handler: getLightState +}) +export const setStateFx = createHIDEffect({ + name: 'setStateFx', + handler: setLightState +}) +export const setBacklightColorFx = createHIDEffect({ + name: 'setBacklightColorFx', + handler: setBacklightColor +}) +export const getBacklightColorsFx = createHIDEffect({ + name: 'getBacklightColorsFx', + handler: getBacklightColors +}) // Simple effects export const getModesFx = createEffect(getModes) diff --git a/frontend/src/shared/lib/index.ts b/frontend/src/shared/lib/index.ts index 704aac78..c57ef270 100644 --- a/frontend/src/shared/lib/index.ts +++ b/frontend/src/shared/lib/index.ts @@ -1,3 +1,2 @@ export * from './from-wails-event' -export * from './sequential-effect' export * from './strings' diff --git a/frontend/src/shared/lib/sequential-effect.ts b/frontend/src/shared/lib/sequential-effect.ts deleted file mode 100644 index a362bd06..00000000 --- a/frontend/src/shared/lib/sequential-effect.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { createEffect, createEvent, createStore, type Effect, sample, type Store } from 'effector' - -type EffectHandlerFn = (params: Params) => Promise - -type EffectCreatorFn = - (effectName: string, handler: EffectHandlerFn) => Effect - -interface SequenceHooks { - stop: () => void - start: () => void -} - -interface SequenceParams { - minInterval?: number - started?: boolean -} - -type SequenceReturn = [EffectCreatorFn, Store, SequenceHooks] - -export function createSequence ({ - minInterval = 0, - started = true -}: SequenceParams): SequenceReturn { - let currentTask: Promise = Promise.resolve() - let lastTaskTime: number = 0 - let isActive = started - const taskQueue: Array<() => Promise> = [] - const $pending = createStore(false, { name: 'isRunning' }) - const runningStateChanged = createEvent('runningStateChanged') - sample({ - clock: runningStateChanged, - target: $pending - }) - - function processQueue () { - if (taskQueue.length === 0 || !isActive) return - - const task = taskQueue.shift() - if (task) { - const currentTime = Date.now() - const timeSinceLastTask = currentTime - lastTaskTime - const delay = Math.max(0, minInterval - timeSinceLastTask) - - currentTask = new Promise((resolve) => { - runningStateChanged(true) - setTimeout(() => { - task().then((result) => { - lastTaskTime = Date.now() - resolve(result) - runningStateChanged(false) - processQueue() - }) - }, delay) - }) - } - } - - function createSequentialEffect ( - effectName: string, - handler: EffectHandlerFn - ) { - const handlerWrapper = async (params: Params): Promise => { - await currentTask - return await new Promise((resolve, reject) => { - taskQueue.push(() => handler(params).then(resolve).catch(reject)) - processQueue() - }) - } - return createEffect(effectName, { - handler: handlerWrapper - }) - } - - function start () { - isActive = true - } - - function stop () { - isActive = false - } - - return [ - createSequentialEffect, - $pending, - { start, stop } - ] -} diff --git a/frontend/src/shared/model/hid-effects.ts b/frontend/src/shared/model/hid-effects.ts index 33ee7258..db174594 100644 --- a/frontend/src/shared/model/hid-effects.ts +++ b/frontend/src/shared/model/hid-effects.ts @@ -1,6 +1,6 @@ -import { createSequence } from '$shared/lib' +import { createQueue } from 'effector-queue' -const [createHIDEffect, pendingHIDStore] = createSequence({ +const [createHIDEffect, pendingHIDStore] = createQueue({ minInterval: 200 })