├── LICENSE.md ├── .vscode ├── settings.json └── launch.json ├── .babelrc ├── public ├── favicon.ico ├── manifest.json └── index.html ├── src ├── index.css ├── package │ └── fastkit-dva │ │ ├── index.js │ │ ├── utils │ │ └── PropTypes.js │ │ ├── lib │ │ ├── utils │ │ │ ├── PropTypes.js.map │ │ │ └── PropTypes.js │ │ └── src │ │ │ ├── Provider.js.map │ │ │ ├── connect.js.map │ │ │ ├── Provider.js │ │ │ ├── createStore.js.map │ │ │ ├── createStore.js │ │ │ └── connect.js │ │ ├── package-lock.json │ │ ├── tsconfig.json │ │ ├── package.json │ │ └── src │ │ ├── Provider.js │ │ ├── connect.js │ │ └── createStore.js ├── App.test.js ├── App.css ├── models │ └── app.js ├── index.js ├── App.js ├── logo.svg └── registerServiceWorker.js ├── .gitignore ├── .github └── workflows │ └── nodejs.yml ├── .eslintrc ├── package.json └── README.md /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | Copyright (C) 2018 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "git.ignoreLimitWarning": true 3 | } -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["env"], 3 | "plugins": ["transform-decorators-legacy"] 4 | } -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fangkyi03/fastdva-core/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /src/package/fastkit-dva/index.js: -------------------------------------------------------------------------------- 1 | import createStore from './src/createStore' 2 | import Provider from './src/Provider' 3 | import connect from './src/connect' 4 | export { 5 | createStore, 6 | Provider, 7 | connect 8 | } -------------------------------------------------------------------------------- /src/package/fastkit-dva/utils/PropTypes.js: -------------------------------------------------------------------------------- 1 | import PropTypes from 'prop-types' 2 | 3 | export const storeShape = PropTypes.shape({ 4 | subscribe: PropTypes.func.isRequired, 5 | dispatch: PropTypes.func.isRequired, 6 | getState: PropTypes.func.isRequired 7 | }) 8 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /src/package/fastkit-dva/lib/utils/PropTypes.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"PropTypes.js","sourceRoot":"","sources":["../../utils/PropTypes.js"],"names":[],"mappings":";;;;;;;;;;;YAEA,wBAAa,UAAU,GAAG,oBAAS,CAAC,KAAK,CAAC;gBACxC,SAAS,EAAE,oBAAS,CAAC,IAAI,CAAC,UAAU;gBACpC,QAAQ,EAAE,oBAAS,CAAC,IAAI,CAAC,UAAU;gBACnC,QAAQ,EAAE,oBAAS,CAAC,IAAI,CAAC,UAAU;aACpC,CAAC,EAAA;QACF,CAAC"} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Build and Release Folders 2 | bin/ 3 | bin-debug/ 4 | bin-release/ 5 | 6 | # Other files and folders 7 | .settings/ 8 | 9 | # Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties` 10 | # should NOT be excluded as they contain compiler settings and other important 11 | # information for Eclipse / Flash Builder. 12 | /node_modules -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /src/package/fastkit-dva/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koa-js", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "redux-saga": { 8 | "version": "0.16.0", 9 | "resolved": "http://registry.npm.taobao.org/redux-saga/download/redux-saga-0.16.0.tgz", 10 | "integrity": "sha1-CiMdsKFIkwHdmA9vL4jYztQY9yQ=" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 80px; 8 | } 9 | 10 | .App-header { 11 | background-color: #222; 12 | height: 150px; 13 | padding: 20px; 14 | color: white; 15 | } 16 | 17 | .App-title { 18 | font-size: 1.5em; 19 | } 20 | 21 | .App-intro { 22 | font-size: large; 23 | } 24 | 25 | @keyframes App-logo-spin { 26 | from { transform: rotate(0deg); } 27 | to { transform: rotate(360deg); } 28 | } 29 | -------------------------------------------------------------------------------- /src/package/fastkit-dva/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": true, 3 | "compilerOptions": { 4 | "module": "system", 5 | "noImplicitAny": true, 6 | "removeComments": true, 7 | "preserveConstEnums": true, 8 | "outDir":"./lib", 9 | "allowJs": true, 10 | "target": "es5", 11 | "sourceMap": true 12 | }, 13 | "include": [ 14 | "src/**/*" 15 | ], 16 | "exclude": [ 17 | "node_modules", 18 | "lib", 19 | "index" 20 | ] 21 | } -------------------------------------------------------------------------------- /src/models/app.js: -------------------------------------------------------------------------------- 1 | export default { 2 | namespace : 'app', 3 | state : { 4 | num: 0 5 | }, 6 | reducers : { 7 | add(state, {payload}) { 8 | return ({...state,num:state.num + 1}) 9 | }, 10 | reduce(state,{payload}){ 11 | return ({...state,num:state.num - 1}) 12 | }, 13 | reset(state,{payload}){ 14 | return ({...state,num:0}) 15 | } 16 | }, 17 | effects : { 18 | *clear({payload},{select,put}){ 19 | yield put({type:'app/reset'}) 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: Node CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | matrix: 12 | node-version: [8.x, 10.x, 12.x] 13 | 14 | steps: 15 | - uses: actions/checkout@v1 16 | - name: Use Node.js ${{ matrix.node-version }} 17 | uses: actions/setup-node@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - name: npm install, build, and test 21 | run: | 22 | npm i 23 | npm run build --if-present 24 | env: 25 | CI: true 26 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import registerServiceWorker from './registerServiceWorker'; 6 | 7 | import {createStore,Provider} from './package/fastkit-dva' 8 | 9 | import appModels from './models/app' 10 | 11 | const app = createStore({}) 12 | app.model(appModels) 13 | const AppView = () => { 14 | return ( 15 | 16 | 17 | 18 | ) 19 | } 20 | ReactDOM.render(, document.getElementById('root')); 21 | registerServiceWorker(); 22 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "commonjs": true, 5 | "es6": true 6 | }, 7 | "extends": "eslint:recommended", 8 | "parserOptions": { 9 | "ecmaFeatures": { 10 | "experimentalObjectRestSpread": true, 11 | "jsx": true 12 | }, 13 | "sourceType": "module" 14 | }, 15 | "plugins": [ 16 | "react" 17 | ], 18 | "rules": { 19 | "no-unused-vars":"off", 20 | "no-console":"off", 21 | "no-new-require":"off", 22 | "no-mixed-requires":"off", 23 | "require-yield":"off", 24 | "global-require":"off" 25 | } 26 | } -------------------------------------------------------------------------------- /src/package/fastkit-dva/lib/src/Provider.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"Provider.js","sourceRoot":"","sources":["../../src/Provider.js"],"names":[],"mappings":";;;;;;;;;;;;;IAGA;QACI;YAAuB,4BAAmB;YAEtC,kBAAY,KAAK,EAAE,OAAO;gBAA1B,YACE,kBAAM,KAAK,EAAE,OAAO,CAAC,SAEtB;gBADC,KAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;;YAC3B,CAAC;YAED,kCAAe,GAAf;gBACI,MAAM,CAAC,EAAC,OAAO,EAAC,IAAI,CAAC,KAAK,EAAC,CAAA;YAC/B,CAAC;YAED,yBAAM,GAAN;gBACI,MAAM,CAAC,eAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;YACnD,CAAC;YACL,eAAC;QAAD,CAAC,AAdD,CAAuB,eAAK,CAAC,aAAa,GAczC;QAED,QAAQ,CAAC,iBAAiB,GAAG;YACzB,OAAO,EAAC,sBAAU,CAAC,UAAU;SAChC,CAAA;QACD,MAAM,CAAC,QAAQ,CAAA;IACnB,CAAC;;;;;;;;;;;;;iCAEc,cAAc,EAAE;QAAA,CAAC"} -------------------------------------------------------------------------------- /src/package/fastkit-dva/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fastkit-dva", 3 | "version": "1.0.2", 4 | "description": "目标是是打造一个性能更加好的框架", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/fangkyi03/koa.git" 12 | }, 13 | "keywords": [ 14 | "dva", 15 | "redux", 16 | "react-redux", 17 | "redux-saga" 18 | ], 19 | "author": "fangkyi02", 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://github.com/fangkyi03/koa/issues" 23 | }, 24 | "homepage": "https://github.com/fangkyi03/koa#readme", 25 | "dependencies": { 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/package/fastkit-dva/src/Provider.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import {storeShape} from '../utils/PropTypes'; 3 | 4 | export function createProvider(){ 5 | class Provider extends React.PureComponent{ 6 | 7 | constructor(props, context) { 8 | super(props, context) 9 | this.store = props.store; 10 | } 11 | 12 | getChildContext(){ 13 | return {'store':this.store} 14 | } 15 | 16 | render(){ 17 | return React.Children.only(this.props.children) 18 | } 19 | } 20 | 21 | Provider.childContextTypes = { 22 | 'store':storeShape.isRequired 23 | } 24 | return Provider 25 | } 26 | 27 | export default createProvider() -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koajs", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "fastkit-dva": "^1.0.2", 7 | "invariant": "^2.2.4", 8 | "prop-types": "^15.6.1", 9 | "react": "^16.2.0", 10 | "react-dom": "^16.2.0", 11 | "react-scripts": "1.1.1", 12 | "redux-saga": "^0.16.0" 13 | }, 14 | "scripts": { 15 | "start": "react-scripts start", 16 | "build": "react-scripts build", 17 | "test": "react-scripts test --env=jsdom", 18 | "eject": "react-scripts eject" 19 | }, 20 | "devDependencies": { 21 | "babel-cli": "^6.26.0", 22 | "babel-plugin-transform-decorators-legacy": "^1.3.4", 23 | "babel-preset-env": "^1.6.1", 24 | "eslint": "^4.18.2", 25 | "eslint-plugin-react": "^7.7.0" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/package/fastkit-dva/lib/utils/PropTypes.js: -------------------------------------------------------------------------------- 1 | System.register(["prop-types"], function (exports_1, context_1) { 2 | "use strict"; 3 | var __moduleName = context_1 && context_1.id; 4 | var prop_types_1, storeShape; 5 | return { 6 | setters: [ 7 | function (prop_types_1_1) { 8 | prop_types_1 = prop_types_1_1; 9 | } 10 | ], 11 | execute: function () { 12 | exports_1("storeShape", storeShape = prop_types_1.default.shape({ 13 | subscribe: prop_types_1.default.func.isRequired, 14 | dispatch: prop_types_1.default.func.isRequired, 15 | getState: prop_types_1.default.func.isRequired 16 | })); 17 | } 18 | }; 19 | }); 20 | //# sourceMappingURL=PropTypes.js.map -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | 5 | import {connect} from './package/fastkit-dva'; 6 | 7 | class App extends Component { 8 | 9 | /** 10 | * 添加按钮被按下 11 | * 12 | * @memberof App 13 | */ 14 | onButtonAdd = () =>{ 15 | this.props.dispatch({type:'app/add'}) 16 | } 17 | 18 | /** 19 | * 减少按钮被按下 20 | * 21 | * @memberof App 22 | */ 23 | onButtonReduce = () =>{ 24 | this.props.dispatch({type:'app/reduce'}) 25 | } 26 | 27 | /** 28 | * 重置按钮被按下 29 | * 30 | * @memberof App 31 | */ 32 | onButtonClear = () =>{ 33 | this.props.dispatch({type:'app/clear'}) 34 | } 35 | 36 | render() { 37 | return ( 38 |
39 |
40 | logo 41 |

