diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..44aeb40 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules\\typescript\\lib" +} diff --git a/LICENSE b/LICENSE index 527f834..39a6d18 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 CoderHXL +Copyright (c) CoderHXL Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/package.json b/package.json index fa67373..8e22130 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xl-store", - "version": "0.1.9", + "version": "0.1.10", "description": "状态管理仓库", "scripts": { "build-esm": "node ./script/build.js esm", @@ -12,7 +12,7 @@ "author": "coderhxl", "license": "MIT", "devDependencies": { - "@types/node": "^18.7.18", + "@types/node": "^18.11.9", "esbuild": "^0.15.8", "prettier": "^2.8.0", "typescript": "^4.9.3" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 29e3f6e..28f9dd7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,13 +1,13 @@ lockfileVersion: 5.4 specifiers: - '@types/node': ^18.7.18 + '@types/node': ^18.11.9 esbuild: ^0.15.8 prettier: ^2.8.0 typescript: ^4.9.3 devDependencies: - '@types/node': 18.7.18 + '@types/node': 18.11.9 esbuild: 0.15.8 prettier: 2.8.0 typescript: 4.9.3 @@ -34,8 +34,8 @@ packages: dev: true optional: true - /@types/node/18.7.18: - resolution: {integrity: sha512-m+6nTEOadJZuTPkKR/SYK3A2d7FZrgElol9UP1Kae90VVU4a6mxnPuLiIW1m4Cq4gZ/nWb9GrdVXJCoCazDAbg==} + /@types/node/18.11.9: + resolution: {integrity: sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==} dev: true /esbuild-android-64/0.15.8: diff --git a/publish/LICENSE b/publish/LICENSE index 527f834..39a6d18 100644 --- a/publish/LICENSE +++ b/publish/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 CoderHXL +Copyright (c) CoderHXL Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/publish/lib/index.js b/publish/lib/index.js index 1311b46..92524b7 100644 --- a/publish/lib/index.js +++ b/publish/lib/index.js @@ -1 +1 @@ -"use strict";function O(t){for(let r in t)if(typeof t[r]!="function")throw new Error("actions \u91CC\u53EA\u80FD\u653E\u51FD\u6570")}function j(t){if(t===null||typeof t!="object")throw new Error("state \u5FC5\u987B\u662F\u5BF9\u8C61")}function l(t){let{state:r={},actions:n={}}=t;j(r),O(n)}function f(t,r=!1){function n(s,o){if(r){let i=t.state[s];o(s,i)}let e=t.trackStore,c=e[s];c||(c=e[s]=new Set),c.add(o)}return(s,o)=>{if(Array.isArray(s))for(let e of s)n(e,o);else n(s,o)}}function d({trackStore:t}){function r(n,s){let o=t[n];!o||o.delete(s)}return(n,s)=>{if(Array.isArray(n))for(let o of n)r(o,s);else r(n,s)}}function P(t,r){let{trackStore:n}=t,s=t.state[r],o=n[r];if(!!o)for(let e of o)e(r,s)}var x=!1,I=null;function m(t){let{state:r,actions:n,storeApi:s}=t;return new Proxy(t,{get(o,e){if(e in t)return t[e];if(e in s)return s[e];if(e in r)return r[e];if(e in n)return n[e];throw new Error(`\u4E0D\u80FD\u83B7\u53D6 ${e}`)},set(o,e,c){if(e in t||e in s)throw new Error(`${e} \u662F Store \u81EA\u5E26\u7684\u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539`);if(e in r)return r[e]=c,!0;throw e in n?new Error(`${e} \u662F actions , \u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539`):new Error(`${e} \u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539\u6216\u6DFB\u52A0`)}})}function a(t,r,n=null){let{isDeepWatch:s}=t.options;return new Proxy(r,{set(o,e,c){if(o[e]===c)return!0;if(s){if(x)return o[e]=c,!0;typeof c=="object"&&c!==null?(I=n!=null?n:e,o[e]=y(t,c),I=null):o[e]=c}else o[e]=c;return P(t,n!=null?n:e),!0}})}function y(t,r,n=!1){let s=Array.isArray(r)?[]:{};function o(e,c,i=!1){for(let S in e){let A=e[S];if(i&&(I=S),typeof A=="object"&&A!==null){let u=Array.isArray(A)?[]:{};o(A,u),c[S]=a(t,u,I)}else c[S]=A;i&&(I=null)}}return x=!0,o(r,s,n),x=!1,a(t,s,n?null:I)}var g=0;function w(t,r){let{isDeepWatch:n}=t.options,s=n?y(t,r,!0):a(t,r);t.state=s}function b(t){let r={watch:f(t),watchEffect:f(t,!0),deleteWatch:d(t)};t.storeApi=r}function R(t,r={}){let{state:n={},actions:s={}}=t,o={id:g++,trackStore:{},storeApi:{},state:{},actions:s,options:r};return b(o),w(o,n),o}module.exports=function(r,n){l(r);let s=R(r,n);return m(s)}; +"use strict";function p(t){for(let r in t)if(typeof t[r]!="function")throw new Error("actions \u91CC\u53EA\u80FD\u653E\u51FD\u6570")}function O(t){if(t===null||typeof t!="object")throw new Error("state \u5FC5\u987B\u662F\u5BF9\u8C61")}function l(t){let{state:r={},actions:e={}}=t;O(r),p(e)}function f(t,r=!1){function e(s,o){if(r){let i=t.state[s];o(s,i)}let n=t.trackStore,c=n[s];c||(c=n[s]=new Set),c.add(o)}return(s,o)=>{if(Array.isArray(s))for(let n of s)e(n,o);else e(s,o)}}function d({trackStore:t}){function r(e,s){let o=t[e];!o||o.delete(s)}return(e,s)=>{if(Array.isArray(e))for(let o of e)r(o,s);else r(e,s)}}function P(t,r){let{trackStore:e}=t,s=t.state[r],o=e[r];if(!!o)for(let n of o)n(r,s)}var x=!1,I=null;function m(t){let{state:r,actions:e,storeApi:s}=t;return new Proxy(t,{get(o,n){return n in s?s[n]:n in r?r[n]:n in e?e[n]:void 0},set(o,n,c){if(n in s)throw new Error(`${n} \u662F Store \u81EA\u5E26\u7684\u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539`);if(n in r)return r[n]=c,!0;throw n in e?new Error(`${n} \u662F actions , \u4E0D\u5141\u8BB8\u5728\u6B64\u88AB\u4FEE\u6539`):new Error(`${n} \u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539\u6216\u6DFB\u52A0`)}})}function a(t,r,e=null){let{isDeepWatch:s}=t.options;return new Proxy(r,{set(o,n,c){if(o[n]===c)return!0;if(s){if(x)return o[n]=c,!0;typeof c=="object"&&c!==null?(I=e!=null?e:n,o[n]=y(t,c),I=null):o[n]=c}else o[n]=c;return P(t,e!=null?e:n),!0}})}function y(t,r,e=!1){let s=Array.isArray(r)?[]:{};function o(n,c,i=!1){for(let S in n){let A=n[S];if(i&&(I=S),typeof A=="object"&&A!==null){let u=Array.isArray(A)?[]:{};o(A,u),c[S]=a(t,u,I)}else c[S]=A;i&&(I=null)}}return x=!0,o(r,s,e),x=!1,a(t,s,e?null:I)}var g=0;function j(t,r){let{isDeepWatch:e}=t.options,s=e?y(t,r,!0):a(t,r);t.state=s}function w(t){let r={watch:f(t),watchEffect:f(t,!0),deleteWatch:d(t)};t.storeApi=r}function R(t,r={}){let{state:e={},actions:s={}}=t,o={id:g++,trackStore:{},storeApi:{},state:{},actions:s,options:r};return w(o),j(o,e),o}function b(t,r){l(t);let e=R(t,r);return m(e)}module.exports=b; diff --git a/publish/lib/index.mjs b/publish/lib/index.mjs index d056714..e0ccfd0 100644 --- a/publish/lib/index.mjs +++ b/publish/lib/index.mjs @@ -1 +1 @@ -function O(t){for(let r in t)if(typeof t[r]!="function")throw new Error("actions \u91CC\u53EA\u80FD\u653E\u51FD\u6570")}function j(t){if(t===null||typeof t!="object")throw new Error("state \u5FC5\u987B\u662F\u5BF9\u8C61")}function l(t){let{state:r={},actions:n={}}=t;j(r),O(n)}function f(t,r=!1){function n(s,o){if(r){let i=t.state[s];o(s,i)}let e=t.trackStore,c=e[s];c||(c=e[s]=new Set),c.add(o)}return(s,o)=>{if(Array.isArray(s))for(let e of s)n(e,o);else n(s,o)}}function d({trackStore:t}){function r(n,s){let o=t[n];!o||o.delete(s)}return(n,s)=>{if(Array.isArray(n))for(let o of n)r(o,s);else r(n,s)}}function P(t,r){let{trackStore:n}=t,s=t.state[r],o=n[r];if(!!o)for(let e of o)e(r,s)}var x=!1,I=null;function m(t){let{state:r,actions:n,storeApi:s}=t;return new Proxy(t,{get(o,e){if(e in t)return t[e];if(e in s)return s[e];if(e in r)return r[e];if(e in n)return n[e];throw new Error(`\u4E0D\u80FD\u83B7\u53D6 ${e}`)},set(o,e,c){if(e in t||e in s)throw new Error(`${e} \u662F Store \u81EA\u5E26\u7684\u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539`);if(e in r)return r[e]=c,!0;throw e in n?new Error(`${e} \u662F actions , \u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539`):new Error(`${e} \u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539\u6216\u6DFB\u52A0`)}})}function a(t,r,n=null){let{isDeepWatch:s}=t.options;return new Proxy(r,{set(o,e,c){if(o[e]===c)return!0;if(s){if(x)return o[e]=c,!0;typeof c=="object"&&c!==null?(I=n!=null?n:e,o[e]=y(t,c),I=null):o[e]=c}else o[e]=c;return P(t,n!=null?n:e),!0}})}function y(t,r,n=!1){let s=Array.isArray(r)?[]:{};function o(e,c,i=!1){for(let S in e){let A=e[S];if(i&&(I=S),typeof A=="object"&&A!==null){let u=Array.isArray(A)?[]:{};o(A,u),c[S]=a(t,u,I)}else c[S]=A;i&&(I=null)}}return x=!0,o(r,s,n),x=!1,a(t,s,n?null:I)}var g=0;function w(t,r){let{isDeepWatch:n}=t.options,s=n?y(t,r,!0):a(t,r);t.state=s}function b(t){let r={watch:f(t),watchEffect:f(t,!0),deleteWatch:d(t)};t.storeApi=r}function R(t,r={}){let{state:n={},actions:s={}}=t,o={id:g++,trackStore:{},storeApi:{},state:{},actions:s,options:r};return b(o),w(o,n),o}function h(t,r){l(t);let n=R(t,r);return m(n)}export{h as default}; +var a=(t,e)=>()=>(t&&(e=t(t=0)),e);var h=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);function k(t){for(let e in t)if(typeof t[e]!="function")throw new Error("actions \u91CC\u53EA\u80FD\u653E\u51FD\u6570")}function E(t){if(t===null||typeof t!="object")throw new Error("state \u5FC5\u987B\u662F\u5BF9\u8C61")}function m(t){let{state:e={},actions:n={}}=t;E(e),k(n)}var R=a(()=>{"use strict"});function x(t,e=!1){function n(s,o){if(e){let i=t.state[s];o(s,i)}let r=t.trackStore,c=r[s];c||(c=r[s]=new Set),c.add(o)}return(s,o)=>{if(Array.isArray(s))for(let r of s)n(r,o);else n(s,o)}}function p({trackStore:t}){function e(n,s){let o=t[n];!o||o.delete(s)}return(n,s)=>{if(Array.isArray(n))for(let o of n)e(o,s);else e(n,s)}}function O(t,e){let{trackStore:n}=t,s=t.state[e],o=n[e];if(!!o)for(let r of o)r(e,s)}var y=a(()=>{"use strict"});function g(t){let{state:e,actions:n,storeApi:s}=t;return new Proxy(t,{get(o,r){return r in s?s[r]:r in e?e[r]:r in n?n[r]:void 0},set(o,r,c){if(r in s)throw new Error(`${r} \u662F Store \u81EA\u5E26\u7684\u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539`);if(r in e)return e[r]=c,!0;throw r in n?new Error(`${r} \u662F actions , \u4E0D\u5141\u8BB8\u5728\u6B64\u88AB\u4FEE\u6539`):new Error(`${r} \u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539\u6216\u6DFB\u52A0`)}})}function f(t,e,n=null){let{isDeepWatch:s}=t.options;return new Proxy(e,{set(o,r,c){if(o[r]===c)return!0;if(s){if(u)return o[r]=c,!0;typeof c=="object"&&c!==null?(I=n!=null?n:r,o[r]=l(t,c),I=null):o[r]=c}else o[r]=c;return O(t,n!=null?n:r),!0}})}function l(t,e,n=!1){let s=Array.isArray(e)?[]:{};function o(r,c,i=!1){for(let S in r){let A=r[S];if(i&&(I=S),typeof A=="object"&&A!==null){let P=Array.isArray(A)?[]:{};o(A,P),c[S]=f(t,P,I)}else c[S]=A;i&&(I=null)}}return u=!0,o(e,s,n),u=!1,f(t,s,n?null:I)}var u,I,d=a(()=>{"use strict";y();u=!1,I=null});function v(t,e){let{isDeepWatch:n}=t.options,s=n?l(t,e,!0):f(t,e);t.state=s}function D(t){let e={watch:x(t),watchEffect:x(t,!0),deleteWatch:p(t)};t.storeApi=e}function j(t,e={}){let{state:n={},actions:s={}}=t,o={id:F++,trackStore:{},storeApi:{},state:{},actions:s,options:e};return D(o),v(o,n),o}var F,w=a(()=>{"use strict";y();d();F=0});var $=h((N,b)=>{R();w();d();function W(t,e){m(t);let n=j(t,e);return g(n)}b.exports=W});export default $(); diff --git a/publish/package.json b/publish/package.json index 1356daa..9b8f9ec 100644 --- a/publish/package.json +++ b/publish/package.json @@ -1,6 +1,6 @@ { "name": "xl-store", - "version": "0.1.9", + "version": "0.1.10", "description": "状态管理仓库", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/script/test.js b/script/test.js index d252c93..50781c8 100644 --- a/script/test.js +++ b/script/test.js @@ -6,7 +6,10 @@ require('esbuild') platform: 'neutral', minify: true, outdir: './test', - watch: true + watch: true, + loader: { + '.ts': 'ts' + } }) .then((res) => { console.log('watching...') diff --git a/test/build-test.js b/test/build-test.js index bd4f3bf..960fe8c 100644 --- a/test/build-test.js +++ b/test/build-test.js @@ -4,25 +4,18 @@ const xlStore = require('../publish/lib/index') const myStore = xlStore( { state: { + count: 0, userInfo: { name: 'hxl', age: 18 - }, - books: [ - { id: 100, name: 'JS高级程序设计', price: 88 }, - { id: 101, name: 'Vue高级程序设计', price: 99 }, - { id: 102, name: 'React高级程序设计', price: 87 } - ] + } }, actions: { - changeUserInfoAction() { - this.userInfo = { - name: 'coderhxl', - age: 18 - } + incrementAction() { + this.count += 1 }, - changeBookPriceAction(index, price) { - this.books[index].price = price + changeUserInfoAction(newUserInfo) { + this.userInfo = newUserInfo } } }, @@ -31,21 +24,19 @@ const myStore = xlStore( } ) -function getUserInfo(key, value) { - console.log('getUserInfo', key, value) +function getCount(key, value) { + console.log('getCount', key, value) } -function getBooks(key, value) { - console.log('getBooks', key, value) +function getUserInfo(key, value) { + console.log('getUserInfo', key, value) } -console.log(myStore.userInfo) -console.log(myStore.books) - -myStore.watch('userInfo', getUserInfo) -myStore.watch('books', getBooks) +myStore.watchEffect('count', getCount) +myStore.watchEffect('userInfo', getUserInfo) console.log('------------------------------') -myStore.changeUserInfoAction() -myStore.changeBookPriceAction(0, 66) +myStore.incrementAction() +myStore.incrementAction() +myStore.changeUserInfoAction({ name: 'coderhxl', age: 18 }) diff --git a/test/index.js b/test/index.js index cf337a4..a359430 100644 --- a/test/index.js +++ b/test/index.js @@ -1 +1 @@ -function b(e){for(let t in e)if(typeof e[t]!="function")throw new Error("actions \u91CC\u53EA\u80FD\u653E\u51FD\u6570")}function w(e){if(e===null||typeof e!="object")throw new Error("state \u5FC5\u987B\u662F\u5BF9\u8C61")}function m(e){let{state:t={},actions:r={}}=e;w(t),b(r)}function y(e,t=!1){function r(o,s){if(t){let A=e.state[o];s(o,A)}let n=e.trackStore,c=n[o];c||(c=n[o]=new Set),c.add(s)}return(o,s)=>{if(Array.isArray(o))for(let n of o)r(n,s);else r(o,s)}}function g({trackStore:e}){function t(r,o){let s=e[r];!s||s.delete(o)}return(r,o)=>{if(Array.isArray(r))for(let s of r)t(s,o);else t(r,o)}}function P(e,t){let{trackStore:r}=e,o=e.state[t],s=r[t];if(!!s)for(let n of s)n(t,o)}var x=!1,i=null;function h(e){let{state:t,actions:r,storeApi:o}=e;return new Proxy(e,{get(s,n){if(n in e)return e[n];if(n in o)return o[n];if(n in t)return t[n];if(n in r)return r[n];throw new Error(`\u4E0D\u80FD\u83B7\u53D6 ${n}`)},set(s,n,c){if(n in e||n in o)throw new Error(`${n} \u662F Store \u81EA\u5E26\u7684\u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539`);if(n in t)return t[n]=c,!0;throw n in r?new Error(`${n} \u662F actions , \u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539`):new Error(`${n} \u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539\u6216\u6DFB\u52A0`)}})}function S(e,t,r=null){let{isDeepWatch:o}=e.options;return new Proxy(t,{set(s,n,c){if(s[n]===c)return!0;if(o){if(x)return s[n]=c,!0;typeof c=="object"&&c!==null?(i=r??n,s[n]=u(e,c),i=null):s[n]=c}else s[n]=c;return P(e,r??n),!0}})}function u(e,t,r=!1){let o=Array.isArray(t)?[]:{};function s(n,c,A=!1){for(let a in n){let I=n[a];if(A&&(i=a),typeof I=="object"&&I!==null){let d=Array.isArray(I)?[]:{};s(I,d),c[a]=S(e,d,i)}else c[a]=I;A&&(i=null)}}return x=!0,s(t,o,r),x=!1,S(e,o,r?null:i)}var O=0;function j(e,t){let{isDeepWatch:r}=e.options,o=r?u(e,t,!0):S(e,t);e.state=o}function k(e){let t={watch:y(e),watchEffect:y(e,!0),deleteWatch:g(e)};e.storeApi=t}function R(e,t={}){let{state:r={},actions:o={}}=e,s={id:O++,trackStore:{},storeApi:{},state:{},actions:o,options:t};return k(s),j(s,r),s}function l(e,t){m(e);let r=R(e,t);return h(r)}var f=l({state:{userInfo:{name:"hxl",age:18},books:[{id:100,name:"JS\u9AD8\u7EA7\u7A0B\u5E8F\u8BBE\u8BA1",price:88},{id:101,name:"Vue\u9AD8\u7EA7\u7A0B\u5E8F\u8BBE\u8BA1",price:99},{id:102,name:"React\u9AD8\u7EA7\u7A0B\u5E8F\u8BBE\u8BA1",price:87}]},actions:{changeUserInfoAction(e){this.userInfo=e},changeBookPriceAction(e,t){this.books[e].price=t}}},{isDeepWatch:!0});function p(e,t){console.log("getUserInfo",e,t)}function E(e,t){console.log("getBooks",e,t)}f.watchEffect("userInfo",p);f.watchEffect("books",E);console.log("------------------------------");f.changeUserInfoAction({name:"coderhxl",age:18});f.changeBookPriceAction(0,66); +var D=Object.create;var P=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,K=Object.prototype.hasOwnProperty;var f=(t,e)=>()=>(t&&(e=t(t=0)),e);var $=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var C=(t,e,n,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of W(e))!K.call(t,r)&&r!==n&&P(t,r,{get:()=>e[r],enumerable:!(s=F(e,r))||s.enumerable});return t};var T=(t,e,n)=>(n=t!=null?D(v(t)):{},C(e||!t||!t.__esModule?P(n,"default",{value:t,enumerable:!0}):n,t));function _(t){for(let e in t)if(typeof t[e]!="function")throw new Error("actions \u91CC\u53EA\u80FD\u653E\u51FD\u6570")}function q(t){if(t===null||typeof t!="object")throw new Error("state \u5FC5\u987B\u662F\u5BF9\u8C61")}function R(t){let{state:e={},actions:n={}}=t;q(e),_(n)}var h=f(()=>{"use strict"});function x(t,e=!1){function n(s,r){if(e){let a=t.state[s];r(s,a)}let o=t.trackStore,c=o[s];c||(c=o[s]=new Set),c.add(r)}return(s,r)=>{if(Array.isArray(s))for(let o of s)n(o,r);else n(s,r)}}function p({trackStore:t}){function e(n,s){let r=t[n];!r||r.delete(s)}return(n,s)=>{if(Array.isArray(n))for(let r of n)e(r,s);else e(n,s)}}function O(t,e){let{trackStore:n}=t,s=t.state[e],r=n[e];if(!!r)for(let o of r)o(e,s)}var u=f(()=>{"use strict"});function j(t){let{state:e,actions:n,storeApi:s}=t;return new Proxy(t,{get(r,o){return o in s?s[o]:o in e?e[o]:o in n?n[o]:void 0},set(r,o,c){if(o in s)throw new Error(`${o} \u662F Store \u81EA\u5E26\u7684\u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539`);if(o in e)return e[o]=c,!0;throw o in n?new Error(`${o} \u662F actions , \u4E0D\u5141\u8BB8\u5728\u6B64\u88AB\u4FEE\u6539`):new Error(`${o} \u4E0D\u5141\u8BB8\u88AB\u4FEE\u6539\u6216\u6DFB\u52A0`)}})}function y(t,e,n=null){let{isDeepWatch:s}=t.options;return new Proxy(e,{set(r,o,c){if(r[o]===c)return!0;if(s){if(l)return r[o]=c,!0;typeof c=="object"&&c!==null?(i=n??o,r[o]=d(t,c),i=null):r[o]=c}else r[o]=c;return O(t,n??o),!0}})}function d(t,e,n=!1){let s=Array.isArray(e)?[]:{};function r(o,c,a=!1){for(let S in o){let I=o[S];if(a&&(i=S),typeof I=="object"&&I!==null){let g=Array.isArray(I)?[]:{};r(I,g),c[S]=y(t,g,i)}else c[S]=I;a&&(i=null)}}return l=!0,r(e,s,n),l=!1,y(t,s,n?null:i)}var l,i,m=f(()=>{"use strict";u();l=!1,i=null});function B(t,e){let{isDeepWatch:n}=t.options,s=n?d(t,e,!0):y(t,e);t.state=s}function G(t){let e={watch:x(t),watchEffect:x(t,!0),deleteWatch:p(t)};t.storeApi=e}function w(t,e={}){let{state:n={},actions:s={}}=t,r={id:z++,trackStore:{},storeApi:{},state:{},actions:s,options:e};return G(r),B(r,n),r}var z,b=f(()=>{"use strict";u();m();z=0});var E=$((st,k)=>{"use strict";h();b();m();function H(t,e){R(t);let n=w(t,e);return j(n)}k.exports=H});var U=T(E()),A=(0,U.default)({state:{count:0,userInfo:{name:"hxl",age:18}},actions:{incrementAction(){this.count+=1},changeUserInfoAction(t){this.userInfo.name=t.name}}},{isDeepWatch:!0});function J(t,e){console.log("getCount",t,e)}function L(t,e){console.log("getUserInfo",t,e)}A.watchEffect("count",J);A.watchEffect("userInfo",L);console.log("------------------------------");A.incrementAction();A.incrementAction();A.changeUserInfoAction({name:"coderhxl",age:18}); diff --git a/test/index.ts b/test/index.ts index 6845a9f..a68a59d 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,24 +1,26 @@ import xlStore from '../src/index' +interface IUserInfo { + name: string + age: number +} + const myStore = xlStore( { state: { + count: 0, userInfo: { name: 'hxl', age: 18 - }, - books: [ - { id: 100, name: 'JS高级程序设计', price: 88 }, - { id: 101, name: 'Vue高级程序设计', price: 99 }, - { id: 102, name: 'React高级程序设计', price: 87 } - ] + } }, actions: { - changeUserInfoAction(newInfo: { name: string; age: number }) { - this.userInfo = newInfo + incrementAction() { + this.count += 1 }, - changeBookPriceAction(index: number, price: number) { - this.books[index].price = price + changeUserInfoAction(newUserInfo: IUserInfo) { + this.userInfo.name = newUserInfo.name + // this.userInfo = newUserInfo } } }, @@ -27,19 +29,20 @@ const myStore = xlStore( } ) -function getUserInfo(key: string, value: any) { - console.log('getUserInfo', key, value) +function getCount(key: string, value: number) { + console.log('getCount', key, value) } -function getBooks(key: string, value: any) { - console.log('getBooks', key, value) +function getUserInfo(key: string, value: IUserInfo) { + console.log('getUserInfo', key, value) } +myStore.watchEffect('count', getCount) myStore.watchEffect('userInfo', getUserInfo) -myStore.watchEffect('books', getBooks) console.log('------------------------------') -myStore.changeUserInfoAction({ name: 'coderhxl', age: 18 }) -myStore.changeBookPriceAction(0, 66) +myStore.incrementAction() +myStore.incrementAction() +myStore.changeUserInfoAction({ name: 'coderhxl', age: 18 }) diff --git a/tsconfig.json b/tsconfig.json index 0b96852..7c79837 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,5 +10,5 @@ "strict": true, "skipLibCheck": true }, - "include": ["src/**/*.ts"] + "include": ["src/**/*", "test/**/*"] }