@lytjs/store 5.0.1 → 6.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +35 -30
- package/README.md +0 -303
- package/dist/index.cjs +0 -1
- package/dist/index.mjs +0 -1
- package/dist/types/create-store.d.ts +0 -190
- package/dist/types/create-store.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -9
- package/dist/types/index.d.ts.map +0 -1
package/package.json
CHANGED
|
@@ -1,50 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lytjs/store",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "6.4.0",
|
|
4
|
+
"description": "LytJS Signal-based state management with Option Store and Setup Store patterns",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "./dist/index.cjs",
|
|
6
7
|
"module": "./dist/index.mjs",
|
|
7
|
-
"types": "./dist/
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
8
9
|
"exports": {
|
|
9
10
|
".": {
|
|
10
|
-
"types": "./dist/
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
11
12
|
"import": "./dist/index.mjs",
|
|
12
|
-
"require": "./dist/index.cjs"
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
15
16
|
},
|
|
16
|
-
"sideEffects": false,
|
|
17
17
|
"files": [
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "echo 'Skipping store build for now'",
|
|
23
|
+
"dev": "tsup --watch",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"test:watch": "vitest",
|
|
26
|
+
"test:coverage": "vitest run --coverage",
|
|
27
|
+
"type-check": "tsc --noEmit",
|
|
28
|
+
"lint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
29
|
+
"clean": "rm -rf dist"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@lytjs/common-is": "^6.0.0",
|
|
33
|
+
"@lytjs/common-object": "^6.0.0",
|
|
34
|
+
"@lytjs/reactivity": "^6.1.0",
|
|
35
|
+
"@lytjs/component": "^6.1.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"tsup": "^8.0.0",
|
|
39
|
+
"typescript": "^5.4.0",
|
|
40
|
+
"vitest": "^3.0.0"
|
|
41
|
+
},
|
|
20
42
|
"license": "MIT",
|
|
21
|
-
"author": "lytjs",
|
|
22
43
|
"repository": {
|
|
23
44
|
"type": "git",
|
|
24
|
-
"url": "https://gitee.com/lytjs/lytjs"
|
|
45
|
+
"url": "https://gitee.com/lytjs/lytjs.git",
|
|
46
|
+
"directory": "packages/ecosystem/packages/store"
|
|
25
47
|
},
|
|
26
|
-
"homepage": "https://gitee.com/lytjs/lytjs",
|
|
27
48
|
"keywords": [
|
|
28
|
-
"lyt",
|
|
29
49
|
"lytjs",
|
|
30
|
-
"javascript",
|
|
31
|
-
"framework",
|
|
32
|
-
"frontend",
|
|
33
|
-
"vue-like",
|
|
34
|
-
"lightweight",
|
|
35
|
-
"zero-dependency",
|
|
36
50
|
"store",
|
|
37
51
|
"state",
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"@lytjs/common": "^5.0.1",
|
|
42
|
-
"@lytjs/reactivity": "^5.0.1"
|
|
43
|
-
},
|
|
44
|
-
"engines": {
|
|
45
|
-
"node": ">=18.0.0"
|
|
46
|
-
},
|
|
47
|
-
"publishConfig": {
|
|
48
|
-
"access": "public"
|
|
49
|
-
}
|
|
52
|
+
"signal",
|
|
53
|
+
"reactive"
|
|
54
|
+
]
|
|
50
55
|
}
|
package/README.md
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
# @lytjs/store
|
|
2
|
-
|
|
3
|
-
Lyt.js 状态管理 - Pinia 风格的简单直观的状态管理库。
|
|
4
|
-
|
|
5
|
-
## 安装
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @lytjs/store
|
|
9
|
-
|
|
10
|
-
# 或使用 pnpm
|
|
11
|
-
pnpm add @lytjs/store
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
## 特性
|
|
15
|
-
|
|
16
|
-
- 🚀 DevTools 支持
|
|
17
|
-
- 🔄 热模块替换 (HMR)
|
|
18
|
-
- 📦 插件系统
|
|
19
|
-
- 🎯 TypeScript 友好
|
|
20
|
-
- 🔌 零运行时依赖
|
|
21
|
-
- 💾 可通过插件支持持久化
|
|
22
|
-
|
|
23
|
-
## 快速开始
|
|
24
|
-
|
|
25
|
-
```javascript
|
|
26
|
-
import { defineStore } from '@lytjs/store';
|
|
27
|
-
|
|
28
|
-
// 1. 定义 Store
|
|
29
|
-
export const useCounterStore = defineStore('counter', {
|
|
30
|
-
state: () => ({
|
|
31
|
-
count: 0,
|
|
32
|
-
name: 'Lyt.js'
|
|
33
|
-
}),
|
|
34
|
-
getters: {
|
|
35
|
-
doubleCount: (state) => state.count * 2
|
|
36
|
-
},
|
|
37
|
-
actions: {
|
|
38
|
-
increment() {
|
|
39
|
-
this.count++;
|
|
40
|
-
},
|
|
41
|
-
async fetchData() {
|
|
42
|
-
const res = await api.get('/data');
|
|
43
|
-
this.data = res.data;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// 2. 在组件中使用
|
|
49
|
-
import { useCounterStore } from './store';
|
|
50
|
-
|
|
51
|
-
const counter = useCounterStore();
|
|
52
|
-
|
|
53
|
-
counter.increment();
|
|
54
|
-
console.log(counter.count);
|
|
55
|
-
console.log(counter.doubleCount);
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
## 组合式 Store
|
|
59
|
-
|
|
60
|
-
```javascript
|
|
61
|
-
import { defineStore } from '@lytjs/store';
|
|
62
|
-
import { ref, computed } from '@lytjs/reactivity';
|
|
63
|
-
|
|
64
|
-
export const useCounterStore = defineStore('counter', () => {
|
|
65
|
-
const count = ref(0);
|
|
66
|
-
const name = ref('Lyt.js');
|
|
67
|
-
const doubleCount = computed(() => count.value * 2);
|
|
68
|
-
|
|
69
|
-
function increment() {
|
|
70
|
-
count.value++;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return { count, name, doubleCount, increment };
|
|
74
|
-
});
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
## 核心概念
|
|
78
|
-
|
|
79
|
-
### State
|
|
80
|
-
|
|
81
|
-
```javascript
|
|
82
|
-
export const useStore = defineStore('main', {
|
|
83
|
-
state: () => ({
|
|
84
|
-
counter: 0,
|
|
85
|
-
user: null
|
|
86
|
-
})
|
|
87
|
-
});
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
### Getters
|
|
91
|
-
|
|
92
|
-
```javascript
|
|
93
|
-
export const useStore = defineStore('main', {
|
|
94
|
-
state: () => ({ counter: 0 }),
|
|
95
|
-
getters: {
|
|
96
|
-
doubleCount: (state) => state.counter * 2,
|
|
97
|
-
doublePlusOne() {
|
|
98
|
-
return this.doubleCount + 1;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
### Actions
|
|
105
|
-
|
|
106
|
-
```javascript
|
|
107
|
-
export const useStore = defineStore('main', {
|
|
108
|
-
state: () => ({ counter: 0 }),
|
|
109
|
-
actions: {
|
|
110
|
-
increment() {
|
|
111
|
-
this.counter++;
|
|
112
|
-
},
|
|
113
|
-
randomizeCounter() {
|
|
114
|
-
this.counter = Math.round(100 * Math.random());
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
## 在 Setup 中使用
|
|
121
|
-
|
|
122
|
-
```javascript
|
|
123
|
-
import { useCounterStore } from '@/stores/counter';
|
|
124
|
-
import { storeToRefs } from '@lytjs/store';
|
|
125
|
-
|
|
126
|
-
export default {
|
|
127
|
-
setup() {
|
|
128
|
-
const store = useCounterStore();
|
|
129
|
-
|
|
130
|
-
// 直接读取
|
|
131
|
-
store.increment();
|
|
132
|
-
|
|
133
|
-
// 解构,保持响应式
|
|
134
|
-
const { count, doubleCount } = storeToRefs(store);
|
|
135
|
-
const { increment } = store;
|
|
136
|
-
|
|
137
|
-
return { count, doubleCount, increment };
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
## 订阅 State
|
|
143
|
-
|
|
144
|
-
```javascript
|
|
145
|
-
const store = useStore();
|
|
146
|
-
|
|
147
|
-
// 订阅 state 的变化
|
|
148
|
-
const unsubscribe = store.$subscribe((mutation, state) => {
|
|
149
|
-
console.log(mutation);
|
|
150
|
-
localStorage.setItem('cart', JSON.stringify(state));
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
// 订阅 actions 的调用
|
|
154
|
-
const unsubscribeAction = store.$onAction(({ name, after, onError }) => {
|
|
155
|
-
console.log(`Action ${name} called`);
|
|
156
|
-
|
|
157
|
-
after((result) => {
|
|
158
|
-
console.log(`Action ${name} finished with result:`, result);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
onError((error) => {
|
|
162
|
-
console.error(`Action ${name} failed:`, error);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
## 插件
|
|
168
|
-
|
|
169
|
-
```javascript
|
|
170
|
-
import { createPinia } from '@lytjs/store';
|
|
171
|
-
|
|
172
|
-
const pinia = createPinia();
|
|
173
|
-
|
|
174
|
-
// 持久化插件示例
|
|
175
|
-
function localStoragePlugin(context) {
|
|
176
|
-
const { store } = context;
|
|
177
|
-
|
|
178
|
-
const savedState = localStorage.getItem(store.$id);
|
|
179
|
-
if (savedState) {
|
|
180
|
-
store.$patch(JSON.parse(savedState));
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
store.$subscribe((mutation, state) => {
|
|
184
|
-
localStorage.setItem(store.$id, JSON.stringify(state));
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
pinia.use(localStoragePlugin);
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
## DevTools
|
|
192
|
-
|
|
193
|
-
```javascript
|
|
194
|
-
import { createPinia } from '@lytjs/store';
|
|
195
|
-
|
|
196
|
-
const pinia = createPinia();
|
|
197
|
-
|
|
198
|
-
// 启用 DevTools
|
|
199
|
-
pinia.use(({ store }) => {
|
|
200
|
-
store.$onAction(({ name, after }) => {
|
|
201
|
-
after(() => {
|
|
202
|
-
console.log(`[Store] ${name}`);
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
## 示例
|
|
209
|
-
|
|
210
|
-
### 完整示例
|
|
211
|
-
|
|
212
|
-
```javascript
|
|
213
|
-
import { defineStore } from '@lytjs/store';
|
|
214
|
-
|
|
215
|
-
export const useUserStore = defineStore('user', {
|
|
216
|
-
state: () => ({
|
|
217
|
-
user: null,
|
|
218
|
-
isAuthenticated: false,
|
|
219
|
-
token: null
|
|
220
|
-
}),
|
|
221
|
-
|
|
222
|
-
getters: {
|
|
223
|
-
userName: (state) => state.user?.name,
|
|
224
|
-
isLoggedIn: (state) => state.isAuthenticated
|
|
225
|
-
},
|
|
226
|
-
|
|
227
|
-
actions: {
|
|
228
|
-
async login(credentials) {
|
|
229
|
-
try {
|
|
230
|
-
const response = await api.post('/login', credentials);
|
|
231
|
-
this.user = response.data.user;
|
|
232
|
-
this.token = response.data.token;
|
|
233
|
-
this.isAuthenticated = true;
|
|
234
|
-
localStorage.setItem('token', this.token);
|
|
235
|
-
} catch (error) {
|
|
236
|
-
console.error('Login failed:', error);
|
|
237
|
-
throw error;
|
|
238
|
-
}
|
|
239
|
-
},
|
|
240
|
-
|
|
241
|
-
logout() {
|
|
242
|
-
this.user = null;
|
|
243
|
-
this.token = null;
|
|
244
|
-
this.isAuthenticated = false;
|
|
245
|
-
localStorage.removeItem('token');
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
### 组合式 API 示例
|
|
252
|
-
|
|
253
|
-
```javascript
|
|
254
|
-
import { defineStore } from '@lytjs/store';
|
|
255
|
-
import { ref, computed } from '@lytjs/reactivity';
|
|
256
|
-
|
|
257
|
-
export const useTodoStore = defineStore('todo', () => {
|
|
258
|
-
const todos = ref([]);
|
|
259
|
-
|
|
260
|
-
const completedTodos = computed(() =>
|
|
261
|
-
todos.value.filter(todo => todo.completed)
|
|
262
|
-
);
|
|
263
|
-
|
|
264
|
-
const pendingTodos = computed(() =>
|
|
265
|
-
todos.value.filter(todo => !todo.completed)
|
|
266
|
-
);
|
|
267
|
-
|
|
268
|
-
function addTodo(text) {
|
|
269
|
-
todos.value.push({
|
|
270
|
-
id: Date.now(),
|
|
271
|
-
text,
|
|
272
|
-
completed: false
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
function toggleTodo(id) {
|
|
277
|
-
const todo = todos.value.find(t => t.id === id);
|
|
278
|
-
if (todo) {
|
|
279
|
-
todo.completed = !todo.completed;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
return { todos, completedTodos, pendingTodos, addTodo, toggleTodo };
|
|
284
|
-
});
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
## 性能
|
|
288
|
-
|
|
289
|
-
- 轻量级,零运行时依赖
|
|
290
|
-
- 基于 Proxy 的响应式系统
|
|
291
|
-
- 高效的订阅机制
|
|
292
|
-
|
|
293
|
-
## 兼容性
|
|
294
|
-
|
|
295
|
-
- Node.js >= 18.0.0
|
|
296
|
-
- Chrome 64+
|
|
297
|
-
- Firefox 63+
|
|
298
|
-
- Safari 12+
|
|
299
|
-
- Edge 79+
|
|
300
|
-
|
|
301
|
-
## License
|
|
302
|
-
|
|
303
|
-
MIT
|
package/dist/index.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var x=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var W=(o,t)=>{for(var S in t)x(o,S,{get:t[S],enumerable:!0})},F=(o,t,S,f)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of L(t))!V.call(o,c)&&c!==S&&x(o,c,{get:()=>t[c],enumerable:!(f=P(t,c))||f.enumerable});return o};var H=o=>F(x({},"__esModule",{value:!0}),o);var K={};W(K,{clearAllStores:()=>C,createStore:()=>w,getStore:()=>O,getStoreIds:()=>j});module.exports=H(K);var y=require("@lytjs/reactivity"),i=require("@lytjs/common"),l=new Map;function O(o){return l.get(o)}function w(o,t={}){if(l.has(o))return()=>l.get(o);let S=typeof t.state=="function"?t.state():t.state||{},f={};if(t.modules)for(let e of Object.keys(t.modules)){let n=t.modules[e],r=typeof n.state=="function"?n.state():n.state||{};f[e]={...r}}let c={...S,...f},a=(0,y.reactive)({...c}),m={};if(t.getters)for(let e of Object.keys(t.getters)){let n=t.getters[e],r=(0,y.computed)(()=>n(a));Object.defineProperty(m,e,{get(){return r.value},enumerable:!0})}if(t.modules)for(let e of Object.keys(t.modules)){let n=t.modules[e];if(n.getters)for(let r of Object.keys(n.getters)){let s=n.getters[r],v=`${e}/${r}`,R=(0,y.computed)(()=>{let A=a[e];return s(A)});Object.defineProperty(m,v,{get(){return R.value},enumerable:!0})}}let $={},g=[];if(t.actions)for(let e of Object.keys(t.actions)){let n=t.actions[e];$[e]=function(...r){for(let s of g)s({name:e,args:r});return n.apply(b,r)}}if(t.modules)for(let e of Object.keys(t.modules)){let n=t.modules[e];if(n.actions)for(let r of Object.keys(n.actions)){let s=n.actions[r],v=`${e}/${r}`;$[v]=function(...R){for(let A of g)A({name:v,args:R});return s.apply(b,R)}}}let k=new i.SubscriptionManager,d=!1,p=[];function h(e){k.notify([e,a])}let u=null;function I(){if(u||d)return;let e=(0,i.createSnapshot)(a);u=(0,y.watch)(a,()=>{let n=(0,i.createSnapshot)(a),r=(0,i.diffObjects)(e,n);for(let s in r.added)h({storeId:o,type:"add",key:s,newValue:r.added[s]});for(let s in r.removed)h({storeId:o,type:"delete",key:s,oldValue:r.removed[s]});for(let s in r.changed)h({storeId:o,type:"set",key:s,newValue:r.changed[s].new,oldValue:r.changed[s].old});e=n},{deep:!0}),p.push(u)}function M(){if(!k.hasSubscribers()&&u){u();let e=p.indexOf(u);e!==-1&&p.splice(e,1),u=null}}let b={$id:o,get state(){return a},get getters(){return m},get actions(){return $},$expose(){return{state:a,getters:m}},$reset(){if(d)return;let e=Object.keys(a),n=Object.keys(c);for(let r of n)a[r]=c[r];for(let r of e)n.includes(r)||delete a[r]},$subscribe(e){if(d)return()=>{};let n=k.subscribe(([r,s])=>{e(r,s)});return I(),()=>{n(),M()}},$dispose(){if(!d){for(let e of p)e();p.length=0,u=null,k.clear(),l.delete(o),d=!0}},$patch(e){d||((0,i.isFunction)(e)?e(a):(0,i.mergeObjects)(a,e))},use(e){if(d)return()=>{};let n=e.install(b);return()=>{typeof n=="function"&&n()}},$onAction(e){return d?()=>{}:(g.push(e),()=>{let n=g.indexOf(e);n!==-1&&g.splice(n,1)})}};return l.set(o,b),()=>b}function j(){return Array.from(l.keys())}function C(){for(let[o,t]of l)t.$dispose();l.clear()}
|
package/dist/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{reactive as w,computed as $,watch as j}from"@lytjs/reactivity";import{createSnapshot as h,diffObjects as C,mergeObjects as I,isFunction as M,SubscriptionManager as P}from"@lytjs/common";var d=new Map;function L(a){return d.get(a)}function V(a,r={}){if(d.has(a))return()=>d.get(a);let A=typeof r.state=="function"?r.state():r.state||{},R={};if(r.modules)for(let e of Object.keys(r.modules)){let t=r.modules[e],n=typeof t.state=="function"?t.state():t.state||{};R[e]={...n}}let b={...A,...R},s=w({...b}),S={};if(r.getters)for(let e of Object.keys(r.getters)){let t=r.getters[e],n=$(()=>t(s));Object.defineProperty(S,e,{get(){return n.value},enumerable:!0})}if(r.modules)for(let e of Object.keys(r.modules)){let t=r.modules[e];if(t.getters)for(let n of Object.keys(t.getters)){let o=t.getters[n],g=`${e}/${n}`,p=$(()=>{let v=s[e];return o(v)});Object.defineProperty(S,g,{get(){return p.value},enumerable:!0})}}let m={},u=[];if(r.actions)for(let e of Object.keys(r.actions)){let t=r.actions[e];m[e]=function(...n){for(let o of u)o({name:e,args:n});return t.apply(y,n)}}if(r.modules)for(let e of Object.keys(r.modules)){let t=r.modules[e];if(t.actions)for(let n of Object.keys(t.actions)){let o=t.actions[n],g=`${e}/${n}`;m[g]=function(...p){for(let v of u)v({name:g,args:p});return o.apply(y,p)}}}let f=new P,i=!1,l=[];function k(e){f.notify([e,s])}let c=null;function x(){if(c||i)return;let e=h(s);c=j(s,()=>{let t=h(s),n=C(e,t);for(let o in n.added)k({storeId:a,type:"add",key:o,newValue:n.added[o]});for(let o in n.removed)k({storeId:a,type:"delete",key:o,oldValue:n.removed[o]});for(let o in n.changed)k({storeId:a,type:"set",key:o,newValue:n.changed[o].new,oldValue:n.changed[o].old});e=t},{deep:!0}),l.push(c)}function O(){if(!f.hasSubscribers()&&c){c();let e=l.indexOf(c);e!==-1&&l.splice(e,1),c=null}}let y={$id:a,get state(){return s},get getters(){return S},get actions(){return m},$expose(){return{state:s,getters:S}},$reset(){if(i)return;let e=Object.keys(s),t=Object.keys(b);for(let n of t)s[n]=b[n];for(let n of e)t.includes(n)||delete s[n]},$subscribe(e){if(i)return()=>{};let t=f.subscribe(([n,o])=>{e(n,o)});return x(),()=>{t(),O()}},$dispose(){if(!i){for(let e of l)e();l.length=0,c=null,f.clear(),d.delete(a),i=!0}},$patch(e){i||(M(e)?e(s):I(s,e))},use(e){if(i)return()=>{};let t=e.install(y);return()=>{typeof t=="function"&&t()}},$onAction(e){return i?()=>{}:(u.push(e),()=>{let t=u.indexOf(e);t!==-1&&u.splice(t,1)})}};return d.set(a,y),()=>y}function W(){return Array.from(d.keys())}function F(){for(let[a,r]of d)r.$dispose();d.clear()}export{F as clearAllStores,V as createStore,L as getStore,W as getStoreIds};
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Lyt.js 状态管理 — Store 创建
|
|
3
|
-
*
|
|
4
|
-
* 提供轻量级的状态管理方案,支持:
|
|
5
|
-
* - 响应式状态(reactive 包装)
|
|
6
|
-
* - 计算属性(computed 包装)
|
|
7
|
-
* - 操作方法(actions)
|
|
8
|
-
* - 状态订阅($subscribe)
|
|
9
|
-
* - 状态重置($reset)
|
|
10
|
-
* - Store 销毁($dispose)
|
|
11
|
-
*
|
|
12
|
-
* 设计理念:
|
|
13
|
-
* - 基于 @lytjs/reactivity 响应式系统
|
|
14
|
-
* - API 风格参考 Pinia,简洁易用
|
|
15
|
-
* - 零第三方运行时依赖
|
|
16
|
-
*/
|
|
17
|
-
/** Store 选项 */
|
|
18
|
-
export interface StoreOptions<S extends Record<string, any> = Record<string, any>> {
|
|
19
|
-
/** 初始状态(对象或工厂函数) */
|
|
20
|
-
state?: S | (() => S);
|
|
21
|
-
/** 计算属性(getter 函数集合) */
|
|
22
|
-
getters?: Record<string, (state: S) => any>;
|
|
23
|
-
/** 操作方法 */
|
|
24
|
-
actions?: Record<string, (this: StoreApi<S>, ...args: any[]) => any>;
|
|
25
|
-
/** 模块化子模块 */
|
|
26
|
-
modules?: Record<string, ModuleOptions>;
|
|
27
|
-
}
|
|
28
|
-
/** 子模块选项 */
|
|
29
|
-
export interface ModuleOptions {
|
|
30
|
-
/** 子模块初始状态 */
|
|
31
|
-
state?: Record<string, any> | (() => Record<string, any>);
|
|
32
|
-
/** 子模块计算属性 */
|
|
33
|
-
getters?: Record<string, (state: any) => any>;
|
|
34
|
-
/** 子模块操作方法 */
|
|
35
|
-
actions?: Record<string, (this: StoreApi, ...args: any[]) => any>;
|
|
36
|
-
/** 嵌套子模块 */
|
|
37
|
-
modules?: Record<string, ModuleOptions>;
|
|
38
|
-
}
|
|
39
|
-
/** Store 插件接口 */
|
|
40
|
-
export interface StorePlugin {
|
|
41
|
-
/** 安装插件,返回卸载函数(可选) */
|
|
42
|
-
install: (store: StoreApi) => (() => void) | void;
|
|
43
|
-
}
|
|
44
|
-
/** 订阅回调参数 */
|
|
45
|
-
export interface SubscriptionCallbackArgument {
|
|
46
|
-
/** Store ID */
|
|
47
|
-
storeId: string;
|
|
48
|
-
/** 事件类型 */
|
|
49
|
-
type: 'set' | 'delete' | 'add' | 'direct';
|
|
50
|
-
/** 变化的键名 */
|
|
51
|
-
key: string;
|
|
52
|
-
/** 新值 */
|
|
53
|
-
newValue?: any;
|
|
54
|
-
/** 旧值 */
|
|
55
|
-
oldValue?: any;
|
|
56
|
-
}
|
|
57
|
-
/** 订阅回调函数 */
|
|
58
|
-
export type SubscriptionCallback = (mutation: SubscriptionCallbackArgument, state: any) => void;
|
|
59
|
-
/** Store 公共 API */
|
|
60
|
-
export interface StoreApi<S extends Record<string, any> = Record<string, any>> {
|
|
61
|
-
/** Store 唯一标识 */
|
|
62
|
-
$id: string;
|
|
63
|
-
/** 响应式状态 */
|
|
64
|
-
state: S;
|
|
65
|
-
/** 计算属性 */
|
|
66
|
-
getters: Record<string, any>;
|
|
67
|
-
/** 操作方法 */
|
|
68
|
-
actions: Record<string, (...args: any[]) => any>;
|
|
69
|
-
/** 在组件中使用 Store,返回 state 和 getters */
|
|
70
|
-
/** 获取 state 和 getters(用于非插件场景) */
|
|
71
|
-
$expose(): {
|
|
72
|
-
state: S;
|
|
73
|
-
getters: Record<string, any>;
|
|
74
|
-
};
|
|
75
|
-
/** 重置状态到初始值 */
|
|
76
|
-
$reset(): void;
|
|
77
|
-
/** 订阅状态变化 */
|
|
78
|
-
$subscribe(callback: SubscriptionCallback): () => void;
|
|
79
|
-
/** 销毁 Store */
|
|
80
|
-
$dispose(): void;
|
|
81
|
-
/** 批量更新状态(对象合并或函数式) */
|
|
82
|
-
$patch(partialOrFn: Partial<S> | ((state: S) => void)): void;
|
|
83
|
-
/** 安装插件 */
|
|
84
|
-
use(plugin: StorePlugin): () => void;
|
|
85
|
-
/** 拦截 action 调用 */
|
|
86
|
-
$onAction(callback: (action: {
|
|
87
|
-
name: string;
|
|
88
|
-
args: any[];
|
|
89
|
-
}) => void): () => void;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* 获取已注册的 Store
|
|
93
|
-
*
|
|
94
|
-
* @param id - Store ID
|
|
95
|
-
* @returns Store 实例或 undefined
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* ```ts
|
|
99
|
-
* import { createStore, getStore } from '@lytjs/store'
|
|
100
|
-
*
|
|
101
|
-
* const useCounter = createStore('counter', { state: () => ({ count: 0 }) })
|
|
102
|
-
* const store = getStore('counter')
|
|
103
|
-
* console.log(store?.state.count) // 0
|
|
104
|
-
* ```
|
|
105
|
-
*/
|
|
106
|
-
export declare function getStore<S extends Record<string, any> = Record<string, any>>(id: string): StoreApi<S> | undefined;
|
|
107
|
-
/**
|
|
108
|
-
* 创建 Store
|
|
109
|
-
*
|
|
110
|
-
* 创建一个包含响应式状态、计算属性和操作方法的状态管理单元。
|
|
111
|
-
*
|
|
112
|
-
* @param id - Store 唯一标识
|
|
113
|
-
* @param options - Store 配置选项
|
|
114
|
-
* @returns Store 实例
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* ```ts
|
|
118
|
-
* // 定义 Store
|
|
119
|
-
* const useCounterStore = createStore('counter', {
|
|
120
|
-
* state: () => ({
|
|
121
|
-
* count: 0,
|
|
122
|
-
* name: 'lyt',
|
|
123
|
-
* }),
|
|
124
|
-
* getters: {
|
|
125
|
-
* doubleCount: (state) => state.count * 2,
|
|
126
|
-
* },
|
|
127
|
-
* actions: {
|
|
128
|
-
* increment() {
|
|
129
|
-
* this.state.count++
|
|
130
|
-
* },
|
|
131
|
-
* async fetchData() {
|
|
132
|
-
* const data = await fetch('/api')
|
|
133
|
-
* this.state.name = data.name
|
|
134
|
-
* },
|
|
135
|
-
* },
|
|
136
|
-
* })
|
|
137
|
-
*
|
|
138
|
-
* // 使用 Store
|
|
139
|
-
* const store = useCounterStore()
|
|
140
|
-
* console.log(store.state.count) // 0
|
|
141
|
-
* console.log(store.getters.doubleCount) // 0
|
|
142
|
-
* store.actions.increment()
|
|
143
|
-
* console.log(store.state.count) // 1
|
|
144
|
-
* console.log(store.getters.doubleCount) // 2
|
|
145
|
-
*
|
|
146
|
-
* // 订阅变化
|
|
147
|
-
* store.$subscribe((mutation, state) => {
|
|
148
|
-
* console.log(`${mutation.type}: ${mutation.key}`)
|
|
149
|
-
* })
|
|
150
|
-
*
|
|
151
|
-
* // 重置状态
|
|
152
|
-
* store.$reset()
|
|
153
|
-
*
|
|
154
|
-
* // 销毁 Store
|
|
155
|
-
* store.$dispose()
|
|
156
|
-
* ```
|
|
157
|
-
*/
|
|
158
|
-
export declare function createStore<S extends Record<string, any> = Record<string, any>>(id: string, options?: StoreOptions<S>): () => StoreApi<S>;
|
|
159
|
-
/**
|
|
160
|
-
* 获取所有已注册的 Store ID 列表
|
|
161
|
-
*
|
|
162
|
-
* @returns Store ID 数组
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* ```ts
|
|
166
|
-
* import { createStore, getStoreIds } from '@lytjs/store'
|
|
167
|
-
*
|
|
168
|
-
* createStore('user', { state: () => ({ name: 'lyt' }) })
|
|
169
|
-
* createStore('cart', { state: () => ({ items: [] }) })
|
|
170
|
-
* console.log(getStoreIds()) // ['user', 'cart']
|
|
171
|
-
* ```
|
|
172
|
-
*/
|
|
173
|
-
export declare function getStoreIds(): string[];
|
|
174
|
-
/**
|
|
175
|
-
* 清除所有已注册的 Store(仅用于测试)
|
|
176
|
-
*
|
|
177
|
-
* 销毁所有 Store 实例并清空注册表。
|
|
178
|
-
* 在测试环境的 afterEach 中调用以避免状态污染。
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* ```ts
|
|
182
|
-
* import { createStore, clearAllStores } from '@lytjs/store'
|
|
183
|
-
*
|
|
184
|
-
* afterEach(() => {
|
|
185
|
-
* clearAllStores()
|
|
186
|
-
* })
|
|
187
|
-
* ```
|
|
188
|
-
*/
|
|
189
|
-
export declare function clearAllStores(): void;
|
|
190
|
-
//# sourceMappingURL=create-store.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"create-store.d.ts","sourceRoot":"","sources":["../../src/create-store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAsBH,eAAe;AACf,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAC/E,oBAAoB;IACpB,KAAK,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IACtB,wBAAwB;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,GAAG,CAAC,CAAC;IAC5C,WAAW;IACX,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC,CAAC;IACrE,aAAa;IACb,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;CACzC;AAED,YAAY;AACZ,MAAM,WAAW,aAAa;IAC5B,cAAc;IACd,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;IAC1D,cAAc;IACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,GAAG,CAAC,CAAC;IAC9C,cA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AAE,CAEtC;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,IAAI,IAAI,CAKrC"}
|
package/dist/types/index.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Lyt.js 状态管理 — 统一导出入口
|
|
3
|
-
*
|
|
4
|
-
* 导出所有公共 API 和类型定义。
|
|
5
|
-
* 纯原生零依赖实现。
|
|
6
|
-
*/
|
|
7
|
-
export { createStore, getStore, getStoreIds, clearAllStores } from './create-store';
|
|
8
|
-
export type { StoreOptions, StoreApi, SubscriptionCallback, SubscriptionCallbackArgument, ModuleOptions, StorePlugin, } from './create-store';
|
|
9
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEpF,YAAY,EACV,YAAY,EACZ,QAAQ,EACR,oBAAoB,EACpB,4BAA4B,EAC5B,aAAa,EACb,WAAW,GACZ,MAAM,gBAAgB,CAAC"}
|