pinia-plugin-subscription 0.0.0-beta.1 → 0.0.0-beta.3
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/README.md +44 -2
- package/dist/pinia-plugin-subscription.js +534 -430
- package/dist/pinia-plugin-subscription.umd.cjs +4 -4
- package/dist/types/index.d.ts +24 -5
- package/dist/types/plugin.d.ts +23 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@ app.mount('#app')
|
|
|
37
37
|
|
|
38
38
|
```typescript
|
|
39
39
|
// src/core/my-store.ts
|
|
40
|
-
import Store from '
|
|
40
|
+
import { Store } from 'pinia-plugin-subscription'
|
|
41
41
|
|
|
42
42
|
class MyStore extends Store {
|
|
43
43
|
constructor(store, options, debug = false) {
|
|
@@ -112,7 +112,43 @@ export const myStoreSubscriber = {
|
|
|
112
112
|
|
|
113
113
|
### `PluginSubscriber`
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
The `PluginSubscriber` interface has been extended: it's still an object with at least an `invoke(context: PiniaPluginContext, debug?: boolean)` method, but it can now expose several useful properties for plugins:
|
|
116
|
+
|
|
117
|
+
- **`resetStoreCallback?: (store?: Store) => void`**: callback invoked when the store is reset.
|
|
118
|
+
- **`storeOnActionSubscription?: StoreOnActionSubscription`**: provides a native Pinia `onAction` subscription via a getter returning `{ store, callback }`.
|
|
119
|
+
- **`storeMutationSubscription?: StoreMutationSubscription`**: provides a native mutation subscription (`store.$subscribe`) via a getter returning `{ store, callback }`.
|
|
120
|
+
- **`subscriptions: PluginSubscriptions | undefined`**: an object listing plugin-specific subscription functions (see `Store.addSubscription`).
|
|
121
|
+
|
|
122
|
+
These additions make it easier for subscribers to integrate with Pinia's native event cycle and to expose reusable extension points.
|
|
123
|
+
|
|
124
|
+
## The `PluginSubscriber` class (abstract)
|
|
125
|
+
|
|
126
|
+
The project provides an abstract `PluginSubscriber` implementation ([src/plugins/pluginSubscriber.ts](src/plugins/pluginSubscriber.ts)) that makes it easy to create reusable subscribers:
|
|
127
|
+
|
|
128
|
+
- Constructor: `new PluginSubscriber(pluginName: string, createInstanceFunction: CreateInstance)`
|
|
129
|
+
- Main behavior: in `invoke(context, debug)` the class creates a helper instance (`Store` or subclass) via the `createInstanceFunction` (typically `MyStore.customizeStore`) and exposes on the instance:
|
|
130
|
+
- `subscriptions` (from `store.getSubscriptions()`)
|
|
131
|
+
- `storeMutationSubscription` (from `store.storeSubscribe`)
|
|
132
|
+
- `storeOnActionSubscription` (from `store.onAction`)
|
|
133
|
+
- optionally a `pluginCreated(store)` hook called after initialization
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import PluginSubscriber from './src/plugins/pluginSubscriber'
|
|
138
|
+
import StoreExtension from './src/extending-pinia-store/core/StoreExtension'
|
|
139
|
+
import { addStore } from './src/extending-pinia-store/plugins/stores'
|
|
140
|
+
|
|
141
|
+
class ExtendingStoreSubscriber extends PluginSubscriber<StoreExtension> {
|
|
142
|
+
constructor() {
|
|
143
|
+
super('extendsPiniaStore', StoreExtension.customizeStore.bind(StoreExtension))
|
|
144
|
+
this.pluginCreated = addStore
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export const extendingStoreSubscriber = new ExtendingStoreSubscriber()
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Here `ExtendingStoreSubscriber` provides a `createInstanceFunction` that returns a `StoreExtension` instance if `options.storeOptions` is present, and sets a `pluginCreated` hook (here `addStore`) to run plugin-specific logic once the instance is ready.
|
|
116
152
|
|
|
117
153
|
## The `Store` class (summary)
|
|
118
154
|
|
|
@@ -121,11 +157,17 @@ export const myStoreSubscriber = {
|
|
|
121
157
|
- Properties: `debug`, `options`, `state`, `store`.
|
|
122
158
|
- Useful methods:
|
|
123
159
|
- `addToState(name, value?)`: adds a property to the store state and exposes it as a `Ref` on the store when appropriate.
|
|
160
|
+
- `addSubscription(pluginName, subscription)`: registers a plugin-specific subscription function (accessible via `getSubscriptions`).
|
|
161
|
+
- `getSubscriptions()`: returns subscriptions registered with `addSubscription` (or `undefined`).
|
|
162
|
+
- `storeSubscribe` (getter/setter): exposes a mutation subscription callback (`store.$subscribe`) as a factory returning `{ store, callback }`.
|
|
163
|
+
- `onAction` (getter/setter): exposes a Pinia `onAction` callback in the same form.
|
|
124
164
|
- `static customizeStore(store, options, debug?)`: instantiate the class (or subclass) when `options.storeOptions` is present.
|
|
125
165
|
- `debugLog(message, args)`: conditional logging when `debug` is true.
|
|
126
166
|
- `hasDeniedFirstChar(property)`, `getOption(...)`, `getValue(value)`, `getStatePropertyValue(...)`.
|
|
127
167
|
- `isOptionApi()`: true when the store uses Pinia Options API.
|
|
128
168
|
|
|
169
|
+
Other small helpers exposed: `stateHas(property)`, `storeHas(property)` and `getValue` to retrieve the real value of a `Ref` or a raw value.
|
|
170
|
+
|
|
129
171
|
`Store.customizeStore(...)` is the recommended entry point used by subscribers to create store helper instances.
|
|
130
172
|
|
|
131
173
|
|