Skip to content

Commit

Permalink
其他
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-hxl committed Nov 27, 2022
1 parent c2b5b52 commit 7aad303
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib"
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xl-store",
"version": "0.1.9",
"version": "0.1.10",
"description": "状态管理仓库",
"scripts": {
"build-esm": "node ./script/build.js esm",
Expand All @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion publish/LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion publish/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion publish/lib/index.mjs
Original file line number Diff line number Diff line change
@@ -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 $();
2 changes: 1 addition & 1 deletion publish/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 4 additions & 1 deletion script/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ require('esbuild')
platform: 'neutral',
minify: true,
outdir: './test',
watch: true
watch: true,
loader: {
'.ts': 'ts'
}
})
.then((res) => {
console.log('watching...')
Expand Down
39 changes: 15 additions & 24 deletions test/build-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
},
Expand All @@ -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 })
2 changes: 1 addition & 1 deletion test/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 20 additions & 17 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
},
Expand All @@ -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 })
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"strict": true,
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
"include": ["src/**/*", "test/**/*"]
}

0 comments on commit 7aad303

Please sign in to comment.