diff --git a/.commitlintrc.json b/.commitlintrc.json new file mode 100644 index 0000000..dbf3662 --- /dev/null +++ b/.commitlintrc.json @@ -0,0 +1,3 @@ +{ + "extends": ["@rambler-tech/commitlint-config/shared"] +} diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4a7ea30 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index 491948f..0000000 --- a/.eslintrc +++ /dev/null @@ -1,62 +0,0 @@ -{ - "extends": [ - "eslint:recommended", - "plugin:@typescript-eslint/recommended", - "plugin:react/recommended", - "prettier", - "plugin:prettier/recommended" - ], - "parser": "@typescript-eslint/parser", - "settings": { - "react": { - "version": "detect" - } - }, - "env": { - "es6": true, - "browser": true, - "node": true, - "jest": true - }, - "plugins": ["react", "@typescript-eslint", "react-hooks", "import"], - "rules": { - "prettier/prettier": [ - "error", { - "singleQuote": true, - "endOfLine": "auto", - "jsxSingleQuote": true - } - ], - "strict": 1, - "react/jsx-indent": [1, 2], - "quotes": [2, "single"], - "no-param-reassign": [0], - "no-unused-vars": 1, - "jsx-quotes": [2, "prefer-single"], - "prefer-const": 2, - "arrow-body-style": [1], - "one-var": [0], - "max-len": [2, { "code": 120 }], - "no-confusing-arrow": 0, - "comma-dangle": [2, "always-multiline"], - "newline-before-return": 2, - "import/no-duplicates": 1, - "no-duplicate-imports": 0, - "no-restricted-syntax": 0, - "react/sort-comp": 0, - "camelcase": 1, - "new-cap": 1, - "no-shadow": 0, - "react/prop-types": 0, - "react/no-string-refs": 0, - "@typescript-eslint/explicit-module-boundary-types": "off", - "@typescript-eslint/no-explicit-any": 1, - "@typescript-eslint/ban-types": 1, - "react/no-find-dom-node": 1, - "no-useless-escape": 0, - "@typescript-eslint/no-inferrable-types": 0, - "react/display-name": 0, - "no-irregular-whitespace": 0, - "no-prototype-builtins": 0 - } -} diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..e1fef74 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,6 @@ +{ + "extends": [ + "@rambler-tech/eslint-config/semi", + "@rambler-tech/eslint-config/ts" + ] +} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..629c4cc --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,55 @@ +name: Release + +on: + push: + branches: + - master + tags-ignore: + - v* + +jobs: + release: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Use Node.js + uses: actions/setup-node@v3 + with: + cache: yarn + node-version: '18.x' + - run: yarn + - run: | + git config --global user.email $BOT_EMAIL + git config --global user.name rambler-ui-bot + env: + BOT_EMAIL: ${{ secrets.BOT_EMAIL }} + - run: | + echo "@rambler-tech:registry=https://registry.npmjs.org/" > .npmrc + echo "registry=https://registry.npmjs.org/" >> .npmrc + echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc + env: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + - run: yarn build + - run: yarn release + env: + GH_TOKEN: ${{ secrets.GH_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + + pages: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Use Node.js + uses: actions/setup-node@v3 + with: + cache: yarn + node-version: '18.x' + - run: yarn + - run: yarn docs + - name: Deploy + uses: JamesIves/github-pages-deploy-action@v4 + with: + branch: gh-pages + folder: public diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..b798a77 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,67 @@ +name: Test + +on: + push: + branches: + - master + tags-ignore: + - v* + + pull_request: + types: + - opened + - synchronize + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Use Node.js + uses: actions/setup-node@v3 + with: + cache: yarn + node-version: '18.x' + - run: yarn + - run: yarn test + + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Use Node.js + uses: actions/setup-node@v3 + with: + cache: yarn + node-version: '18.x' + - run: yarn + - run: yarn lint + - run: yarn typecheck + + size: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Use Node.js + uses: actions/setup-node@v3 + with: + cache: yarn + node-version: '18.x' + - run: yarn + - run: yarn sizecheck + + commitlint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Use Node.js + uses: actions/setup-node@v3 + with: + cache: yarn + node-version: '18.x' + - run: yarn + - uses: wagoid/commitlint-github-action@v3 + env: + NODE_PATH: ${{ github.workspace }}/node_modules diff --git a/.gitignore b/.gitignore index 0e796c3..b14b6ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,11 @@ +.DS_Store node_modules +npm-debug.log +yarn-error.log +lerna-debug.log +coverage +.nyc_output +.vscode +.idea dist -build \ No newline at end of file +spec.* diff --git a/.husky/.gitignore b/.husky/.gitignore new file mode 100644 index 0000000..31354ec --- /dev/null +++ b/.husky/.gitignore @@ -0,0 +1 @@ +_ diff --git a/.husky/commit-msg b/.husky/commit-msg new file mode 100755 index 0000000..4c49ae6 --- /dev/null +++ b/.husky/commit-msg @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +yarn commitlint --edit $1 diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000..5a182ef --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +yarn lint-staged diff --git a/.lintstagedrc.json b/.lintstagedrc.json new file mode 100644 index 0000000..8509693 --- /dev/null +++ b/.lintstagedrc.json @@ -0,0 +1,5 @@ +{ + "*.{js,ts,tsx}": ["prettier --write", "eslint --fix"], + "*.md": ["prettier --write"], + "!(packages/*/tsconfig).json": ["prettier --write"] +} diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..66b00f2 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1 @@ +"@rambler-tech/prettier-config/semi" diff --git a/.size-limit.json b/.size-limit.json new file mode 100644 index 0000000..8815a3f --- /dev/null +++ b/.size-limit.json @@ -0,0 +1,10 @@ +[ + { + "path": "packages/core-v1/dist/index.js", + "limit": "1 KB" + }, + { + "path": "packages/react-v1/dist/index.js", + "limit": "1 KB" + } +] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..59dfc35 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Rambler Internet Holdings LLC + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..00cbcf0 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,9 @@ +/** @type {import('jest').Config} */ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'jsdom', + moduleDirectories: ['packages', 'node_modules'], + collectCoverage: true, + coverageReporters: ['text'], + // setupFilesAfterEnv: ['./jest.setup.js'] +}; diff --git a/lerna.json b/lerna.json new file mode 100644 index 0000000..761364f --- /dev/null +++ b/lerna.json @@ -0,0 +1,22 @@ +{ + "version": "independent", + "npmClient": "yarn", + "packages": [ + "packages/core-v1", + "packages/react-v1", + "packages/ssr" + ], + "command": { + "version": { + "allowBranch": "master", + "conventionalCommits": true, + "createRelease": "github", + "message": "chore(release): publish", + "ignoreChanges": ["**/*.md"] + }, + "publish": { + "yes": true, + "verifyAccess": false + } + } +} diff --git a/package.json b/package.json index fbe82a8..8d64ec3 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,51 @@ { "name": "reflexio", - "private": true, "description": "Business flow manager, more than just state manager", - "scripts": {}, - "workspaces": [ - "packages/*" + "keywords": [ + "reflexio" ], - "author": "Konstantin Astapov", - "license": "MIT", - "readme": "README.md", + "scripts": { + "build": "lerna exec -- tsc", + "test": "jest", + "test:watch": "jest --watch", + "lint": "eslint '**/*.{js,ts,tsx}'", + "format": "prettier --write '**/*.{js,ts,tsx}' && eslint '**/*.{js,ts,tsx}' --fix", + "typecheck": "tsc --noEmit", + "sizecheck": "lerna exec -- tsc && size-limit", + "release": "lerna version && lerna publish from-package", + "prepare": "[ -n \"$CI\" ] && exit 0 || husky install" + }, "repository": { "type": "git", - "url": "git+ssh://git@github.com/reflexio/reflexio.git" + "url": "git+https://github.com/rambler-digital-solutions/reflexio.git" }, - "keywords": [ - "reflexio" - ], + "author": "Konstantin Astapov", + "license": "MIT", + "private": true, "devDependencies": { - "@reflexio/reflexio-on-redux": "*", + "@commitlint/cli": "^19.2.1", + "@rambler-tech/commitlint-config": "^0.1.1", + "@rambler-tech/eslint-config": "^0.13.0", + "@rambler-tech/prettier-config": "^0.1.0", + "@rambler-tech/ts-config": "^0.1.1", + "@size-limit/preset-small-lib": "^11.1.2", + "@types/jest": "^29.5.12", + "eslint": "^8.57.0", + "husky": "^9.0.11", + "jest": "^29.7.0", + "jest-environment-jsdom": "^29.7.0", + "lerna": "^8.1.2", + "lint-staged": "^15.2.2", + "prettier": "^3.2.5", "react": "^18.2.0", - "react-dom": "^18.2.0", - "redux": "^4.2.1", - "uuid": "^8.3.2" - } + "redux": "^4", + "size-limit": "^11.1.2", + "ts-jest": "^29.1.2", + "typescript": "^5.4.3" + }, + "workspaces": [ + "packages/core-v1", + "packages/react-v1", + "packages/ssr" + ] } diff --git a/packages/core-v1/index.ts b/packages/core-v1/index.ts new file mode 100644 index 0000000..4f098e8 --- /dev/null +++ b/packages/core-v1/index.ts @@ -0,0 +1,7 @@ +/* eslint-disable import/no-unused-modules */ +export {Slice} from './lib/Slice'; +export {Bite} from './lib/Bite'; +export {useSystem} from './lib/System'; +export {configureRoot} from './lib/configureRoot'; +export type {UpdateOnType, DispatcherType} from './lib/types'; +export {getTriggerAndStatus, getActionType} from './lib/utils'; diff --git a/packages/v1-core/lib/Bite.ts b/packages/core-v1/lib/Bite.ts similarity index 55% rename from packages/v1-core/lib/Bite.ts rename to packages/core-v1/lib/Bite.ts index b2cc2dd..3181847 100644 --- a/packages/v1-core/lib/Bite.ts +++ b/packages/core-v1/lib/Bite.ts @@ -1,15 +1,15 @@ -import { MakeBiteProcessorType, MakeBiteReducerType } from './types'; +import type {MakeBiteProcessorType, MakeBiteReducerType} from './types'; -export const Bite: BiteType = (reducer, processor) => ({ reducer, processor }); +export const Bite: BiteType = (reducer, processor) => ({reducer, processor}); export type BiteType = < ITriggers, IState, K extends keyof ITriggers, - IRootTrigger + IRootTrigger, >( reducer: MakeBiteReducerType, - processor: MakeBiteProcessorType + processor: MakeBiteProcessorType, ) => { reducer: MakeBiteReducerType; processor: MakeBiteProcessorType; diff --git a/packages/v1-core/lib/Script.ts b/packages/core-v1/lib/Script.ts similarity index 92% rename from packages/v1-core/lib/Script.ts rename to packages/core-v1/lib/Script.ts index eb45723..35ccb08 100644 --- a/packages/v1-core/lib/Script.ts +++ b/packages/core-v1/lib/Script.ts @@ -1,4 +1,4 @@ -import { +import type { WatchArgsType, ScriptOptsType, InitArgsType, @@ -10,7 +10,7 @@ export abstract class Script< RSt, Bitename extends keyof RTg, PhK extends TriggerPhaseKeys, - Inj = unknown + Inj = unknown, > { abstract opts: ScriptOptsType; diff --git a/packages/v1-core/lib/Slice.ts b/packages/core-v1/lib/Slice.ts similarity index 66% rename from packages/v1-core/lib/Slice.ts rename to packages/core-v1/lib/Slice.ts index 979fa77..00180c0 100644 --- a/packages/v1-core/lib/Slice.ts +++ b/packages/core-v1/lib/Slice.ts @@ -1,6 +1,6 @@ -import { makeProcMiddleware } from './createMiddleware'; -import { makeReducer } from './createReducer'; -import { MakeBiteType } from './types'; +import {makeProcMiddleware} from './createMiddleware'; +import {makeReducer} from './createReducer'; +import type {MakeBiteType} from './types'; export const Slice = ( sliceName: keyof IRootState, @@ -8,15 +8,15 @@ export const Slice = ( initialState: IState, sliceConfig?: { ignoreExternal?: Array | 'ignoreAll'; - } + }, ) => { const reducer = Object.keys(bites).reduce( - (acc, curKey) => ({ ...acc, [curKey]: bites[curKey].reducer }), - {} + (acc, curKey) => ({...acc, [curKey]: bites[curKey].reducer}), + {}, ); const processor = Object.keys(bites).reduce((acc, curKey) => { if (bites[curKey].processor) { - return { ...acc, [curKey]: bites[curKey].processor }; + return {...acc, [curKey]: bites[curKey].processor}; } else { return acc; } @@ -28,13 +28,13 @@ export const Slice = ( inject>(args: Inj) { Object.assign(injected, args); }, - reducer: { [sliceName]: makeReducer(reducer, initialState) }, + reducer: {[sliceName]: makeReducer(reducer, initialState)}, middleware: makeProcMiddleware( processor, reducer, sliceName, injected, - sliceConfig + sliceConfig, ), }; }; diff --git a/packages/v1-core/lib/System.ts b/packages/core-v1/lib/System.ts similarity index 84% rename from packages/v1-core/lib/System.ts rename to packages/core-v1/lib/System.ts index b7b03d0..b58139f 100644 --- a/packages/v1-core/lib/System.ts +++ b/packages/core-v1/lib/System.ts @@ -1,5 +1,5 @@ -import { TaskQueue } from './TaskQueue'; -import { SystemConfig } from './types'; +import {TaskQueue} from './TaskQueue'; +import type {SystemConfig} from './types'; interface ProcessorOpts { propagate: boolean; @@ -33,11 +33,11 @@ export class System { public afterHandlers: Array = []; - public context: { [triggerer: string]: any } = {}; + public context: {[triggerer: string]: any} = {}; - private waits: { [triggerer: string]: any } = {}; + private waits: {[triggerer: string]: any} = {}; - private processors: { [triggerer: string]: any } = {}; + private processors: {[triggerer: string]: any} = {}; private triggerWithoutUid = (name) => name.split(':')[0]; @@ -47,13 +47,14 @@ 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) => { const timeOutId = setTimeout(() => { if (this.waits[trigger]) { this.waits[trigger].reject(`${trigger} TIMEOUT`); } }, timeout || 5000); - this.waits[trigger] = { resolve, reject, args, id: timeOutId }; + + this.waits[trigger] = {resolve, reject, args, id: timeOutId}; }; public resolveWait(trigger: string, args) { @@ -72,6 +73,7 @@ export class System { public upProcess(obj: any, trigger: string, uid?: string) { const processName = !uid ? trigger : `${trigger}:${uid}`; + this.context[processName] = obj; } diff --git a/packages/v1-core/lib/TaskQueue.ts b/packages/core-v1/lib/TaskQueue.ts similarity index 67% rename from packages/v1-core/lib/TaskQueue.ts rename to packages/core-v1/lib/TaskQueue.ts index baf1893..a2935ea 100644 --- a/packages/v1-core/lib/TaskQueue.ts +++ b/packages/core-v1/lib/TaskQueue.ts @@ -1,12 +1,12 @@ export class TaskQueue { - private currentTask: { type: string; payload: any }; + private currentTask: {type: string; payload: any}; private taskQueue: Array; - public setCurrentTask(action: { type: string; payload: any }) { + public setCurrentTask(action: {type: string; payload: any}) { this.currentTask = action; } - public getCurrentTask(): { type: string; payload: any } { + public getCurrentTask(): {type: string; payload: any} { return this.currentTask; } diff --git a/packages/v1-core/lib/configureRoot.ts b/packages/core-v1/lib/configureRoot.ts similarity index 82% rename from packages/v1-core/lib/configureRoot.ts rename to packages/core-v1/lib/configureRoot.ts index 5e7b024..f7aadb9 100644 --- a/packages/v1-core/lib/configureRoot.ts +++ b/packages/core-v1/lib/configureRoot.ts @@ -2,10 +2,10 @@ import { createStore, applyMiddleware, compose, - Reducer, - Middleware, + type Reducer, + type Middleware, } from 'redux'; -import { useSystem } from './System'; +import {useSystem} from './System'; export function configureRoot(args: { rootReducer: Reducer; @@ -13,7 +13,7 @@ export function configureRoot(args: { }) { const store = createStore( args.rootReducer, - compose(applyMiddleware(...args.middlewares)) + compose(applyMiddleware(...args.middlewares)), ); const system = useSystem(); diff --git a/packages/v1-core/lib/createMiddleware.ts b/packages/core-v1/lib/createMiddleware.ts similarity index 79% rename from packages/v1-core/lib/createMiddleware.ts rename to packages/core-v1/lib/createMiddleware.ts index 7113ebb..9bb7f13 100644 --- a/packages/v1-core/lib/createMiddleware.ts +++ b/packages/core-v1/lib/createMiddleware.ts @@ -1,20 +1,20 @@ -import { Middleware } from 'redux'; -import { createProcessorInstance } from './processor/createProcessorInstance'; -import { getInstance } from './processor/getProcessorInstance'; -import { onInit } from './processor/lifecycle/Init'; -import { BeforeUpdate } from './processor/lifecycle/Update'; -import { matchBiteName } from './processor/matchBiteName'; -import { matchInitTrigger } from './processor/matchInitTrigger'; -import { matchUpdateTrigger } from './processor/matchUpdateTrigger'; -import { prepareOpts } from './processor/prepareInstanceOpts'; -import { useSystem } from './System'; +import type {Middleware} from 'redux'; +import {createProcessorInstance} from './processor/createProcessorInstance'; +import {getInstance} from './processor/getProcessorInstance'; +import {onInit} from './processor/lifecycle/Init'; +import {BeforeUpdate} from './processor/lifecycle/Update'; +import {matchBiteName} from './processor/matchBiteName'; +import {matchInitTrigger} from './processor/matchInitTrigger'; +import {matchUpdateTrigger} from './processor/matchUpdateTrigger'; +import {prepareOpts} from './processor/prepareInstanceOpts'; +import {useSystem} from './System'; export const makeProcMiddleware = ( configs, reducers, sliceName, injected, - sliceConfig + sliceConfig, ): Middleware => { const system = useSystem(); @@ -24,6 +24,7 @@ export const makeProcMiddleware = ( const sourceSlice = action.sourceSlice; const actionType = action.type; const isBiteHit = matchBiteName(configs, actionType); + if (sliceConfig?.ignoreExternal) { if (sliceConfig.ignoreExternal === 'ignoreAll') { ignore = true; @@ -35,12 +36,14 @@ export const makeProcMiddleware = ( ignore = true; } } + const actionPayload = action.payload || null; const nexio = (args) => { system.taksQueue.setCurrentTask(action); return next(args); }; + if (isBiteHit && ignore) { return next(action); } @@ -49,40 +52,47 @@ export const makeProcMiddleware = ( 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 + actionType, ); + if (instance) { onInit(instance, actionPayload); } + if (instance.watchAfter) { // get list of events form config // check if contains then call system.afterHandlers.push(() => instance.watchAfter); } } + 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 + sliceName, ); + if (!proppagate) { forceStopPropagate = true; } }); }); } + const processorOpts = system.getProcessorInfo(action.type); system.resolveWait(action.type, action.payload); diff --git a/packages/v1-core/lib/createReducer.ts b/packages/core-v1/lib/createReducer.ts similarity index 79% rename from packages/v1-core/lib/createReducer.ts rename to packages/core-v1/lib/createReducer.ts index 7689238..727c647 100644 --- a/packages/v1-core/lib/createReducer.ts +++ b/packages/core-v1/lib/createReducer.ts @@ -1,9 +1,10 @@ -import { useSystem } from './System'; -import { MakeReducerType } from './types'; -import { getNullReducersNames } from './utils'; +import {useSystem} from './System'; +import type {MakeReducerType} from './types'; +import {getNullReducersNames} from './utils'; function makeImmutable(state, payload, reducer) { - const stateCopy = { ...state }; + const stateCopy = {...state}; + reducer(stateCopy, payload); return stateCopy; @@ -11,10 +12,11 @@ function makeImmutable(state, payload, reducer) { export function makeReducer( reducers: MakeReducerType, - initialState: StoreType + initialState: StoreType, ) { const nullReducers = getNullReducersNames(reducers); const system = useSystem(); + nullReducers.forEach((red) => { system.registerProcessor(red, { propagate: false, @@ -23,10 +25,11 @@ export function makeReducer( return function ( state: StoreType = initialState, - action: { type: string; payload: unknown } + action: {type: string; payload: unknown}, ) { const parts = action.type.split('/'); const [trigger, status] = [parts[0], parts[1]]; + if (!status) { if (reducers[trigger]) { if (typeof reducers[trigger] === 'function') { diff --git a/packages/v1-core/lib/processor/createProcessorInstance.ts b/packages/core-v1/lib/processor/createProcessorInstance.ts similarity index 92% rename from packages/v1-core/lib/processor/createProcessorInstance.ts rename to packages/core-v1/lib/processor/createProcessorInstance.ts index 1a6fed1..a151fdb 100644 --- a/packages/v1-core/lib/processor/createProcessorInstance.ts +++ b/packages/core-v1/lib/processor/createProcessorInstance.ts @@ -1,4 +1,4 @@ -import { System } from '../System'; +import {System} from '../System'; type Modes = 'stable' | 'refreshing' | 'multiple'; @@ -6,9 +6,10 @@ export function createProcessorInstance( system: System, config: any, opt: any, - actionType: string + actionType: string, ) { const mode: Modes = config.instance; + switch (mode) { case 'stable': return stableMode(system, config, opt, actionType); @@ -17,7 +18,7 @@ export function createProcessorInstance( case 'multiple': return multipleMode(system, config, opt, actionType); default: - throw Error('Unknown instance mode'); + throw new Error('Unknown instance mode'); } } @@ -25,6 +26,7 @@ function multipleMode(system, config, opt, actionType) { const processor = config.script; const newInstance = new processor(opt); const processUid = opt.uid; + system.upProcess(newInstance, actionType, processUid); return newInstance; //.init(actionPayload); @@ -34,10 +36,13 @@ function stableMode(system, config, opt, actionType) { const processor = config.script; const processUid = opt.uid; const found = system.findProcess(actionType); + if (found.length) { return found[0]; } + const newInstance = new processor(opt); + system.upProcess(newInstance, actionType, processUid); return newInstance; //.init(actionPayload); @@ -47,10 +52,13 @@ function refreshingMode(system, config, opt, actionType) { const processor = config.script; const found = system.findProcess(actionType); const processUid = opt.uid; + if (found) { system.downProcess(actionType); } + const newInstance = new processor(opt); + system.upProcess(newInstance, actionType, processUid); return newInstance; //.init(actionPayload); diff --git a/packages/v1-core/lib/processor/getProcessorInstance.ts b/packages/core-v1/lib/processor/getProcessorInstance.ts similarity index 79% rename from packages/v1-core/lib/processor/getProcessorInstance.ts rename to packages/core-v1/lib/processor/getProcessorInstance.ts index ac580ba..bbb43b5 100644 --- a/packages/v1-core/lib/processor/getProcessorInstance.ts +++ b/packages/core-v1/lib/processor/getProcessorInstance.ts @@ -1,4 +1,4 @@ -import { getActionType } from '../utils'; +import {getActionType} from '../utils'; export function getInstance(config, trigger, system) { const actionType = getActionType(trigger, config.initOn); diff --git a/packages/v1-core/lib/processor/lifecycle/Init.ts b/packages/core-v1/lib/processor/lifecycle/Init.ts similarity index 100% rename from packages/v1-core/lib/processor/lifecycle/Init.ts rename to packages/core-v1/lib/processor/lifecycle/Init.ts diff --git a/packages/v1-core/lib/processor/lifecycle/Update.ts b/packages/core-v1/lib/processor/lifecycle/Update.ts similarity index 83% rename from packages/v1-core/lib/processor/lifecycle/Update.ts rename to packages/core-v1/lib/processor/lifecycle/Update.ts index 23029a4..0e14017 100644 --- a/packages/v1-core/lib/processor/lifecycle/Update.ts +++ b/packages/core-v1/lib/processor/lifecycle/Update.ts @@ -1,4 +1,4 @@ -import { getActionType, getTriggerAndStatus } from '../../utils'; +import {getActionType, getTriggerAndStatus} from '../../utils'; export function BeforeUpdate(instance, state, action, reducers, sliceName) { //const { trigger, status } = getTriggerAndStatus(actionType); @@ -8,13 +8,13 @@ export function BeforeUpdate(instance, state, action, reducers, sliceName) { const actionPayload = action.payload; let propagate = true; let keepUpdate = false; - const stopPropagate = (args?: { keepUpdate: boolean }) => { + const stopPropagate = (args?: {keepUpdate: boolean}) => { keepUpdate = (args && args.keepUpdate) || false; propagate = false; }; if (instance.watch) { - const { trigger, status } = getTriggerAndStatus(actionType); + const {trigger, status} = getTriggerAndStatus(actionType); const updateArgs = { payload: actionPayload, trigger, @@ -26,8 +26,9 @@ export function BeforeUpdate(instance, state, action, reducers, sliceName) { if (instance.updatable) { const foundKey = Object.keys(instance.updatable).find( - (u) => u === getActionType(updateArgs.trigger, updateArgs.status) + (u) => u === getActionType(updateArgs.trigger, updateArgs.status), ); + if (foundKey) { instance[instance.updatable[foundKey]](updateArgs); } else { diff --git a/packages/core-v1/lib/processor/matchBiteName.ts b/packages/core-v1/lib/processor/matchBiteName.ts new file mode 100644 index 0000000..fcee6b9 --- /dev/null +++ b/packages/core-v1/lib/processor/matchBiteName.ts @@ -0,0 +1,7 @@ +import {getTriggerAndStatus} from '../utils'; + +export function matchBiteName(config, actionType) { + const {trigger, status} = getTriggerAndStatus(actionType); + + return Boolean(config[trigger]); +} diff --git a/packages/core-v1/lib/processor/matchInitTrigger.ts b/packages/core-v1/lib/processor/matchInitTrigger.ts new file mode 100644 index 0000000..a053f3d --- /dev/null +++ b/packages/core-v1/lib/processor/matchInitTrigger.ts @@ -0,0 +1,15 @@ +import {getTriggerAndStatus} from '../utils'; + +export function matchInitTrigger(config, actionType) { + const {trigger, status} = getTriggerAndStatus(actionType); + + 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}; + } + } + + return null; +} diff --git a/packages/v1-core/lib/processor/matchUpdateTrigger.ts b/packages/core-v1/lib/processor/matchUpdateTrigger.ts similarity index 90% rename from packages/v1-core/lib/processor/matchUpdateTrigger.ts rename to packages/core-v1/lib/processor/matchUpdateTrigger.ts index 6a36bd6..397afe0 100644 --- a/packages/v1-core/lib/processor/matchUpdateTrigger.ts +++ b/packages/core-v1/lib/processor/matchUpdateTrigger.ts @@ -1,21 +1,24 @@ -import { getTriggerAndStatus } from '../utils'; +import {getTriggerAndStatus} from '../utils'; export function matchUpdateTrigger(configs, actionType) { - const { trigger, status } = getTriggerAndStatus(actionType); + const {trigger, status} = getTriggerAndStatus(actionType); return Object.keys(configs) .filter((c) => { const conf = configs[c]; const watchScope = conf.watchScope; + if (watchScope) { if (watchScope.length === 0) { return true; } + const matchedTrigger = watchScope.find((t) => { const firstKey = Object.keys(t)[0]; return t === trigger || firstKey === trigger; }); + if (matchedTrigger) { if (typeof matchedTrigger === 'string') { return true; diff --git a/packages/v1-core/lib/processor/opts/bind.ts b/packages/core-v1/lib/processor/opts/bind.ts similarity index 89% rename from packages/v1-core/lib/processor/opts/bind.ts rename to packages/core-v1/lib/processor/opts/bind.ts index 3423c8a..2d5b409 100644 --- a/packages/v1-core/lib/processor/opts/bind.ts +++ b/packages/core-v1/lib/processor/opts/bind.ts @@ -1,4 +1,4 @@ -import { getActionType } from '../../utils'; +import {getActionType} from '../../utils'; export function Bind(system, config) { const actionType = getActionType(config.trigger, config.config.initOn); @@ -7,8 +7,9 @@ export function Bind(system, config) { const instances = system.findProcess(actionType); const fullEventName = getActionType( targetName || config.trigger, - eventName + eventName, ); + instances.forEach((ist) => { if (ist.updatable) { ist.updatable[fullEventName] = handlerName; diff --git a/packages/v1-core/lib/processor/opts/drop.ts b/packages/core-v1/lib/processor/opts/drop.ts similarity index 79% rename from packages/v1-core/lib/processor/opts/drop.ts rename to packages/core-v1/lib/processor/opts/drop.ts index 06da856..c3feca5 100644 --- a/packages/v1-core/lib/processor/opts/drop.ts +++ b/packages/core-v1/lib/processor/opts/drop.ts @@ -1,4 +1,4 @@ -import { getActionType } from '../../utils'; +import {getActionType} from '../../utils'; export function Drop(system, config) { const actionType = getActionType(config.trigger, config.config.initOn); diff --git a/packages/v1-core/lib/processor/opts/hook.ts b/packages/core-v1/lib/processor/opts/hook.ts similarity index 83% rename from packages/v1-core/lib/processor/opts/hook.ts rename to packages/core-v1/lib/processor/opts/hook.ts index 58d5ff9..072af00 100644 --- a/packages/v1-core/lib/processor/opts/hook.ts +++ b/packages/core-v1/lib/processor/opts/hook.ts @@ -1,4 +1,4 @@ -import { getActionType } from '../../utils'; +import {getActionType} from '../../utils'; export function Hook(store, config, system, uid) { //const canTrigger = config.config.canTrigger; @@ -9,7 +9,7 @@ export function Hook(store, config, system, uid) { actionStatusStart, actionStatusStop, startPAyload, - timeout + timeout, ) => { const combynedTypeStart = getActionType(actionType, actionStatusStart); const combynedTypeStop = getActionType(actionType, actionStatusStop); @@ -23,7 +23,7 @@ export function Hook(store, config, system, uid) { }, 0); return new Promise((resolve, reject) => { - system.addWait(combynedTypeStop, { resolve, reject }, timeout); + system.addWait(combynedTypeStop, {resolve, reject}, timeout); }); }; } diff --git a/packages/v1-core/lib/processor/opts/save.ts b/packages/core-v1/lib/processor/opts/save.ts similarity index 93% rename from packages/v1-core/lib/processor/opts/save.ts rename to packages/core-v1/lib/processor/opts/save.ts index 54204ef..6f613ad 100644 --- a/packages/v1-core/lib/processor/opts/save.ts +++ b/packages/core-v1/lib/processor/opts/save.ts @@ -1,13 +1,15 @@ -import { getActionType } from '../../utils'; +import {getActionType} from '../../utils'; export function Save(store, config, system, uid) { //const canTrigger = config.config.canTrigger; return (actionType, actionStatus, actionArgs) => { const process = system.findProcessByUid(uid); + if (process.length) { //if (canTrigger && canTrigger.includes(actionType)) { const combynedType = getActionType(actionType, actionStatus); + store.dispatch({ type: combynedType, payload: actionArgs, diff --git a/packages/v1-core/lib/processor/opts/setStatus.ts b/packages/core-v1/lib/processor/opts/setStatus.ts similarity index 88% rename from packages/v1-core/lib/processor/opts/setStatus.ts rename to packages/core-v1/lib/processor/opts/setStatus.ts index 21ed768..4015171 100644 --- a/packages/v1-core/lib/processor/opts/setStatus.ts +++ b/packages/core-v1/lib/processor/opts/setStatus.ts @@ -1,8 +1,9 @@ -import { getActionType } from '../../utils'; +import {getActionType} from '../../utils'; export function SetStatus(store, config, system, uid, sourceSlice) { return (status, args) => { const process = system.findProcessByUid(uid); + if (process.length) { store.dispatch({ type: getActionType(config.trigger, status), diff --git a/packages/v1-core/lib/processor/opts/trigger.ts b/packages/core-v1/lib/processor/opts/trigger.ts similarity index 94% rename from packages/v1-core/lib/processor/opts/trigger.ts rename to packages/core-v1/lib/processor/opts/trigger.ts index 3175735..53b4d76 100644 --- a/packages/v1-core/lib/processor/opts/trigger.ts +++ b/packages/core-v1/lib/processor/opts/trigger.ts @@ -1,13 +1,15 @@ -import { getActionType } from '../../utils'; +import {getActionType} from '../../utils'; export function Trigger(store, config, system, uid, sourceSlice) { const sourceTrigger = config.trigger; return (actionType, actionStatus, actionArgs) => { const process = system.findProcessByUid(uid); + if (process.length) { //if (canTrigger && canTrigger.includes(actionType)) { const combynedType = getActionType(actionType, actionStatus); + store.dispatch({ type: combynedType, payload: actionArgs, diff --git a/packages/v1-core/lib/processor/opts/triggerOnly.ts b/packages/core-v1/lib/processor/opts/triggerOnly.ts similarity index 93% rename from packages/v1-core/lib/processor/opts/triggerOnly.ts rename to packages/core-v1/lib/processor/opts/triggerOnly.ts index 629a75b..846e237 100644 --- a/packages/v1-core/lib/processor/opts/triggerOnly.ts +++ b/packages/core-v1/lib/processor/opts/triggerOnly.ts @@ -1,13 +1,15 @@ -import { getActionType } from '../../utils'; +import {getActionType} from '../../utils'; export function TriggerOnly(store, config, system, uid) { //const canTrigger = config.config.canTrigger; return (actionType, actionStatus, actionArgs) => { const process = system.findProcessByUid(uid); + if (process.length) { //if (canTrigger && canTrigger.includes(actionType)) { const combynedType = getActionType(actionType, actionStatus); + store.dispatch({ type: combynedType, payload: actionArgs, diff --git a/packages/v1-core/lib/processor/opts/wait.ts b/packages/core-v1/lib/processor/opts/wait.ts similarity index 71% rename from packages/v1-core/lib/processor/opts/wait.ts rename to packages/core-v1/lib/processor/opts/wait.ts index 1f74f3c..1498593 100644 --- a/packages/v1-core/lib/processor/opts/wait.ts +++ b/packages/core-v1/lib/processor/opts/wait.ts @@ -1,4 +1,4 @@ -import { getActionType } from '../../utils'; +import {getActionType} from '../../utils'; export function Wait(store, config, system, uid) { //const canTrigger = config.config.canTrigger; @@ -7,7 +7,7 @@ export function Wait(store, config, system, uid) { const combynedType = getActionType(actionType, actionStatus); return new Promise((resolve, reject) => { - system.addWait(combynedType, { resolve, reject }, timeout); + system.addWait(combynedType, {resolve, reject}, timeout); }); }; } diff --git a/packages/v1-core/lib/processor/prepareInstanceOpts.ts b/packages/core-v1/lib/processor/prepareInstanceOpts.ts similarity index 84% rename from packages/v1-core/lib/processor/prepareInstanceOpts.ts rename to packages/core-v1/lib/processor/prepareInstanceOpts.ts index 58354dc..0e3225f 100644 --- a/packages/v1-core/lib/processor/prepareInstanceOpts.ts +++ b/packages/core-v1/lib/processor/prepareInstanceOpts.ts @@ -1,12 +1,13 @@ -import { Drop } from './opts/drop'; -import { SetStatus } from './opts/setStatus'; -import { Trigger } from './opts/trigger'; -import { v4 } from 'uuid'; +import {v4} from 'uuid'; +import {Drop} from './opts/drop'; +import {SetStatus} from './opts/setStatus'; +import {Trigger} from './opts/trigger'; + // import { Save } from './opts/save'; // import { TriggerOnly } from './opts/triggerOnly'; -import { Wait } from './opts/wait'; -import { Hook } from './opts/hook'; -import { Bind } from './opts/bind'; +import {Wait} from './opts/wait'; +import {Hook} from './opts/hook'; +import {Bind} from './opts/bind'; export function prepareOpts(config, store, system, sliceName, injected) { const processUid = v4(); @@ -27,7 +28,7 @@ export function prepareOpts(config, store, system, sliceName, injected) { getCurrentTask: system.taksQueue.getCurrentTask, dispatch: store.dispatch, uid: processUid, - sliceName: sliceName, + sliceName, biteName: config.trigger, wait, hook, diff --git a/packages/v1-core/lib/types.ts b/packages/core-v1/lib/types.ts similarity index 73% rename from packages/v1-core/lib/types.ts rename to packages/core-v1/lib/types.ts index a5b3b1d..141ac5f 100644 --- a/packages/v1-core/lib/types.ts +++ b/packages/core-v1/lib/types.ts @@ -1,4 +1,4 @@ -import { Dispatch, Store } from 'redux'; +import type {Dispatch, Store} from 'redux'; export type SystemConfig = { env: 'dev' | 'prod' | 'test'; @@ -11,11 +11,12 @@ export type MakeReducerType = { ? { [S in keyof AC[T]]: ( state: StoreType, - payload: GetByKey + payload: GetByKey, ) => void; } : (state: StoreType, payload: AC[T]) => void; }; + export type MakeActionCreatorsType = { [T in keyof AC]: (args: AC[T]) => void; }; @@ -27,90 +28,89 @@ export type TriggerPhaseVals = { }; export type TriggerPhaseKeys< IR, - K extends keyof IR + K extends keyof IR, > = K extends keyof OmitNever> ? keyof OmitNever>[K] : ''; type TriggerPhasePayload< IR, K extends keyof IR, - S extends TriggerPhaseKeys -> = IR[K] extends BiteStatusWrap> - ? GetByKey, S> - : IR[K]; + S extends TriggerPhaseKeys, +> = + IR[K] extends BiteStatusWrap> + ? GetByKey, S> + : IR[K]; -type TriggerPhasePayload2< - IR, - K extends keyof IR -> = IR[K] extends BiteStatusWrap> - ? GetByKey, TriggerPhaseKeys> - : IR[K]; +type TriggerPhasePayload2 = + IR[K] extends BiteStatusWrap> + ? GetByKey, TriggerPhaseKeys> + : IR[K]; export type DispatcherType = < K extends keyof IR, - S extends TriggerPhaseKeys + S extends TriggerPhaseKeys, >( type: K, status: S, - payload: TriggerPhasePayload + payload: TriggerPhasePayload, ) => void; export type WaiterType = < K extends keyof IR, - S extends TriggerPhaseKeys + S extends TriggerPhaseKeys, >( type: K, status: S, - timeout?: number + timeout?: number, ) => Promise>; export type HookerType = < K extends keyof IR, S extends TriggerPhaseKeys, - P extends TriggerPhaseKeys + P extends TriggerPhaseKeys, >( type: K, statusStart: S, statusStop: P, startArgs: TriggerPhasePayload, - timeout?: number + timeout?: number, ) => Promise>; export type CatchStatusType = < - S extends TriggerPhaseKeys + S extends TriggerPhaseKeys, >( status: S, - args: unknown -) => { payload: TriggerPhasePayload; isCatched: boolean }; + args: unknown, +) => {payload: TriggerPhasePayload; isCatched: boolean}; export type CatchEventType = < K extends keyof IR, - S extends TriggerPhaseKeys + S extends TriggerPhaseKeys, >( type: K, status: S, - args: unknown -) => { payload: TriggerPhasePayload; isCatched: boolean }; + args: unknown, +) => {payload: TriggerPhasePayload; isCatched: boolean}; export type SetStatusType = < - S extends TriggerPhaseKeys + S extends TriggerPhaseKeys, >( status: S, - payload: TriggerPhasePayload + payload: TriggerPhasePayload, ) => void; export type BindHandlerType = < - S extends TriggerPhaseKeys + S extends TriggerPhaseKeys, >( status: S, - handlerName: string + handlerName: string, ) => void; export type DefautOpts< IRootTrigger, IState, BiteName extends keyof IRootTrigger, - Inj + Inj, > = { injected?: Inj; sliceName: keyof IState; @@ -132,7 +132,7 @@ export type DefautOpts< catchEvent: CatchEventType; }; -type OmitNever = { [K in keyof T as T[K] extends never ? never : K]: T[K] }; +type OmitNever = {[K in keyof T as T[K] extends never ? never : K]: T[K]}; export type BiteStatusWrap = (args: Args) => Args; @@ -151,20 +151,21 @@ export type MakeBiteReducerType< ITrigger, IRootTrigger, IState, - BiteName extends keyof ITrigger -> = ITrigger[BiteName] extends BiteStatusWrap> - ? { - [S in keyof ReturnType]: ( - state: IState, - payload: GetByKey, S> - ) => void; - } - : (state: IState, payload: ITrigger[BiteName]) => void; + BiteName extends keyof ITrigger, +> = + ITrigger[BiteName] extends BiteStatusWrap> + ? { + [S in keyof ReturnType]: ( + state: IState, + payload: GetByKey, S>, + ) => void; + } + : (state: IState, payload: ITrigger[BiteName]) => void; export type MakeBiteProcessorType< ITriggers, IRootTrigger, - BiteName extends keyof ITriggers + BiteName extends keyof ITriggers, > = { initOn: ITriggers[BiteName] extends BiteStatusWrap> ? keyof ReturnType @@ -199,7 +200,7 @@ export type ScriptOptsType< IRootTrigger, IRootState, BiteName extends keyof IRootTrigger, - Inj + Inj, > = DefautOpts; export type WatchArgsType = { @@ -215,5 +216,5 @@ export type WatchArgsType = { export type InitArgsType< ITrigger, Tr extends keyof ITrigger, - PhK extends TriggerPhaseKeys + PhK extends TriggerPhaseKeys, > = TriggerPhasePayload; diff --git a/packages/v1-core/lib/utils/index.ts b/packages/core-v1/lib/utils/index.ts similarity index 77% rename from packages/v1-core/lib/utils/index.ts rename to packages/core-v1/lib/utils/index.ts index bc52dbf..43052d0 100644 --- a/packages/v1-core/lib/utils/index.ts +++ b/packages/core-v1/lib/utils/index.ts @@ -1,7 +1,7 @@ -import { MakeReducerType } from '../types'; +import type {MakeReducerType} from '../types'; export const getNullReducersNames = ( - reducers: MakeReducerType + reducers: MakeReducerType, ): Array => Object.keys(reducers).reduce((acc: string[], cur: string) => { if (reducers[cur] === null) { @@ -9,7 +9,7 @@ export const getNullReducersNames = ( } else if (Object.keys(reducers[cur]).length) { const subArr = Object.keys(reducers[cur]) .filter((key) => reducers[cur][key] === null) - .map((k) => cur + '/' + k); + .map((k) => `${cur}/${k}`); return [...acc, ...subArr]; } @@ -18,8 +18,8 @@ export const getNullReducersNames = ( }, []); export const getTriggerAndStatus = ( - actionType: string -): { trigger: string; status: string | null } => { + actionType: string, +): {trigger: string; status: string | null} => { const parts = actionType.split('/'); return { diff --git a/packages/core-v1/package.json b/packages/core-v1/package.json new file mode 100644 index 0000000..569eebb --- /dev/null +++ b/packages/core-v1/package.json @@ -0,0 +1,25 @@ +{ + "name": "@reflexio/core-v1", + "version": "1.2.4", + "description": "Reflexio core-v1 package", + "keywords": ["react", "redux", "reflexio"], + "main": "dist", + "module": "dist", + "types": "dist/index.d.ts", + "repository": { + "type": "git", + "url": "git+https://github.com/rambler-digital-solutions/reflexio.git" + }, + "author": "Konstantin Astapov", + "license": "MIT", + "sideEffects": false, + "publishConfig": { + "access": "public" + }, + "dependencies": { + "uuid": "^8.3.2" + }, + "peerDependencies": { + "redux": ">=4.0.5" + } +} diff --git a/packages/core-v1/tsconfig.json b/packages/core-v1/tsconfig.json new file mode 120000 index 0000000..238bf1b --- /dev/null +++ b/packages/core-v1/tsconfig.json @@ -0,0 +1 @@ +../../tsconfig.package.json \ No newline at end of file diff --git a/packages/playground/.eslintrc b/packages/playground/.eslintrc deleted file mode 100644 index 491948f..0000000 --- a/packages/playground/.eslintrc +++ /dev/null @@ -1,62 +0,0 @@ -{ - "extends": [ - "eslint:recommended", - "plugin:@typescript-eslint/recommended", - "plugin:react/recommended", - "prettier", - "plugin:prettier/recommended" - ], - "parser": "@typescript-eslint/parser", - "settings": { - "react": { - "version": "detect" - } - }, - "env": { - "es6": true, - "browser": true, - "node": true, - "jest": true - }, - "plugins": ["react", "@typescript-eslint", "react-hooks", "import"], - "rules": { - "prettier/prettier": [ - "error", { - "singleQuote": true, - "endOfLine": "auto", - "jsxSingleQuote": true - } - ], - "strict": 1, - "react/jsx-indent": [1, 2], - "quotes": [2, "single"], - "no-param-reassign": [0], - "no-unused-vars": 1, - "jsx-quotes": [2, "prefer-single"], - "prefer-const": 2, - "arrow-body-style": [1], - "one-var": [0], - "max-len": [2, { "code": 120 }], - "no-confusing-arrow": 0, - "comma-dangle": [2, "always-multiline"], - "newline-before-return": 2, - "import/no-duplicates": 1, - "no-duplicate-imports": 0, - "no-restricted-syntax": 0, - "react/sort-comp": 0, - "camelcase": 1, - "new-cap": 1, - "no-shadow": 0, - "react/prop-types": 0, - "react/no-string-refs": 0, - "@typescript-eslint/explicit-module-boundary-types": "off", - "@typescript-eslint/no-explicit-any": 1, - "@typescript-eslint/ban-types": 1, - "react/no-find-dom-node": 1, - "no-useless-escape": 0, - "@typescript-eslint/no-inferrable-types": 0, - "react/display-name": 0, - "no-irregular-whitespace": 0, - "no-prototype-builtins": 0 - } -} diff --git a/packages/playground/babel.config.js b/packages/playground/babel.config.js index abca272..84cbad2 100644 --- a/packages/playground/babel.config.js +++ b/packages/playground/babel.config.js @@ -2,7 +2,7 @@ module.exports = { presets: [ - ['@babel/preset-env', { targets: { node: 'current' } }], + ['@babel/preset-env', {targets: {node: 'current'}}], '@babel/preset-typescript', ], }; diff --git a/packages/playground/src/_api/index.ts b/packages/playground/src/_api/index.ts index fd0a70d..4f06ed5 100644 --- a/packages/playground/src/_api/index.ts +++ b/packages/playground/src/_api/index.ts @@ -1,4 +1,4 @@ -import { ILetter } from 'src/letters/interfaces/Letter.interface'; +import {ILetter} from 'src/letters/interfaces/Letter.interface'; export interface UpdateLetterArgs { id: number; @@ -10,11 +10,11 @@ export interface CreateLetterArgs { } export const createLetter = async ( - args: CreateLetterArgs -): Promise<{ id: number }> => + args: CreateLetterArgs, +): Promise<{id: number}> => new Promise((resolve, reject) => { setTimeout(() => { - resolve({ id: Math.floor(Math.random() * 100) }); + resolve({id: Math.floor(Math.random() * 100)}); }, 600); }); diff --git a/packages/playground/src/_components/ErrorBoundary/index.ts b/packages/playground/src/_components/ErrorBoundary/index.ts index 30995bf..c8d2e55 100644 --- a/packages/playground/src/_components/ErrorBoundary/index.ts +++ b/packages/playground/src/_components/ErrorBoundary/index.ts @@ -1,4 +1,4 @@ -import { Component } from 'react'; +import {Component} from 'react'; interface ErrorBoundaryProps { onError?: () => void; @@ -16,7 +16,7 @@ class ErrorBoundary extends Component { this.props.onError && this.props.onError(); - this.setState({ hasError: true }); + this.setState({hasError: true}); } render() { diff --git a/packages/playground/src/_components/index.ts b/packages/playground/src/_components/index.ts index b7ae297..41d2b0e 100644 --- a/packages/playground/src/_components/index.ts +++ b/packages/playground/src/_components/index.ts @@ -1,2 +1 @@ -export { default as ErrorBoundary } from './ErrorBoundary'; - +export {default as ErrorBoundary} from './ErrorBoundary'; diff --git a/packages/playground/src/_redux/effectiveBite.ts b/packages/playground/src/_redux/effectiveBite.ts index 02e1fdf..a024be5 100644 --- a/packages/playground/src/_redux/effectiveBite.ts +++ b/packages/playground/src/_redux/effectiveBite.ts @@ -1,5 +1,5 @@ -import { Bite } from '../../../v1-core/lib'; -import { DispatcherType, BiteStatusWrap } from '../../../v1-core/lib/types'; +import {Bite} from '../../../core-v1/lib'; +import {DispatcherType, BiteStatusWrap} from '../../../core-v1/lib/types'; export type EffectiveState = { input?: I; @@ -29,11 +29,11 @@ export const effectiveBite = < ITrigger, IState, K extends keyof ITrigger, - IRootTrigger + IRootTrigger, >( promise, name: string, - opts?: EffectiveBiteOpts + opts?: EffectiveBiteOpts, ) => { const defaultStartReducer = (state: IState, payload: any) => { state[name].input = payload; @@ -69,12 +69,12 @@ export const effectiveBite = < watchScope: [name], instance: opts && opts.mode ? (opts.mode as any) : 'stable', script: EffectScript, - } as any + } as any, ) as any; }; export const effectiveInitialState = ( - state?: EffectiveState + state?: EffectiveState, ): EffectiveState => ({ data: state && state.data ? state.data : null, loading: state && state.loading ? state.loading : false, @@ -86,17 +86,22 @@ class EffectScript { public async init(args) { const co = this.opts.customOpts; + try { if (co && co.onStart) { co.onStart(this.opts.trigger); } + const result = await this.opts.addOpts.promise(); + this.opts.setStatus('done', result); + if (co && co.onDone) { co.onDone(this.opts.trigger); } - } catch (err) { - this.opts.setStatus('error', err); + } catch (error) { + this.opts.setStatus('error', error); + if (co && co.onError) { co.onError(this.opts.trigger); } diff --git a/packages/playground/src/_redux/index.ts b/packages/playground/src/_redux/index.ts index 9ec26be..57bcc04 100644 --- a/packages/playground/src/_redux/index.ts +++ b/packages/playground/src/_redux/index.ts @@ -1,11 +1,11 @@ -import { createStore, applyMiddleware, compose, Middleware } from 'redux'; -import { composeSlice } from '../compose/compose.config'; -import { lettersSlice } from '../letters/letters.config'; -import { notificationSlice } from '../notification/notification.config'; -import { popupSlice } from '../popup/popup.config'; -import { settingsSlice } from '../settings/settings.config'; +import {createStore, applyMiddleware, compose, Middleware} from 'redux'; +import {composeSlice} from '../compose/compose.config'; +import {lettersSlice} from '../letters/letters.config'; +import {notificationSlice} from '../notification/notification.config'; +import {popupSlice} from '../popup/popup.config'; +import {settingsSlice} from '../settings/settings.config'; +import {useSystem} from '../../../core-v1/lib/System'; import rootReducer from './reducer'; -import { useSystem } from '../../../v1-core/lib/System'; composeSlice.inject({ someData: 'someData For Testing', @@ -13,9 +13,11 @@ composeSlice.inject({ function configureStore() { const system = useSystem(); + system.setConfig({ env: 'dev', }); + const middlewares: Middleware[] = [ lettersSlice.middleware, settingsSlice.middleware, @@ -24,14 +26,11 @@ function configureStore() { popupSlice.middleware, ]; - const store = createStore( - rootReducer, - compose(applyMiddleware(...middlewares)) - ); - - return store; + return createStore(rootReducer, compose(applyMiddleware(...middlewares))); } + const store = configureStore(); + store.dispatch({ type: 'setContent/init', payload: null, diff --git a/packages/playground/src/_redux/reducer.ts b/packages/playground/src/_redux/reducer.ts index daf0110..04db0af 100644 --- a/packages/playground/src/_redux/reducer.ts +++ b/packages/playground/src/_redux/reducer.ts @@ -1,9 +1,9 @@ -import { combineReducers } from 'redux'; -import { composeSlice } from '../compose/compose.config'; -import { lettersSlice } from '../letters/letters.config'; -import { notificationSlice } from '../notification/notification.config'; -import { popupSlice } from '../popup/popup.config'; -import { settingsSlice } from '../settings/settings.config'; +import {combineReducers} from 'redux'; +import {composeSlice} from '../compose/compose.config'; +import {lettersSlice} from '../letters/letters.config'; +import {notificationSlice} from '../notification/notification.config'; +import {popupSlice} from '../popup/popup.config'; +import {settingsSlice} from '../settings/settings.config'; const rootReducer = combineReducers({ ...lettersSlice.reducer, diff --git a/packages/playground/src/_redux/test-core.ts b/packages/playground/src/_redux/test-core.ts index cfb3ee9..91b7403 100644 --- a/packages/playground/src/_redux/test-core.ts +++ b/packages/playground/src/_redux/test-core.ts @@ -1,15 +1,17 @@ /* eslint-disable @typescript-eslint/no-var-requires */ +import {DispatcherType} from '../../../core-v1/lib/types'; +import {getActionType} from '../../../core-v1/lib/utils'; +import {IState, ITriggers} from './types'; + const store = require('../../build'); -import { IState, ITriggers } from './types'; -import { DispatcherType } from '../../../v1-core/lib/types'; -import { getActionType } from '../../../v1-core/lib/utils'; export const useTrigger = () => { const dispatch = store.default.dispatch; const trigger: DispatcherType = (trigger, status, payload) => { const combynedType = getActionType(trigger, status as any); - dispatch({ type: combynedType, payload }); + + dispatch({type: combynedType, payload}); }; return trigger; diff --git a/packages/playground/src/_redux/types.ts b/packages/playground/src/_redux/types.ts index da47fb3..1ec2039 100644 --- a/packages/playground/src/_redux/types.ts +++ b/packages/playground/src/_redux/types.ts @@ -1,12 +1,12 @@ -import { Store } from 'redux'; -import { IComposeState, IComposeTriggers } from '../compose/compose.config'; -import { ILettersState, ILettersTriggers } from '../letters/letters.config'; +import {Store} from 'redux'; +import {IComposeState, IComposeTriggers} from '../compose/compose.config'; +import {ILettersState, ILettersTriggers} from '../letters/letters.config'; import { INotificationState, INotificationTriggers, } from '../notification/notification.config'; -import { IPopupState, IPopupTriggers } from '../popup/popup.config'; -import { ISettingsState, ISettingsTriggers } from '../settings/settings.config'; +import {IPopupState, IPopupTriggers} from '../popup/popup.config'; +import {ISettingsState, ISettingsTriggers} from '../settings/settings.config'; export type IState = { letters: ILettersState; diff --git a/packages/playground/src/_root/index.tsx b/packages/playground/src/_root/index.tsx index d730de7..81c8bd0 100644 --- a/packages/playground/src/_root/index.tsx +++ b/packages/playground/src/_root/index.tsx @@ -1,14 +1,14 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import { Provider } from 'react-redux'; +import {Provider} from 'react-redux'; import store from '../_redux'; -import { ErrorBoundary } from '../_components'; +import {ErrorBoundary} from '../_components'; -import { LettersList } from '../letters/components/LettersList'; -import { Notification } from '../notification/components/Notification'; -import { Window } from '../popup/components/Window'; -import { ReflexProvider } from '../../../v1-react/lib'; +import {LettersList} from '../letters/components/LettersList'; +import {Notification} from '../notification/components/Notification'; +import {Window} from '../popup/components/Window'; +import {ReflexProvider} from '../../../react-v1/lib'; /* ** In MULD-4 we implement popup window diff --git a/packages/playground/src/compose/components/Compose.tsx b/packages/playground/src/compose/components/Compose.tsx index 8a7f83e..e3340f9 100644 --- a/packages/playground/src/compose/components/Compose.tsx +++ b/packages/playground/src/compose/components/Compose.tsx @@ -1,13 +1,12 @@ import * as React from 'react'; -import { IState, ITriggers } from '../../_redux/types'; -import { useReflector } from '@reflexio/react-v1/lib/useReflector'; -import { useTrigger } from '@reflexio/react-v1/lib/useTrigger'; -import { ComposeWrapper } from './ComposeWrapper'; +import {useReflector, useTrigger} from '@reflexio/react-v1'; +import {IState, ITriggers} from '../../_redux/types'; +import {ComposeWrapper} from './ComposeWrapper'; import './Compose.less'; -import { PopupComposeContent } from './PopupComposeContent'; +import {PopupComposeContent} from './PopupComposeContent'; export const Compose = () => { - const { subject, to, from, body } = useReflector< + const {subject, to, from, body} = useReflector< ITriggers, IState, IState['compose'] @@ -19,14 +18,14 @@ export const Compose = () => { }, []); return ( -
-
-
-
+
+
+
+
trigger('setContent', 'syncForm', { input: 'subject', @@ -37,27 +36,25 @@ export const Compose = () => {