@free-walk/svelte-store 1.0.0 → 1.1.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.
Files changed (3) hide show
  1. package/README.md +50 -50
  2. package/README.zh-CN.md +185 -0
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -1,27 +1,29 @@
1
- # svelte-store
1
+ # @free-walk/svelte-store
2
2
 
3
- Pinia 风格的 Svelte 5 状态管理库,基于 `svelte/store` 构建。
3
+ A Pinia-style state management library for Svelte 5, built on `svelte/store`.
4
4
 
5
- ## 特性
5
+ [中文文档](./README.zh-CN.md)
6
6
 
7
- - 🏪 **Pinia 风格 API** — `defineStore` 定义 store,支持 Options API 和 Setup 风格
8
- - 📦 **基于 svelte/store** — 完全兼容 Svelte 生态,支持 `$store` 自动订阅
9
- - 🔌 **插件系统** — 支持全局插件,可扩展持久化、日志等功能
10
- - 🎯 **TypeScript** — 完整类型推导
11
- - ⚡ **轻量** — 零额外依赖,仅依赖 `svelte/store`
7
+ ## Features
12
8
 
13
- ## 安装
9
+ - **Pinia-style API** — `defineStore` with Options API and Setup style support
10
+ - **Built on svelte/store** — fully compatible with Svelte ecosystem, supports `$store` auto-subscription
11
+ - **Plugin system** — global plugins for persistence, logging, and more
12
+ - **TypeScript** — full type inference
13
+ - **Lightweight** — zero extra dependencies, relies only on `svelte/store`
14
+
15
+ ## Installation
14
16
 
15
17
  ```bash
16
- npm install svelte-store
18
+ npm install @free-walk/svelte-store
17
19
  ```
18
20
 
19
- ## 快速开始
21
+ ## Quick Start
20
22
 
21
- ### Options API 风格
23
+ ### Options API Style
22
24
 
23
25
  ```ts
24
- import { defineStore } from 'svelte-store'
26
+ import { defineStore } from '@free-walk/svelte-store'
25
27
 
26
28
  export const useCounterStore = defineStore('counter', {
27
29
  state: () => ({
@@ -46,10 +48,10 @@ export const useCounterStore = defineStore('counter', {
46
48
  })
47
49
  ```
48
50
 
49
- ### Setup 风格
51
+ ### Setup Style
50
52
 
51
53
  ```ts
52
- import { defineStore, writable, derived } from 'svelte-store'
54
+ import { defineStore, writable, derived } from '@free-walk/svelte-store'
53
55
 
54
56
  export const useCounterStore = defineStore('counter', () => {
55
57
  const count = writable(0)
@@ -67,7 +69,7 @@ export const useCounterStore = defineStore('counter', () => {
67
69
  })
68
70
  ```
69
71
 
70
- ### 在组件中使用
72
+ ### Usage in Components
71
73
 
72
74
  ```svelte
73
75
  <script>
@@ -83,49 +85,48 @@ export const useCounterStore = defineStore('counter', () => {
83
85
  <button onclick={() => counter.decrement()}>-</button>
84
86
  ```
85
87
 
86
- ## Store 实例 API
88
+ ## Store Instance API
87
89
 
