From 0678814d9b6c2a746d0096ce6f6906429f608e43 Mon Sep 17 00:00:00 2001 From: Andrey Polischuk Date: Wed, 3 Apr 2024 03:19:55 +0300 Subject: [PATCH] chore: fix some lint errors --- package.json | 1 + packages/core-v1/lib/Bite.ts | 11 +- packages/core-v1/lib/System.ts | 8 +- packages/core-v1/lib/createMiddleware.ts | 129 +++++++++--------- packages/core-v1/lib/createReducer.ts | 16 +-- .../lib/processor/createProcessorInstance.ts | 12 +- .../core-v1/lib/processor/lifecycle/Update.ts | 5 +- .../core-v1/lib/processor/matchBiteName.ts | 2 +- .../core-v1/lib/processor/matchInitTrigger.ts | 11 +- .../lib/processor/matchUpdateTrigger.ts | 24 ++-- packages/core-v1/lib/processor/opts/save.ts | 1 + .../core-v1/lib/processor/opts/triggerOnly.ts | 1 + packages/core-v1/lib/processor/opts/wait.ts | 2 +- packages/core-v1/lib/types.ts | 29 ++-- packages/ssr/lib/core/app.ts | 6 +- packages/ssr/lib/core/effect-collection.ts | 12 +- packages/ssr/lib/render.tsx | 16 ++- packages/ssr/lib/slice/script.ts | 8 +- yarn.lock | 15 ++ 19 files changed, 156 insertions(+), 153 deletions(-) diff --git a/package.json b/package.json index 8d64ec3..63dd835 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "lint-staged": "^15.2.2", "prettier": "^3.2.5", "react": "^18.2.0", + "react-dom": "^18.2.0", "redux": "^4", "size-limit": "^11.1.2", "ts-jest": "^29.1.2", diff --git a/packages/core-v1/lib/Bite.ts b/packages/core-v1/lib/Bite.ts index 3181847..df57258 100644 --- a/packages/core-v1/lib/Bite.ts +++ b/packages/core-v1/lib/Bite.ts @@ -1,16 +1,11 @@ import type {MakeBiteProcessorType, MakeBiteReducerType} from './types'; -export const Bite: BiteType = (reducer, processor) => ({reducer, processor}); - -export type BiteType = < - ITriggers, - IState, - K extends keyof ITriggers, - IRootTrigger, ->( +type BiteType = ( reducer: MakeBiteReducerType, processor: MakeBiteProcessorType, ) => { reducer: MakeBiteReducerType; processor: MakeBiteProcessorType; }; + +export const Bite: BiteType = (reducer, processor) => ({reducer, processor}); diff --git a/packages/core-v1/lib/System.ts b/packages/core-v1/lib/System.ts index b58139f..605d21f 100644 --- a/packages/core-v1/lib/System.ts +++ b/packages/core-v1/lib/System.ts @@ -47,12 +47,16 @@ export class System { return arr.length > 1 ? arr[1] : null; }; - public addWait = (trigger: string, {resolve, reject, args}, timeout) => { + public addWait = ( + trigger: string, + {resolve, reject, args}, + timeout = 5000, + ) => { const timeOutId = setTimeout(() => { if (this.waits[trigger]) { this.waits[trigger].reject(`${trigger} TIMEOUT`); } - }, timeout || 5000); + }, timeout); this.waits[trigger] = {resolve, reject, args, id: timeOutId}; }; diff --git a/packages/core-v1/lib/createMiddleware.ts b/packages/core-v1/lib/createMiddleware.ts index 9bb7f13..1940689 100644 --- a/packages/core-v1/lib/createMiddleware.ts +++ b/packages/core-v1/lib/createMiddleware.ts @@ -18,84 +18,87 @@ export const makeProcMiddleware = ( ): Middleware => { const system = useSystem(); - return (store) => (next) => (action) => { - let ignore = false; - let forceStopPropagate = false; - const sourceSlice = action.sourceSlice; - const actionType = action.type; - const isBiteHit = matchBiteName(configs, actionType); + const handleInit = (store, actionType, actionPayload, skipInit) => { + const initConfig = matchInitTrigger(configs, actionType); - if (sliceConfig?.ignoreExternal) { - if (sliceConfig.ignoreExternal === 'ignoreAll') { - ignore = true; - } else if ( - sourceSlice && - sliceConfig.ignoreExternal.length && - sliceConfig.ignoreExternal.indexOf(sourceSlice) !== -1 - ) { - ignore = true; - } + if (!initConfig || skipInit) { + return; } - const actionPayload = action.payload || null; - const nexio = (args) => { - system.taksQueue.setCurrentTask(action); + const opts = prepareOpts(initConfig, store, system, sliceName, injected); + const instance = createProcessorInstance( + system, + initConfig.config, + opts, + actionType, + ); - return next(args); - }; + if (instance) { + onInit(instance, actionPayload); + } - if (isBiteHit && ignore) { - return next(action); + if (instance?.watchAfter) { + system.afterHandlers.push(() => instance.watchAfter); + } + }; + + const handleUpdate = (store, action, skipUpdate) => { + if (skipUpdate) { + return false; } - const skipInit = action.opts && action.opts.noInit; - const skipUpdate = action.opts && action.opts.noUpdate; - const initConfig = matchInitTrigger(configs, actionType); /// Возвращает 1 конфиг - const updateConfigs = matchUpdateTrigger(configs, actionType); //Возвращает массив конфигов - - if (initConfig && !skipInit) { - const opts = prepareOpts(initConfig, store, system, sliceName, injected); - const instance = createProcessorInstance( - system, - initConfig.config, - opts, - actionType, + const updateConfigs = matchUpdateTrigger(configs, action.type); + + return updateConfigs.reduce((forceStopPropagate, config) => { + const instances = getInstance(config.config, config.trigger, system); + + return ( + forceStopPropagate || + instances.some( + (instance) => + !BeforeUpdate( + instance, + store.getState(), + action, + reducers, + sliceName, + ), + ) ); + }, false); + }; - if (instance) { - onInit(instance, actionPayload); - } + return (store) => (next) => (action) => { + const { + sourceSlice, + type: actionType, + payload: actionPayload, + opts, + } = action; + const isBiteHit = matchBiteName(configs, actionType); + const ignore = + sliceConfig?.ignoreExternal && + (sliceConfig.ignoreExternal === 'ignoreAll' || + (sourceSlice && + sliceConfig.ignoreExternal.length && + sliceConfig.ignoreExternal.includes(sourceSlice))); - if (instance.watchAfter) { - // get list of events form config - // check if contains then call - system.afterHandlers.push(() => instance.watchAfter); - } + if (isBiteHit && ignore) { + return next(action); } - if (updateConfigs.length && !skipUpdate) { - updateConfigs.forEach((c) => { - const instances = getInstance(c.config, c.trigger, system); - - instances.forEach((i) => { - const proppagate = BeforeUpdate( - i, - store.getState(), - action, - reducers, - sliceName, - ); - - if (!proppagate) { - forceStopPropagate = true; - } - }); - }); - } + handleInit(store, actionType, actionPayload, opts?.noInit); + const forceStopPropagate = handleUpdate(store, action, opts?.noUpdate); const processorOpts = system.getProcessorInfo(action.type); - system.resolveWait(action.type, action.payload); + system.resolveWait(action.type, actionPayload); + + const nexio = (args) => { + system.taksQueue.setCurrentTask(action); + + return next(args); + }; return forceStopPropagate || (isBiteHit && processorOpts && !processorOpts.propagate) diff --git a/packages/core-v1/lib/createReducer.ts b/packages/core-v1/lib/createReducer.ts index 727c647..81dbc87 100644 --- a/packages/core-v1/lib/createReducer.ts +++ b/packages/core-v1/lib/createReducer.ts @@ -27,19 +27,15 @@ export function makeReducer( state: StoreType = initialState, action: {type: string; payload: unknown}, ) { - const parts = action.type.split('/'); - const [trigger, status] = [parts[0], parts[1]]; + const [trigger, status] = action.type.split('/'); + const reducer = reducers[trigger]; if (!status) { - if (reducers[trigger]) { - if (typeof reducers[trigger] === 'function') { - return makeImmutable(state, action.payload, reducers[trigger]); - } - } - } else if (reducers[trigger]) { - if (reducers[trigger][status]) { - return makeImmutable(state, action.payload, reducers[trigger][status]); + if (typeof reducer === 'function') { + return makeImmutable(state, action.payload, reducer); } + } else if (reducer?.[status]) { + return makeImmutable(state, action.payload, reducer[status]); } return state; diff --git a/packages/core-v1/lib/processor/createProcessorInstance.ts b/packages/core-v1/lib/processor/createProcessorInstance.ts index a151fdb..2f6b66b 100644 --- a/packages/core-v1/lib/processor/createProcessorInstance.ts +++ b/packages/core-v1/lib/processor/createProcessorInstance.ts @@ -23,8 +23,8 @@ export function createProcessorInstance( } function multipleMode(system, config, opt, actionType) { - const processor = config.script; - const newInstance = new processor(opt); + const Processor = config.script; + const newInstance = new Processor(opt); const processUid = opt.uid; system.upProcess(newInstance, actionType, processUid); @@ -33,7 +33,7 @@ function multipleMode(system, config, opt, actionType) { } function stableMode(system, config, opt, actionType) { - const processor = config.script; + const Processor = config.script; const processUid = opt.uid; const found = system.findProcess(actionType); @@ -41,7 +41,7 @@ function stableMode(system, config, opt, actionType) { return found[0]; } - const newInstance = new processor(opt); + const newInstance = new Processor(opt); system.upProcess(newInstance, actionType, processUid); @@ -49,7 +49,7 @@ function stableMode(system, config, opt, actionType) { } function refreshingMode(system, config, opt, actionType) { - const processor = config.script; + const Processor = config.script; const found = system.findProcess(actionType); const processUid = opt.uid; @@ -57,7 +57,7 @@ function refreshingMode(system, config, opt, actionType) { system.downProcess(actionType); } - const newInstance = new processor(opt); + const newInstance = new Processor(opt); system.upProcess(newInstance, actionType, processUid); diff --git a/packages/core-v1/lib/processor/lifecycle/Update.ts b/packages/core-v1/lib/processor/lifecycle/Update.ts index 0e14017..66e08fd 100644 --- a/packages/core-v1/lib/processor/lifecycle/Update.ts +++ b/packages/core-v1/lib/processor/lifecycle/Update.ts @@ -7,9 +7,8 @@ export function BeforeUpdate(instance, state, action, reducers, sliceName) { const actionType = action.type; const actionPayload = action.payload; let propagate = true; - let keepUpdate = false; - const stopPropagate = (args?: {keepUpdate: boolean}) => { - keepUpdate = (args && args.keepUpdate) || false; + + const stopPropagate = () => { propagate = false; }; diff --git a/packages/core-v1/lib/processor/matchBiteName.ts b/packages/core-v1/lib/processor/matchBiteName.ts index fcee6b9..41dca92 100644 --- a/packages/core-v1/lib/processor/matchBiteName.ts +++ b/packages/core-v1/lib/processor/matchBiteName.ts @@ -1,7 +1,7 @@ import {getTriggerAndStatus} from '../utils'; export function matchBiteName(config, actionType) { - const {trigger, status} = getTriggerAndStatus(actionType); + const {trigger} = getTriggerAndStatus(actionType); return Boolean(config[trigger]); } diff --git a/packages/core-v1/lib/processor/matchInitTrigger.ts b/packages/core-v1/lib/processor/matchInitTrigger.ts index a053f3d..562f49e 100644 --- a/packages/core-v1/lib/processor/matchInitTrigger.ts +++ b/packages/core-v1/lib/processor/matchInitTrigger.ts @@ -1,14 +1,11 @@ import {getTriggerAndStatus} from '../utils'; -export function matchInitTrigger(config, actionType) { +export function matchInitTrigger(configs, actionType) { const {trigger, status} = getTriggerAndStatus(actionType); + const config = configs[trigger]; - if (config[trigger]) { - if (!config[trigger].initOn) { - return {config: config[trigger], trigger}; - } else if (config[trigger] && config[trigger].initOn === status) { - return {config: config[trigger], trigger}; - } + if (config && (!config.initOn || config.initOn === status)) { + return {config, trigger}; } return null; diff --git a/packages/core-v1/lib/processor/matchUpdateTrigger.ts b/packages/core-v1/lib/processor/matchUpdateTrigger.ts index 397afe0..3ecf232 100644 --- a/packages/core-v1/lib/processor/matchUpdateTrigger.ts +++ b/packages/core-v1/lib/processor/matchUpdateTrigger.ts @@ -14,26 +14,22 @@ export function matchUpdateTrigger(configs, actionType) { } const matchedTrigger = watchScope.find((t) => { - const firstKey = Object.keys(t)[0]; + const [firstKey] = Object.keys(t); return t === trigger || firstKey === trigger; }); - if (matchedTrigger) { - if (typeof matchedTrigger === 'string') { - return true; - } else if ( - matchedTrigger[Object.keys(matchedTrigger)[0]] === status + if (typeof matchedTrigger === 'string') { + return true; + } else if (matchedTrigger) { + const [firstKey] = Object.keys(matchedTrigger); + const matchedStatus = matchedTrigger[firstKey]; + + if ( + matchedStatus === status || + (Array.isArray(matchedStatus) && matchedStatus.includes(status)) ) { return true; - } else if ( - Array.isArray(matchedTrigger[Object.keys(matchedTrigger)[0]]) - ) { - if ( - matchedTrigger[Object.keys(matchedTrigger)[0]].includes(status) - ) { - return true; - } } } } diff --git a/packages/core-v1/lib/processor/opts/save.ts b/packages/core-v1/lib/processor/opts/save.ts index 6f613ad..57b4726 100644 --- a/packages/core-v1/lib/processor/opts/save.ts +++ b/packages/core-v1/lib/processor/opts/save.ts @@ -1,5 +1,6 @@ import {getActionType} from '../../utils'; +// eslint-disable-next-line import/no-unused-modules export function Save(store, config, system, uid) { //const canTrigger = config.config.canTrigger; diff --git a/packages/core-v1/lib/processor/opts/triggerOnly.ts b/packages/core-v1/lib/processor/opts/triggerOnly.ts index 846e237..99d515e 100644 --- a/packages/core-v1/lib/processor/opts/triggerOnly.ts +++ b/packages/core-v1/lib/processor/opts/triggerOnly.ts @@ -1,5 +1,6 @@ import {getActionType} from '../../utils'; +// eslint-disable-next-line import/no-unused-modules export function TriggerOnly(store, config, system, uid) { //const canTrigger = config.config.canTrigger; diff --git a/packages/core-v1/lib/processor/opts/wait.ts b/packages/core-v1/lib/processor/opts/wait.ts index 1498593..881b9f5 100644 --- a/packages/core-v1/lib/processor/opts/wait.ts +++ b/packages/core-v1/lib/processor/opts/wait.ts @@ -1,6 +1,6 @@ import {getActionType} from '../../utils'; -export function Wait(store, config, system, uid) { +export function Wait(store, config, system, _uid) { //const canTrigger = config.config.canTrigger; return (actionType, actionStatus, timeout) => { diff --git a/packages/core-v1/lib/types.ts b/packages/core-v1/lib/types.ts index 141ac5f..076c3d9 100644 --- a/packages/core-v1/lib/types.ts +++ b/packages/core-v1/lib/types.ts @@ -4,7 +4,7 @@ export type SystemConfig = { env: 'dev' | 'prod' | 'test'; }; -export type GetByKey = K extends keyof T ? T[K] : null; +type GetByKey = K extends keyof T ? T[K] : null; export type MakeReducerType = { [T in keyof AC]: AC[T] extends Record @@ -17,21 +17,19 @@ export type MakeReducerType = { : (state: StoreType, payload: AC[T]) => void; }; -export type MakeActionCreatorsType = { - [T in keyof AC]: (args: AC[T]) => void; -}; - -export type TriggerPhaseVals = { +type TriggerPhaseVals = { [K in keyof IR]: IR[K] extends BiteStatusWrap> ? ReturnType : never; }; + export type TriggerPhaseKeys< IR, K extends keyof IR, > = K extends keyof OmitNever> ? keyof OmitNever>[K] : ''; + type TriggerPhasePayload< IR, K extends keyof IR, @@ -55,16 +53,13 @@ export type DispatcherType = < payload: TriggerPhasePayload, ) => void; -export type WaiterType = < - K extends keyof IR, - S extends TriggerPhaseKeys, ->( +type WaiterType = >( type: K, status: S, timeout?: number, ) => Promise>; -export type HookerType = < +type HookerType = < K extends keyof IR, S extends TriggerPhaseKeys, P extends TriggerPhaseKeys, @@ -76,14 +71,14 @@ export type HookerType = < timeout?: number, ) => Promise>; -export type CatchStatusType = < +type CatchStatusType = < S extends TriggerPhaseKeys, >( status: S, args: unknown, ) => {payload: TriggerPhasePayload; isCatched: boolean}; -export type CatchEventType = < +type CatchEventType = < K extends keyof IR, S extends TriggerPhaseKeys, >( @@ -92,21 +87,21 @@ export type CatchEventType = < args: unknown, ) => {payload: TriggerPhasePayload; isCatched: boolean}; -export type SetStatusType = < +type SetStatusType = < S extends TriggerPhaseKeys, >( status: S, payload: TriggerPhasePayload, ) => void; -export type BindHandlerType = < +type BindHandlerType = < S extends TriggerPhaseKeys, >( status: S, handlerName: string, ) => void; -export type DefautOpts< +type DefautOpts< IRootTrigger, IState, BiteName extends keyof IRootTrigger, @@ -149,7 +144,7 @@ export type UpdateOnType = Array< export type MakeBiteReducerType< ITrigger, - IRootTrigger, + _IRootTrigger, IState, BiteName extends keyof ITrigger, > = diff --git a/packages/ssr/lib/core/app.ts b/packages/ssr/lib/core/app.ts index c572019..ccb6a74 100644 --- a/packages/ssr/lib/core/app.ts +++ b/packages/ssr/lib/core/app.ts @@ -5,7 +5,7 @@ export class App { public isReady: boolean; - private calledState: {}; + private calledState: Record = {}; static getApp() { if (App.instance) { @@ -16,9 +16,7 @@ export class App { } public setEffectCalledState = (id: string): void => { - if (!App.instance.calledState[id]) { - App.instance.calledState[id] = true; - } + App.instance.calledState[id] ||= true; }; public getEffectCalledState = (id: string): boolean => { diff --git a/packages/ssr/lib/core/effect-collection.ts b/packages/ssr/lib/core/effect-collection.ts index 976bff2..d00e521 100644 --- a/packages/ssr/lib/core/effect-collection.ts +++ b/packages/ssr/lib/core/effect-collection.ts @@ -46,16 +46,16 @@ export class EffectCollection { results.forEach((result, num) => { const effect = waited[num]; + if (!effect) { + return; + } + if (result.status === 'fulfilled') { - if (effect) { - effect.done(); - } + effect.done(); } if (result.status === 'rejected') { - if (effect) { - effect.failed(); - } + effect.failed(); } }); } diff --git a/packages/ssr/lib/render.tsx b/packages/ssr/lib/render.tsx index f821f90..3089299 100644 --- a/packages/ssr/lib/render.tsx +++ b/packages/ssr/lib/render.tsx @@ -1,7 +1,7 @@ import React from 'react'; import ReactDOMServer, { - RenderToPipeableStreamOptions, - PipeableStream, + // type RenderToPipeableStreamOptions, + type PipeableStream, } from 'react-dom/server'; import {App} from './core/app'; @@ -11,10 +11,10 @@ interface IState { [key: string]: unknown; } -interface IServerRenderResultString { - html: string; - state: IState; -} +// interface IServerRenderResultString { +// html: string; +// state: IState; +// } interface IServerRenderResultStream { stream: PipeableStream; @@ -27,7 +27,9 @@ interface IServerRenderResultStream { // } export const serverRender = { - stream: async (): Promise => { + stream: async < + _T extends () => void, + >(): Promise => { // opts?: IServerRenderOptions, if (typeof ReactDOMServer.renderToPipeableStream === 'undefined') { throw new Error('Streaming is available only on React 18 or more'); diff --git a/packages/ssr/lib/slice/script.ts b/packages/ssr/lib/slice/script.ts index ebf97f5..48d8999 100644 --- a/packages/ssr/lib/slice/script.ts +++ b/packages/ssr/lib/slice/script.ts @@ -1,9 +1,9 @@ import {isClient} from '../utils'; export class SsrScript { - constructor(private opts) {} - - private condition; + constructor(private opts) { + this.opts = opts; + } public init() { const client = isClient(); @@ -18,7 +18,7 @@ export class SsrScript { } } - public update(args) { + public update(_args) { // checkIsReady // if(checkIsReady) { // App.commitState() diff --git a/yarn.lock b/yarn.lock index 98317f3..7da5300 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7111,6 +7111,14 @@ quick-lru@^4.0.1: resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-4.0.1.tgz#5b8878f113a58217848c6482026c73e1ba57727f" integrity sha512-ARhCpm70fzdcvNQfPoy49IaanKkTlRWF2JMzqhcJbhSFRZv7nPTvZJdcY7301IPmvW+/p0RgIWnQDLJxifsQ7g== +react-dom@^18.2.0: + version "18.2.0" + resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.2.0.tgz#22aaf38708db2674ed9ada224ca4aa708d821e3d" + integrity sha512-6IMTriUmvsjHUjNtEDudZfuDQUoWXVxKHhlEGSk81n4YFS+r/Kl99wXiwlVXtPBtJenozv2P+hxDsw9eA7Xo6g== + dependencies: + loose-envify "^1.1.0" + scheduler "^0.23.0" + react-is@^16.13.1: version "16.13.1" resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" @@ -7480,6 +7488,13 @@ saxes@^6.0.0: dependencies: xmlchars "^2.2.0" +scheduler@^0.23.0: + version "0.23.0" + resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.23.0.tgz#ba8041afc3d30eb206a487b6b384002e4e61fdfe" + integrity sha512-CtuThmgHNg7zIZWAXi3AsyIzA3n4xx7aNyjwC2VJldO2LMVDhFK+63xGqq6CsJH4rTAt6/M+N4GhZiDYPx9eUw== + dependencies: + loose-envify "^1.1.0" + scslre@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/scslre/-/scslre-0.3.0.tgz#c3211e9bfc5547fc86b1eabaa34ed1a657060155"