Welcome to React

42 |
43 |

44 | To get started, edit src/App.js and save to reload. 45 |

46 | 47 | 48 | 49 |
  • {`D显示${this.props.num}`}
  • 50 |
    51 | ); 52 | } 53 | } 54 | 55 | export default connect(['app'],({app})=>({...app}))(App) -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
    29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/package/fastkit-dva/lib/src/connect.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"connect.js","sourceRoot":"","sources":["../../src/connect.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;IAIA,uBACI,SAAc,EACd,eAAe,EACf,kBAAkB,EAClB,UAAU,EACV,EAGM;QAPN,0BAAA,EAAA,cAAc;YAId,4BAGM,EAFF,YAAW,EAAX,gCAAW,EACX,eAAe,EAAf,oCAAe;QAGnB;YAAsB,2BAAmB;YAErC,iBAAY,KAAK,EAAC,OAAO;gBAAzB,YACI,kBAAM,KAAK,EAAC,OAAO,CAAC,SAIvB;gBAcD,0BAAoB,GAAG;oBACnB,SAAS,CAAC,OAAO,CAAC,UAAC,CAAC;wBAChB,KAAI,CAAC,SAAS,CAAC,OAAO,CAAC,UAAC,EAAE;4BACtB,EAAE,CAAC,CAAC,KAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA,CAAC;gCAChC,OAAO,KAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;4BACzC,CAAC;wBACL,CAAC,CAAC,CAAA;oBACN,CAAC,CAAC,CAAA;gBACN,CAAC,CAAC;gBAOF,mBAAa,GAAG;oBACZ,SAAS,CAAC,OAAO,CAAC,UAAC,CAAC;wBAChB,KAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAA;wBAC3C,KAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,KAAI,CAAC,aAAa,CAAA;oBACxF,CAAC,CAAC,CAAA;gBACN,CAAC,CAAA;gBAOD,mBAAa,GAAG;oBACZ,KAAI,CAAC,WAAW,EAAE,CAAA;gBACtB,CAAC,CAAA;gBAOD,mBAAa,GAAG;oBACZ,MAAM,CAAC,eAAe,CAAC,KAAI,CAAC,UAAU,CAAC,CAAA;gBAC3C,CAAC,CAAA;gBAvDG,KAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAA;gBAC1B,KAAI,CAAC,UAAU,GAAG,KAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAA;gBACvC,KAAI,CAAC,SAAS,GAAG,EAAE,CAAA;;YACvB,CAAC;YAGD,oCAAkB,GAAlB;gBACI,mBAAS,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,EAAC,uBAAuB,CAAC,CAAA;gBACvD,mBAAS,CAAC,OAAO,eAAe,IAAI,UAAU,EAAC,8BAA8B,CAAC,CAAA;gBAC9E,IAAI,CAAC,aAAa,EAAE,CAAA;YACxB,CAAC;YA+CD,wBAAM,GAAN;gBACI,MAAM,CAAC,CACH,eAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,eAAK,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,aAAE,QAAQ,EAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,QAAQ,IAAI,IAAI,CAAC,aAAa,EAAE,EAAE,CAAC,CACjI,CAAA;YACL,CAAC;YACL,cAAC;QAAD,CAAC,AAlED,CAAsB,eAAK,CAAC,aAAa,GAkExC;QACD,OAAO,CAAC,YAAY,GAAG;YACnB,OAAO,EAAC,sBAAU,CAAC,UAAU;SAChC,CAAA;QAED,MAAM,CAAC,UAAC,IAAI,IAAK,OAAA;YACb,MAAM,CAAC,CACH,CAAC,OAAO,CACJ;gBAAA,CAAC,IAAI,CAAC,EAAE,IAAI,CAChB;YAAA,EAAE,OAAO,CAAC,CACb,CAAA;QACL,CAAC,EANgB,CAMhB,CAAA;IACL,CAAC;;;;;;;;;;;;;;;;iCAEc,aAAa;QAAA,CAAC"} -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // 使用 IntelliSense 了解相关属性。 3 | // 悬停以查看现有属性的描述。 4 | // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "chrome", 9 | "request": "launch", 10 | "name": "Launch Chrome", 11 | "url": "http://localhost:3000", 12 | "webRoot": "${workspaceFolder}" 13 | }, 14 | { 15 | "type": "chrome", 16 | "request": "attach", 17 | "name": "Attach to Chrome", 18 | "port": 3000, 19 | "webRoot": "${workspaceFolder}" 20 | }, 21 | { 22 | "name": "Debug Android", 23 | "program": "${workspaceRoot}/.vscode/launchReactNative.js", 24 | "type": "reactnative", 25 | "request": "launch", 26 | "platform": "android", 27 | "sourceMaps": true, 28 | "outDir": "${workspaceRoot}/.vscode/.react" 29 | }, 30 | { 31 | "name": "Debug iOS", 32 | "program": "${workspaceRoot}/.vscode/launchReactNative.js", 33 | "type": "reactnative", 34 | "request": "launch", 35 | "platform": "ios", 36 | "sourceMaps": true, 37 | "outDir": "${workspaceRoot}/.vscode/.react" 38 | }, 39 | { 40 | "name": "Attach to packager", 41 | "program": "${workspaceRoot}/.vscode/launchReactNative.js", 42 | "type": "reactnative", 43 | "request": "attach", 44 | "sourceMaps": true, 45 | "outDir": "${workspaceRoot}/.vscode/.react" 46 | }, 47 | { 48 | "name": "Debug in Exponent", 49 | "program": "${workspaceRoot}/.vscode/launchReactNative.js", 50 | "type": "reactnative", 51 | "request": "launch", 52 | "platform": "exponent", 53 | "sourceMaps": true, 54 | "outDir": "${workspaceRoot}/.vscode/.react" 55 | } 56 | ] 57 | } -------------------------------------------------------------------------------- /src/package/fastkit-dva/lib/src/Provider.js: -------------------------------------------------------------------------------- 1 | System.register(["react", "../utils/PropTypes"], function (exports_1, context_1) { 2 | "use strict"; 3 | var __extends = (this && this.__extends) || (function () { 4 | var extendStatics = Object.setPrototypeOf || 5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || 6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; 7 | return function (d, b) { 8 | extendStatics(d, b); 9 | function __() { this.constructor = d; } 10 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 11 | }; 12 | })(); 13 | var __moduleName = context_1 && context_1.id; 14 | function createProvider() { 15 | var Provider = (function (_super) { 16 | __extends(Provider, _super); 17 | function Provider(props, context) { 18 | var _this = _super.call(this, props, context) || this; 19 | _this.store = props.store; 20 | return _this; 21 | } 22 | Provider.prototype.getChildContext = function () { 23 | return { 'store': this.store }; 24 | }; 25 | Provider.prototype.render = function () { 26 | return react_1.default.Children.only(this.props.children); 27 | }; 28 | return Provider; 29 | }(react_1.default.PureComponent)); 30 | Provider.childContextTypes = { 31 | 'store': PropTypes_1.storeShape.isRequired 32 | }; 33 | return Provider; 34 | } 35 | exports_1("createProvider", createProvider); 36 | var react_1, PropTypes_1; 37 | return { 38 | setters: [ 39 | function (react_1_1) { 40 | react_1 = react_1_1; 41 | }, 42 | function (PropTypes_1_1) { 43 | PropTypes_1 = PropTypes_1_1; 44 | } 45 | ], 46 | execute: function () { 47 | exports_1("default", createProvider()); 48 | } 49 | }; 50 | }); 51 | //# sourceMappingURL=Provider.js.map -------------------------------------------------------------------------------- /src/package/fastkit-dva/lib/src/createStore.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"createStore.js","sourceRoot":"","sources":["../../src/createStore.js"],"names":[],"mappings":"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"} -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/package/fastkit-dva/src/connect.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import {storeShape} from '../utils/PropTypes' 3 | import invariant from 'invariant'; 4 | 5 | export function createConnect( 6 | modelList = [], 7 | mapStateToProps, 8 | mapDispatchToProps, 9 | mergeProps, 10 | { 11 | pure = true, 12 | withRef = false 13 | } = {} 14 | ){ 15 | class Connect extends React.PureComponent{ 16 | 17 | constructor(props,context) { 18 | super(props,context); 19 | this.store = context.store 20 | this.storeState = this.store.getState() 21 | this.timestamp = [] 22 | } 23 | 24 | 25 | componentWillMount() { 26 | invariant(modelList.length > 0,'请输入你要监听改变的model 这是必输项') 27 | invariant(typeof mapStateToProps == 'function','mapStateToProps必须为一个function') 28 | this.initSubscribe() 29 | } 30 | 31 | /** 32 | * 如果页面被销毁则清除掉原来的监听事件 减少计算 33 | * 34 | * @memberof Connect 35 | */ 36 | componentWillUnmount = () => { 37 | modelList.forEach((e)=>{ 38 | this.timestamp.forEach((el)=>{ 39 | if (this.store.callBackList[e][el]){ 40 | delete this.store.callBackList[e][el] 41 | } 42 | }) 43 | }) 44 | }; 45 | 46 | /** 47 | * 初始化监听 48 | * 49 | * @memberof Connect 50 | */ 51 | initSubscribe = () =>{ 52 | modelList.forEach((e)=>{ 53 | this.timestamp.push(Date.parse(new Date())) 54 | this.store.callBackList[e][String(this.timestamp.slice(-1)[0])] = this.onStateChange 55 | }) 56 | } 57 | 58 | /** 59 | * state发生变化点击事件 60 | * 61 | * @memberof Connect 62 | */ 63 | onStateChange = () =>{ 64 | this.forceUpdate() 65 | } 66 | 67 | /** 68 | * 添加扩展props属性 69 | * 70 | * @memberof Connect 71 | */ 72 | addExtraProps = () =>{ 73 | return mapStateToProps(this.storeState) 74 | } 75 | 76 | render(){ 77 | return ( 78 | React.Children.only(React.cloneElement(this.props.children,{dispatch:this.context['store'].dispatch,...this.addExtraProps()})) 79 | ) 80 | } 81 | } 82 | Connect.contextTypes = { 83 | 'store':storeShape.isRequired 84 | } 85 | 86 | return (View) => () => { 87 | return ( 88 | 89 | 90 | 91 | ) 92 | } 93 | } 94 | 95 | export default createConnect -------------------------------------------------------------------------------- /src/package/fastkit-dva/src/createStore.js: -------------------------------------------------------------------------------- 1 | import {runSaga} from 'redux-saga' 2 | import * as sagaEffects from 'redux-saga/effects' 3 | 4 | export default function (option) { 5 | const app = { 6 | _store:{}, 7 | _model:{}, 8 | model, 9 | dispatch, 10 | getState, 11 | subscribe, 12 | callBackList:{}, 13 | isBatchUpdate:false 14 | } 15 | return app 16 | 17 | /** 18 | * 注册model文件 19 | * 20 | * @param {any} m 21 | */ 22 | function model(m) { 23 | // 判断是否存在 只在当前不存在的时候进行添加 24 | if (!app._model[m.namespace]){ 25 | app._model[m.namespace] = m 26 | app._store[m.namespace] = m.state 27 | app.callBackList[m.namespace] = {} 28 | } 29 | } 30 | 31 | function dispatch(action) { 32 | const s = action.type.split('/') 33 | if (s.length < 1) throw new 'type参数不符合要求' 34 | const name = s[0] 35 | const fun = s[1] 36 | if (isReducers(name,fun)){ 37 | const currState = startReducers(name,fun,action) 38 | if (!app.isBatchUpdate && currState !== app._store[name]){ 39 | updateSub(name,currState) 40 | } 41 | }else { 42 | startSaga(name,fun,action) 43 | } 44 | } 45 | 46 | function startSaga(name,fun,action) { 47 | runSaga({subscribe,dispatch,getState},app._model[name].effects[fun],action,sagaEffects) 48 | } 49 | 50 | /** 51 | * 应用更新并且启动回调 52 | * 53 | * @param {any} name 54 | * @param {any} currState 55 | */ 56 | function updateSub(name,currState) { 57 | app._store[name] = currState 58 | app.isBatchUpdate = false 59 | if (app.callBackList[name]){ 60 | Object.keys(app.callBackList[name]).forEach((e)=>{ 61 | app.callBackList[name][e]() 62 | }) 63 | } 64 | } 65 | 66 | /** 67 | * 调用一个Reducers 68 | * 69 | * @param {any} name 70 | * @param {any} fun 71 | * @param {any} action 72 | */ 73 | function startReducers(name,fun,action) { 74 | const state = app._store[name] 75 | return app._model[name].reducers[fun](state,action) 76 | } 77 | 78 | function isReducers(name,fun) { 79 | return Object.keys(app._model[name].reducers).some((e)=> e == fun) 80 | } 81 | /** 82 | * 返回当前数据源 83 | * 84 | * @returns 85 | */ 86 | function getState() { 87 | return app._store 88 | } 89 | 90 | /** 91 | * 监听 92 | * 93 | * @param {any} params 94 | */ 95 | function subscribe(params) { 96 | return function unsubscribe(){ 97 | 98 | } 99 | } 100 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 初始化部分 2 | 3 | > 安装 4 | 5 | > npm i 6 | 7 | > 调试 8 | 9 | > npm run start 10 | 11 | 12 | 13 | import {createStore,Provider} from './package/fastkit-dva' 14 | import appModels from './models/app' 15 | const app = createStore({}) 16 | app.model(appModels) 17 | const AppView = () => { 18 | return ( 19 | 20 | 21 | 22 | ) 23 | } 24 | 25 | 这里是初始化的部分代码 之后会与dva尽可能的保持一致 在后续版本会进行修改 26 | 27 | ##connect部分 28 | 29 | 30 | export default connect(['app'],({app})=>({...app}))(App) 31 | 32 | 33 | 这里其余字段都与react-redux保持一致 但是首字段加了一个modelList 34 | 这个字段的作用就是为了能够更好的进行监听 比如上面的app 35 | 也就是说 我只会监听app这个model中的state发生的变化 其他的reduce变化 不会触发这个页面的监听回调 36 | 37 | ##connect初始化监听部分 38 | 39 | 40 | /** 41 | * 初始化监听 42 | * 43 | * @memberof Connect 44 | */ 45 | initSubscribe = () =>{ 46 | modelList.forEach((e)=>{ 47 | this.timestamp.push(Date.parse(new Date())) 48 | this.store.callBackList[e][String(this.timestamp.slice(-1)[0])] = this.onStateChange 49 | }) 50 | } 51 | 52 | 在这里会直接对指定modelName去添加一个callBack 并且名字使用时间戳的方式来实现 避免出现重复命名 53 | 这个时间戳将会在组件被销毁的时候被使用到 54 | 55 | ##connect组件销毁 56 | 57 | 58 | /** 59 | * 如果页面被销毁则清除掉原来的监听事件 减少计算 60 | * 61 | * @memberof Connect 62 | */ 63 | componentWillUnmount = () => { 64 | modelList.forEach((e)=>{ 65 | this.timestamp.forEach((el)=>{ 66 | if (this.store.callBackList[e][el]){ 67 | delete this.store.callBackList[e][el] 68 | } 69 | }) 70 | }) 71 | }; 72 | 73 | 当组件被销毁的时候 会从当前modelName的callBack回调中去删除属于当前页面的回调 避免下次在触发dispatch的时候 重新参与运算 74 | 75 | 76 | ##model部分 77 | 78 | 79 | import test from '../utils/test' 80 | export default { 81 | namespace : 'app', 82 | state : { 83 | num: 0 84 | }, 85 | reducers : { 86 | add(state, {payload}) { 87 | return ({...state,num:state.num + 1}) 88 | }, 89 | reduce(state,{payload}){ 90 | return ({...state,num:state.num - 1}) 91 | }, 92 | reset(state,{payload}){ 93 | return ({...state,num:0}) 94 | } 95 | }, 96 | effects : { 97 | *clear({payload},{select,put,take,call}){ 98 | yield call(test,11111) 99 | } 100 | } 101 | } 102 | 103 | 这里的使用方式跟dva保持了一致 因为是直接使用的saga来进行的实现 所以saga的一些功能与属性都可以直接的进行使用 不会有影响 104 | 105 | 现在这个项目还不够成熟 目前的这个版本删除了中间件 下一个版本将会用redux中间件的方式去将dispatch进行合并 但是只会支持saga与router这两个 尽可能去减少项目中不必要的reduce从来提升性能吧 真的是有得必有失啊 106 | -------------------------------------------------------------------------------- /src/package/fastkit-dva/lib/src/createStore.js: -------------------------------------------------------------------------------- 1 | System.register(["redux-saga", "redux-saga/effects"], function (exports_1, context_1) { 2 | "use strict"; 3 | var __moduleName = context_1 && context_1.id; 4 | function default_1(option) { 5 | var app = { 6 | _store: {}, 7 | _model: {}, 8 | model: model, 9 | dispatch: dispatch, 10 | getState: getState, 11 | subscribe: subscribe, 12 | callBackList: {}, 13 | isBatchUpdate: false 14 | }; 15 | return app; 16 | function model(m) { 17 | if (!app._model[m.namespace]) { 18 | app._model[m.namespace] = m; 19 | app._store[m.namespace] = m.state; 20 | app.callBackList[m.namespace] = {}; 21 | } 22 | } 23 | function dispatch(action) { 24 | var s = action.type.split('/'); 25 | if (s.length < 1) 26 | throw new 'type参数不符合要求'; 27 | var name = s[0]; 28 | var fun = s[1]; 29 | if (isReducers(name, fun)) { 30 | var currState = startReducers(name, fun, action); 31 | if (!app.isBatchUpdate && currState !== app._store[name]) { 32 | updateSub(name, currState); 33 | } 34 | } 35 | else { 36 | startSaga(name, fun, action); 37 | } 38 | } 39 | function startSaga(name, fun, action) { 40 | redux_saga_1.runSaga({ subscribe: subscribe, dispatch: dispatch, getState: getState }, app._model[name].effects[fun], action, sagaEffects); 41 | } 42 | function updateSub(name, currState) { 43 | app._store[name] = currState; 44 | app.isBatchUpdate = false; 45 | if (app.callBackList[name]) { 46 | Object.keys(app.callBackList[name]).forEach(function (e) { 47 | app.callBackList[name][e](); 48 | }); 49 | } 50 | } 51 | function startReducers(name, fun, action) { 52 | var state = app._store[name]; 53 | return app._model[name].reducers[fun](state, action); 54 | } 55 | function isReducers(name, fun) { 56 | return Object.keys(app._model[name].reducers).some(function (e) { return e == fun; }); 57 | } 58 | function getState() { 59 | return app._store; 60 | } 61 | function subscribe(params) { 62 | return function unsubscribe() { 63 | }; 64 | } 65 | } 66 | exports_1("default", default_1); 67 | var redux_saga_1, sagaEffects; 68 | return { 69 | setters: [ 70 | function (redux_saga_1_1) { 71 | redux_saga_1 = redux_saga_1_1; 72 | }, 73 | function (sagaEffects_1) { 74 | sagaEffects = sagaEffects_1; 75 | } 76 | ], 77 | execute: function () { 78 | } 79 | }; 80 | }); 81 | //# sourceMappingURL=createStore.js.map -------------------------------------------------------------------------------- /src/package/fastkit-dva/lib/src/connect.js: -------------------------------------------------------------------------------- 1 | System.register(["react", "../utils/PropTypes", "invariant"], function (exports_1, context_1) { 2 | "use strict"; 3 | var __extends = (this && this.__extends) || (function () { 4 | var extendStatics = Object.setPrototypeOf || 5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || 6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; 7 | return function (d, b) { 8 | extendStatics(d, b); 9 | function __() { this.constructor = d; } 10 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 11 | }; 12 | })(); 13 | var __assign = (this && this.__assign) || Object.assign || function(t) { 14 | for (var s, i = 1, n = arguments.length; i < n; i++) { 15 | s = arguments[i]; 16 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) 17 | t[p] = s[p]; 18 | } 19 | return t; 20 | }; 21 | var __moduleName = context_1 && context_1.id; 22 | function createConnect(modelList, mapStateToProps, mapDispatchToProps, mergeProps, _a) { 23 | if (modelList === void 0) { modelList = []; } 24 | var _b = _a === void 0 ? {} : _a, _c = _b.pure, pure = _c === void 0 ? true : _c, _d = _b.withRef, withRef = _d === void 0 ? false : _d; 25 | var Connect = (function (_super) { 26 | __extends(Connect, _super); 27 | function Connect(props, context) { 28 | var _this = _super.call(this, props, context) || this; 29 | _this.componentWillUnmount = function () { 30 | modelList.forEach(function (e) { 31 | _this.timestamp.forEach(function (el) { 32 | if (_this.store.callBackList[e][el]) { 33 | delete _this.store.callBackList[e][el]; 34 | } 35 | }); 36 | }); 37 | }; 38 | _this.initSubscribe = function () { 39 | modelList.forEach(function (e) { 40 | _this.timestamp.push(Date.parse(new Date())); 41 | _this.store.callBackList[e][String(_this.timestamp.slice(-1)[0])] = _this.onStateChange; 42 | }); 43 | }; 44 | _this.onStateChange = function () { 45 | _this.forceUpdate(); 46 | }; 47 | _this.addExtraProps = function () { 48 | return mapStateToProps(_this.storeState); 49 | }; 50 | _this.store = context.store; 51 | _this.storeState = _this.store.getState(); 52 | _this.timestamp = []; 53 | return _this; 54 | } 55 | Connect.prototype.componentWillMount = function () { 56 | invariant_1.default(modelList.length > 0, '请输入你要监听改变的model 这是必输项'); 57 | invariant_1.default(typeof mapStateToProps == 'function', 'mapStateToProps必须为一个function'); 58 | this.initSubscribe(); 59 | }; 60 | Connect.prototype.render = function () { 61 | return (react_1.default.Children.only(react_1.default.cloneElement(this.props.children, __assign({ dispatch: this.context['store'].dispatch }, this.addExtraProps())))); 62 | }; 63 | return Connect; 64 | }(react_1.default.PureComponent)); 65 | Connect.contextTypes = { 66 | 'store': PropTypes_1.storeShape.isRequired 67 | }; 68 | return function (View) { return function () { 69 | return ( 70 | 71 | ); 72 | }; }; 73 | } 74 | exports_1("createConnect", createConnect); 75 | var react_1, PropTypes_1, invariant_1; 76 | return { 77 | setters: [ 78 | function (react_1_1) { 79 | react_1 = react_1_1; 80 | }, 81 | function (PropTypes_1_1) { 82 | PropTypes_1 = PropTypes_1_1; 83 | }, 84 | function (invariant_1_1) { 85 | invariant_1 = invariant_1_1; 86 | } 87 | ], 88 | execute: function () { 89 | exports_1("default", createConnect); 90 | } 91 | }; 92 | }); 93 | //# sourceMappingURL=connect.js.map -------------------------------------------------------------------------------- /src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | 39 | // Add some additional logging to localhost, pointing developers to the 40 | // service worker/PWA documentation. 41 | navigator.serviceWorker.ready.then(() => { 42 | console.log( 43 | 'This web app is being served cache-first by a service ' + 44 | 'worker. To learn more, visit https://goo.gl/SC7cgQ' 45 | ); 46 | }); 47 | } else { 48 | // Is not local host. Just register service worker 49 | registerValidSW(swUrl); 50 | } 51 | }); 52 | } 53 | } 54 | 55 | function registerValidSW(swUrl) { 56 | navigator.serviceWorker 57 | .register(swUrl) 58 | .then(registration => { 59 | registration.onupdatefound = () => { 60 | const installingWorker = registration.installing; 61 | installingWorker.onstatechange = () => { 62 | if (installingWorker.state === 'installed') { 63 | if (navigator.serviceWorker.controller) { 64 | // At this point, the old content will have been purged and 65 | // the fresh content will have been added to the cache. 66 | // It's the perfect time to display a "New content is 67 | // available; please refresh." message in your web app. 68 | console.log('New content is available; please refresh.'); 69 | } else { 70 | // At this point, everything has been precached. 71 | // It's the perfect time to display a 72 | // "Content is cached for offline use." message. 73 | console.log('Content is cached for offline use.'); 74 | } 75 | } 76 | }; 77 | }; 78 | }) 79 | .catch(error => { 80 | console.error('Error during service worker registration:', error); 81 | }); 82 | } 83 | 84 | function checkValidServiceWorker(swUrl) { 85 | // Check if the service worker can be found. If it can't reload the page. 86 | fetch(swUrl) 87 | .then(response => { 88 | // Ensure service worker exists, and that we really are getting a JS file. 89 | if ( 90 | response.status === 404 || 91 | response.headers.get('content-type').indexOf('javascript') === -1 92 | ) { 93 | // No service worker found. Probably a different app. Reload the page. 94 | navigator.serviceWorker.ready.then(registration => { 95 | registration.unregister().then(() => { 96 | window.location.reload(); 97 | }); 98 | }); 99 | } else { 100 | // Service worker found. Proceed as normal. 101 | registerValidSW(swUrl); 102 | } 103 | }) 104 | .catch(() => { 105 | console.log( 106 | 'No internet connection found. App is running in offline mode.' 107 | ); 108 | }); 109 | } 110 | 111 | export function unregister() { 112 | if ('serviceWorker' in navigator) { 113 | navigator.serviceWorker.ready.then(registration => { 114 | registration.unregister(); 115 | }); 116 | } 117 | } 118 | --------------------------------------------------------------------------------