88
- | 方法 / 属性 | 说明 |
89
- |---|---|
90
- | `$id` | Store 的唯一标识字符串 |
91
- | `$state` | 获取当前 state 快照 |
92
- | `$patch(partial)` | 批量更新 state(对象或函数) |
93
- | `$reset()` | 重置 state 到初始值(仅 Options API |
94
- | `$subscribe(callback)` | 监听 state 变化,返回取消订阅函数 |
95
- | `subscribe(run)` | svelte/store 标准订阅接口 |
90
+ | Method / Property | Description |
91
+ |-------------------|-------------|
92
+ | `$id` | Store's unique identifier string |
93
+ | `$state` | Get current state snapshot |
94
+ | `$patch(partial)` | Batch update state (object or function) |
95
+ | `$reset()` | Reset state to initial value (Options API only) |
96
+ | `$subscribe(callback)` | Listen for state changes, returns unsubscribe function |
97
+ | `subscribe(run)` | Standard svelte/store subscription interface |
96
98
 
97
- ### $patch 用法
99
+ ### $patch Usage
98
100
 
99
101
  ```ts
100
- // 对象方式
102
+ // Object style
101
103
  counter.$patch({ count: 10 })
102
104
 
103
- // 函数方式
105
+ // Function style
104
106
  counter.$patch((state) => {
105
107
  state.count++
106
108
  state.name = 'Updated'
107
109
  })
108
110
  ```
109
111
 
110
- ### $subscribe 用法
112
+ ### $subscribe Usage
111
113
 
112
114
  ```ts
113
115
  const unsubscribe = counter.$subscribe((state) => {
114
- console.log('state 变化:', state)
116
+ console.log('State changed:', state)
115
117
  })
116
118
 
117
- // 需要时取消订阅
119
+ // Unsubscribe when needed
118
120
  unsubscribe()
119
121
  ```
120
122
 
121
- ## 插件系统
123
+ ## Plugin System
122
124
 
123
125
  ```ts
124
- import { addPlugin } from 'svelte-store'
126
+ import { addPlugin } from '@free-walk/svelte-store'
125
127
 
126
- // 持久化插件示例
128
+ // Persistence plugin example
127
129
  addPlugin(({ store, storeId }) => {
128
- // 恢复持久化数据
129
130
  const key = `svelte-store-${storeId}`
130
131
  const saved = localStorage.getItem(key)
131
132
  if (saved) {
@@ -134,13 +135,12 @@ addPlugin(({ store, storeId }) => {
134
135
  } catch {}
135
136
  }
136
137
 
137
- // 监听变化并持久化
138
138
  store.$subscribe((state) => {
139
139
  localStorage.setItem(key, JSON.stringify(state))
140
140
  })
141
141
  })
142
142
 
143
- // 日志插件示例
143
+ // Logging plugin example
144
144
  addPlugin(({ store, storeId }) => {
145
145
  store.$subscribe((state) => {
146
146
  console.log(`[${storeId}]`, state)
@@ -148,37 +148,37 @@ addPlugin(({ store, storeId }) => {
148
148
  })
149
149
  ```
150
150
 
151
- ## 辅助函数
151
+ ## Helper Functions
152
152
 
153
153
  ### mapState
154
154
 
155
- store 中提取状态为独立的 Readable store:
155
+ Extract state properties as individual Readable stores:
156
156
 
157
157
  ```ts
158
- import { mapState } from 'svelte-store'
158
+ import { mapState } from '@free-walk/svelte-store'
159
159
 
160
160
  const { count, name } = mapState(useCounterStore, ['count', 'name'])
161
- // count name Readable<number> Readable<string>
161
+ // count and name are Readable<number> and Readable<string>
162
162
  ```
163
163
 
164
164
  ### mapActions
165
165
 
166
- store 中提取 actions 为独立函数:
166
+ Extract actions as standalone functions:
167
167
 
168
168
  ```ts
169
- import { mapActions } from 'svelte-store'
169
+ import { mapActions } from '@free-walk/svelte-store'
170
170
 
171
171
  const { increment, decrement } = mapActions(useCounterStore, ['increment', 'decrement'])
172
- increment() // 直接调用
172
+ increment() // Call directly
173
173
  ```
174
174
 
175
- ## 兼容性
175
+ ## Compatibility
176
176
 
177
- | 格式 | 文件 | 用途 |
178
- |---|---|---|
179
- | ESM | `dist/svelte-store.mjs` | `import` 语法 |
180
- | CJS | `dist/svelte-store.cjs` | `require()` 语法 |
181
- | Svelte 源码 | `src/index.ts` | Svelte 项目直接引用 |
177
+ | Format | File | Usage |
178
+ |--------|------|-------|
179
+ | ESM | `dist/svelte-store.mjs` | `import` syntax |
180
+ | CJS | `dist/svelte-store.cjs` | `require()` syntax |
181
+ | Svelte Source | `src/index.ts` | Direct Svelte project import |
182
182
 
183
183
  ## License
184
184
 
@@ -0,0 +1,185 @@
1
+ # svelte-store
2
+
3
+ Pinia 风格的 Svelte 5 状态管理库,基于 `svelte/store` 构建。
4
+
5
+ ## 特性
6
+
7
+ - 🏪 **Pinia 风格 API** — `defineStore` 定义 store,支持 Options API 和 Setup 风格
8
+ - 📦 **基于 svelte/store** — 完全兼容 Svelte 生态,支持 `$store` 自动订阅
9
+ - 🔌 **插件系统** — 支持全局插件,可扩展持久化、日志等功能
10
+ - 🎯 **TypeScript** — 完整类型推导
11
+ - ⚡ **轻量** — 零额外依赖,仅依赖 `svelte/store`
12
+
13
+ ## 安装
14
+
15
+ ```bash
16
+ npm install svelte-store
17
+ ```
18
+
19
+ ## 快速开始
20
+
21
+ ### Options API 风格
22
+
23
+ ```ts
24
+ import { defineStore } from 'svelte-store'
25
+
26
+ export const useCounterStore = defineStore('counter', {
27
+ state: () => ({
28
+ count: 0,
29
+ name: 'Counter',
30
+ }),
31
+ getters: {
32
+ doubleCount: (state) => state.count * 2,
33
+ displayName: (state) => `${state.name}: ${state.count}`,
34
+ },
35
+ actions: {
36
+ increment() {
37
+ this.$patch({ count: this.$state.count + 1 })
38
+ },
39
+ decrement() {
40
+ this.$patch({ count: this.$state.count - 1 })
41
+ },
42
+ incrementBy(amount: number) {
43
+ this.$patch({ count: this.$state.count + amount })
44
+ },
45
+ },
46
+ })
47
+ ```
48
+
49
+ ### Setup 风格
50
+
51
+ ```ts
52
+ import { defineStore, writable, derived } from 'svelte-store'
53
+
54
+ export const useCounterStore = defineStore('counter', () => {
55
+ const count = writable(0)
56
+ const doubleCount = derived(count, ($count) => $count * 2)
57
+
58
+ function increment() {
59
+ count.update((n) => n + 1)
60
+ }
61
+
62
+ function decrement() {
63
+ count.update((n) => n - 1)
64
+ }
65
+
66
+ return { count, doubleCount, increment, decrement }
67
+ })
68
+ ```
69
+
70
+ ### 在组件中使用
71
+
72
+ ```svelte
73
+ <script>
74
+ import { useCounterStore } from '../stores/counter'
75
+
76
+ const counter = useCounterStore()
77
+ </script>
78
+
79
+ <p>Count: {counter.$state.count}</p>
80
+ <p>Double: {counter.doubleCount}</p>
81
+
82
+ <button onclick={() => counter.increment()}>+</button>
83
+ <button onclick={() => counter.decrement()}>-</button>
84
+ ```
85
+
86
+ ## Store 实例 API
87
+
88
+ | 方法 / 属性 | 说明 |
89
+ |---|---|
90
+ | `$id` | Store 的唯一标识字符串 |
91
+ | `$state` | 获取当前 state 快照 |
92
+ | `$patch(partial)` | 批量更新 state(对象或函数) |
93
+ | `$reset()` | 重置 state 到初始值(仅 Options API) |
94
+ | `$subscribe(callback)` | 监听 state 变化,返回取消订阅函数 |
95
+ | `subscribe(run)` | svelte/store 标准订阅接口 |
96
+
97
+ ### $patch 用法
98
+
99
+ ```ts
100
+ // 对象方式
101
+ counter.$patch({ count: 10 })
102
+
103
+ // 函数方式
104
+ counter.$patch((state) => {
105
+ state.count++
106
+ state.name = 'Updated'
107
+ })
108
+ ```
109
+
110
+ ### $subscribe 用法
111
+
112
+ ```ts
113
+ const unsubscribe = counter.$subscribe((state) => {
114
+ console.log('state 变化:', state)
115
+ })
116
+
117
+ // 需要时取消订阅
118
+ unsubscribe()
119
+ ```
120
+
121
+ ## 插件系统
122
+
123
+ ```ts
124
+ import { addPlugin } from 'svelte-store'
125
+
126
+ // 持久化插件示例
127
+ addPlugin(({ store, storeId }) => {
128
+ // 恢复持久化数据
129
+ const key = `svelte-store-${storeId}`
130
+ const saved = localStorage.getItem(key)
131
+ if (saved) {
132
+ try {
133
+ store.$patch(JSON.parse(saved))
134
+ } catch {}
135
+ }
136
+
137
+ // 监听变化并持久化
138
+ store.$subscribe((state) => {
139
+ localStorage.setItem(key, JSON.stringify(state))
140
+ })
141
+ })
142
+
143
+ // 日志插件示例
144
+ addPlugin(({ store, storeId }) => {
145
+ store.$subscribe((state) => {
146
+ console.log(`[${storeId}]`, state)
147
+ })
148
+ })
149
+ ```
150
+
151
+ ## 辅助函数
152
+
153
+ ### mapState
154
+
155
+ 从 store 中提取状态为独立的 Readable store:
156
+
157
+ ```ts
158
+ import { mapState } from 'svelte-store'
159
+
160
+ const { count, name } = mapState(useCounterStore, ['count', 'name'])
161
+ // count 和 name 是 Readable<number> 和 Readable<string>
162
+ ```
163
+
164
+ ### mapActions
165
+
166
+ 从 store 中提取 actions 为独立函数:
167
+
168
+ ```ts
169
+ import { mapActions } from 'svelte-store'
170
+
171
+ const { increment, decrement } = mapActions(useCounterStore, ['increment', 'decrement'])
172
+ increment() // 直接调用
173
+ ```
174
+
175
+ ## 兼容性
176
+
177
+ | 格式 | 文件 | 用途 |
178
+ |---|---|---|
179
+ | ESM | `dist/svelte-store.mjs` | `import` 语法 |
180
+ | CJS | `dist/svelte-store.cjs` | `require()` 语法 |
181
+ | Svelte 源码 | `src/index.ts` | Svelte 项目直接引用 |
182
+
183
+ ## License
184
+
185
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@free-walk/svelte-store",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "type": "module",
5
5
  "description": "Pinia 风格的 Svelte 5 状态管理库,基于 svelte/store 构建",
6
6
  "keywords": [
@@ -30,7 +30,7 @@
30
30
  "files": [
31
31
  "dist",
32
32
  "src",
33
- "README.md"
33
+ "README.md","README.zh-CN.md"
34
34
  ],
35
35
  "scripts": {
36
36
  "build": "node scripts/build.js"
@@ -42,4 +42,4 @@
42
42
  "esbuild": "^0.27.7",
43
43
  "svelte": "^5.55.1"
44
44
  }
45
- }
45
+ }