pinia-plugin-subscription 0.0.0-beta.19 → 0.0.0-beta.20
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 +71 -90
- package/dist/types/index.d.ts +4 -4
- package/dist/types/plugin.d.ts +1 -1
- package/dist/types/store.d.ts +1 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,122 +10,109 @@
|
|
|
10
10
|
|
|
11
11
|
The main goal is to offer a clear API for writing reusable Pinia plugins and to make it easy to extend stores from plugin code.
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
## Installation
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
Ensure Pinia is installed, then register the plugin in your `main.ts`:
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
|
-
// main.ts
|
|
19
18
|
import { createApp } from 'vue'
|
|
20
19
|
import { createPinia } from 'pinia'
|
|
21
20
|
import { createPlugin } from 'pinia-plugin-subscription'
|
|
22
|
-
import myStoreSubscriber from './src/core/my-store'
|
|
21
|
+
import { myStoreSubscriber } from './src/core/my-store'
|
|
23
22
|
import App from './App.vue'
|
|
24
23
|
|
|
25
24
|
const app = createApp(App)
|
|
26
25
|
const pinia = createPinia()
|
|
27
26
|
|
|
28
|
-
// Register plugin (subscribers array, debug
|
|
29
|
-
pinia.use(createPlugin([myStoreSubscriber],
|
|
27
|
+
// Register plugin (subscribers array, debug mode)
|
|
28
|
+
pinia.use(createPlugin([myStoreSubscriber], true))
|
|
30
29
|
|
|
31
30
|
app.use(pinia)
|
|
32
31
|
app.mount('#app')
|
|
33
32
|
```
|
|
34
33
|
|
|
35
|
-
|
|
34
|
+
## Usage — Examples
|
|
35
|
+
|
|
36
|
+
**1) Subscriber using a `Store` subclass:**
|
|
36
37
|
|
|
37
38
|
```typescript
|
|
38
|
-
import
|
|
39
|
-
import {
|
|
39
|
+
import PluginSubscriber from 'pinia-plugin-subscription'
|
|
40
|
+
import { Store } from 'pinia-plugin-subscription'
|
|
40
41
|
|
|
41
42
|
class MyPlugin extends Store {
|
|
42
43
|
constructor(store, options, debug = false) {
|
|
43
44
|
super(store, options, debug)
|
|
44
45
|
this.doSomething()
|
|
45
46
|
}
|
|
46
|
-
// ...
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
class MyPluginSubscriber extends PluginSubscriber<MyPlugin> {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
MyPlugin.customizeStore.bind(MyPlugin),
|
|
54
|
-
PluginConsole
|
|
55
|
-
)
|
|
56
|
-
}
|
|
50
|
+
constructor() {
|
|
51
|
+
super('my-plugin', MyPlugin.customizeStore.bind(MyPlugin))
|
|
52
|
+
}
|
|
57
53
|
}
|
|
54
|
+
|
|
55
|
+
export const myStoreSubscriber = new MyPluginSubscriber()
|
|
58
56
|
```
|
|
59
57
|
|
|
60
|
-
|
|
58
|
+
**2) Simple subscriber implementing `PluginSubscriberInterface`:**
|
|
61
59
|
|
|
62
60
|
```typescript
|
|
63
61
|
import type { PluginSubscriberInterface } from 'pinia-plugin-subscription'
|
|
64
62
|
|
|
65
|
-
export const myStoreSubscriber = {
|
|
66
|
-
name: 'my-plugin'
|
|
63
|
+
export const myStoreSubscriber: PluginSubscriberInterface = {
|
|
64
|
+
name: 'my-plugin',
|
|
65
|
+
|
|
67
66
|
invoke: (context, debug) => {
|
|
68
|
-
|
|
67
|
+
// context contains `store`, `options`, `pinia`
|
|
68
|
+
console.log('store registered', context.store.$id)
|
|
69
|
+
|
|
70
|
+
return true
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
resetStoreCallback: (store) => {
|
|
74
|
+
console.log('store reset:', store.$id)
|
|
69
75
|
}
|
|
70
76
|
}
|
|
71
77
|
```
|
|
72
78
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
#### Debug mode
|
|
79
|
+
## Advanced Features
|
|
76
80
|
|
|
77
|
-
|
|
81
|
+
- **Debug mode:** Pass `true` as the second argument to `createPlugin` to enable detailed logging and plugin filtering.
|
|
82
|
+
- **Reset callbacks:** Define `resetStoreCallback` to run custom logic when a store is reset.
|
|
78
83
|
|
|
79
|
-
|
|
80
|
-
pinia.use(createPlugin([subscriber], ['my-plugin', 'external-plugin']))
|
|
81
|
-
```
|
|
84
|
+
## API Reference
|
|
82
85
|
|
|
83
|
-
|
|
86
|
+
### `createPlugin(subscribers: PluginSubscriber[], debug?: boolean): PiniaPlugin`
|
|
84
87
|
|
|
85
|
-
|
|
88
|
+
Creates and returns a Pinia plugin from the provided `subscribers`. Each subscriber is invoked when a store is registered.
|
|
86
89
|
|
|
87
|
-
|
|
88
|
-
const subscriber = {
|
|
89
|
-
invoke: (context, debug) => {
|
|
90
|
-
console.log('Store changed:', context.store.$state)
|
|
91
|
-
},
|
|
92
|
-
resetStoreCallback: (store) => {
|
|
93
|
-
console.log('Store reset:', store.$id)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
```
|
|
90
|
+
### `PluginSubscriberInterface`
|
|
97
91
|
|
|
98
|
-
|
|
92
|
+
An object with at least an `invoke(context: PiniaPluginContext, debug?: boolean)` method, plus optional properties:
|
|
99
93
|
|
|
100
|
-
|
|
94
|
+
- `resetStoreCallback?: (store?: any) => void`
|
|
95
|
+
- `storeOnActionSubscription?: { store, callback }` (getter)
|
|
96
|
+
- `storeMutationSubscription?: { store, callback }` (getter)
|
|
97
|
+
- `subscriptions?: Record<string, Function>` (plugin-specific subscription functions)
|
|
101
98
|
|
|
102
|
-
|
|
99
|
+
## The `PluginSubscriber` Abstract Class
|
|
103
100
|
|
|
104
|
-
|
|
101
|
+
The project provides an abstract `PluginSubscriber` implementation (see [src/plugins/pluginSubscriber.ts](src/plugins/pluginSubscriber.ts)) to simplify creating reusable subscribers.
|
|
105
102
|
|
|
106
|
-
|
|
103
|
+
**Typical usage:**
|
|
107
104
|
|
|
108
|
-
-
|
|
109
|
-
-
|
|
110
|
-
-
|
|
111
|
-
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
## The `PluginSubscriber` class (abstract)
|
|
116
|
-
|
|
117
|
-
The project provides an abstract `PluginSubscriber` implementation ([src/plugins/pluginSubscriber.ts](src/plugins/pluginSubscriber.ts)) that makes it easy to create reusable subscribers:
|
|
118
|
-
|
|
119
|
-
- Constructor: `new PluginSubscriber(pluginName: string, createInstanceFunction: CreateInstance)`
|
|
120
|
-
- 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:
|
|
121
|
-
- `subscriptions` (from `store.getSubscriptions()`)
|
|
122
|
-
- `storeMutationSubscription` (from `store.storeSubscribe`)
|
|
123
|
-
- `storeOnActionSubscription` (from `store.onAction`)
|
|
124
|
-
- optionally a `pluginCreated(store)` hook called after initialization
|
|
105
|
+
- The subscriber instantiates a `Store` (or subclass) via a factory (`MyStore.customizeStore`).
|
|
106
|
+
- The instance exposes:
|
|
107
|
+
- `subscriptions` (from `getSubscriptions()`)
|
|
108
|
+
- `storeMutationSubscription` (from `storeSubscribe`)
|
|
109
|
+
- `storeOnActionSubscription` (from `onAction`)
|
|
110
|
+
- optional `pluginCreated(store)` hook called after initialization
|
|
125
111
|
|
|
112
|
+
**Example:**
|
|
126
113
|
|
|
127
114
|
```typescript
|
|
128
|
-
import PluginSubscriber from '
|
|
115
|
+
import PluginSubscriber from 'pinia-plugin-subscription'
|
|
129
116
|
import StoreExtension from './src/extending-pinia-store/core/StoreExtension'
|
|
130
117
|
import { addStore } from './src/extending-pinia-store/plugins/stores'
|
|
131
118
|
|
|
@@ -139,40 +126,34 @@ class ExtendingStoreSubscriber extends PluginSubscriber<StoreExtension> {
|
|
|
139
126
|
export const extendingStoreSubscriber = new ExtendingStoreSubscriber()
|
|
140
127
|
```
|
|
141
128
|
|
|
142
|
-
|
|
129
|
+
## The `Store` Class — Summary
|
|
143
130
|
|
|
144
|
-
|
|
131
|
+
The `Store` class (see [src/core/Store.ts](src/core/Store.ts)) is a wrapper around a `PiniaStore` providing:
|
|
145
132
|
|
|
146
|
-
|
|
133
|
+
- **Properties:** `debug`, `options`, `state`, `store`.
|
|
134
|
+
- **Useful methods:**
|
|
135
|
+
- `addToState(name, value?)` — adds a property to store state and exposes it as a `Ref` when appropriate.
|
|
136
|
+
- `addSubscription(pluginName, subscription)` — registers a plugin-specific subscription function.
|
|
137
|
+
- `getSubscriptions()` — returns registered subscriptions.
|
|
138
|
+
- `storeSubscribe` (getter/setter) — factory for `store.$subscribe` ({ store, callback }).
|
|
139
|
+
- `onAction` (getter/setter) — factory for `onAction` ({ store, callback }).
|
|
140
|
+
- `static customizeStore(store, options, debug?)` — recommended factory for class instantiation.
|
|
141
|
+
- `debugLog(message, args)` — conditional logging.
|
|
142
|
+
- Helpers: `stateHas()`, `storeHas()`, `getValue()`.
|
|
147
143
|
|
|
148
|
-
|
|
149
|
-
- Useful methods:
|
|
150
|
-
- `addToState(name, value?)`: adds a property to the store state and exposes it as a `Ref` on the store when appropriate.
|
|
151
|
-
- `addSubscription(pluginName, subscription)`: registers a plugin-specific subscription function (accessible via `getSubscriptions`).
|
|
152
|
-
- `getSubscriptions()`: returns subscriptions registered with `addSubscription` (or `undefined`).
|
|
153
|
-
- `storeSubscribe` (getter/setter): exposes a mutation subscription callback (`store.$subscribe`) as a factory returning `{ store, callback }`.
|
|
154
|
-
- `onAction` (getter/setter): exposes a Pinia `onAction` callback in the same form.
|
|
155
|
-
- `static customizeStore(store, options, debug?)`: instantiate the class (or subclass) when `options.storeOptions` is present.
|
|
156
|
-
- `debugLog(message, args)`: conditional logging when `debug` is true.
|
|
157
|
-
- `hasDeniedFirstChar(property)`, `getOption(...)`, `getValue(value)`, `getStatePropertyValue(...)`.
|
|
158
|
-
- `isOptionApi()`: true when the store uses Pinia Options API.
|
|
144
|
+
## Testing
|
|
159
145
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
`Store.customizeStore(...)` is the recommended entry point used by subscribers to create store helper instances.
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
## Testing
|
|
146
|
+
This plugin is tested with Vitest. Coverage reports are available in the `coverage/` directory:
|
|
166
147
|
|
|
167
|
-
|
|
148
|
+
- Statements: **97.64%** (83/85)
|
|
149
|
+
- Branches: **86.53%** (45/52)
|
|
150
|
+
- Functions: **100%** (38/38)
|
|
151
|
+
- Lines: **97.59%** (81/83)
|
|
168
152
|
|
|
169
|
-
|
|
170
|
-
- Branches: **86.53%** (45/52)
|
|
171
|
-
- Functions: **100%** (38/38)
|
|
172
|
-
- Lines: **97.59%** (81/83)
|
|
153
|
+
## Contributing
|
|
173
154
|
|
|
174
|
-
|
|
155
|
+
Pull requests are welcome. Please respect the project's coding style and add tests for new features.
|
|
175
156
|
|
|
176
157
|
## License
|
|
177
158
|
|
|
178
|
-
MIT
|
|
159
|
+
MIT
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PiniaPlugin, PiniaPluginContext, Store as PiniaStore, StateTree } from "pinia"
|
|
1
|
+
import type { PiniaPlugin, PiniaPluginContext, Store as PiniaStore, StateTree, StoreDefinition } from "pinia"
|
|
2
2
|
import type { Console } from "./log"
|
|
3
3
|
import type {
|
|
4
4
|
PluginSubscriber as PluginSubscriberInterface,
|
|
@@ -8,7 +8,7 @@ import type {
|
|
|
8
8
|
StoreMutationSubscription,
|
|
9
9
|
StoreMutationSubscriptionCallback
|
|
10
10
|
} from "./plugin"
|
|
11
|
-
import type {
|
|
11
|
+
import type { StatePropertyValue, StoreOptions, StoreOptionsPropertyValue } from "./store"
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
declare module 'pinia-plugin-subscription' {
|
|
@@ -16,7 +16,7 @@ declare module 'pinia-plugin-subscription' {
|
|
|
16
16
|
|
|
17
17
|
export function createPlugin(subscribers: PluginSubscriberInterface[], debug?: string[]): PiniaPlugin;
|
|
18
18
|
|
|
19
|
-
export function defineAStore<TStore, TState>(id: string, storeDefinition: () => AnyObject, storeOptions?: StoreOptions):
|
|
19
|
+
export function defineAStore<TStore, TState>(id: string, storeDefinition: () => AnyObject, storeOptions?: StoreOptions): StoreDefinition & TState & TStore
|
|
20
20
|
|
|
21
21
|
export function isEmpty(value: any): boolean
|
|
22
22
|
|
|
@@ -131,5 +131,5 @@ export interface SearchCollectionCriteria {
|
|
|
131
131
|
[key: number | string | symbol]: boolean | number | string;
|
|
132
132
|
}
|
|
133
133
|
export { Console, LogType, StyleDefinition as ConsoleStyleDefinition, StyleDefinitionKeys as ConsoleStyleDefinitionKeys, StyleDefinitions as ConsoleStyleDefinitions } from './log';
|
|
134
|
-
export type { CustomStore,
|
|
134
|
+
export type { CustomStore, PluginStoreOptions, StatePropertyValue, StoreOptions, StoreOptionsPropertyValue } from "./store";
|
|
135
135
|
export type { PluginSubscriber as PluginSubscriberInterface, StoreMutationSubscriptionCallback, StoreOnActionCallbackParameters } from "./plugin";
|
package/dist/types/plugin.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export interface PluginSubscriber {
|
|
|
8
8
|
resetStoreCallback?: (store?: Store) => void;
|
|
9
9
|
storeOnActionSubscription?: StoreOnActionSubscription;
|
|
10
10
|
storeMutationSubscription?: StoreMutationSubscription;
|
|
11
|
-
subscriptions
|
|
11
|
+
subscriptions?: PluginSubscriptions;
|
|
12
12
|
}
|
|
13
13
|
export interface NativePiniaSubscriptionReturn<Callback> {
|
|
14
14
|
store: Store;
|
package/dist/types/store.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AnyObject } from ".";
|
|
2
2
|
import type { PiniaCustomProperties, Store, StoreDefinition } from "pinia";
|
|
3
3
|
import type { Ref } from "vue";
|
|
4
|
-
export type CustomStore<TStore, TState> = Store &
|
|
4
|
+
export type CustomStore<TStore, TState> = Store & TStore & TState & PiniaCustomProperties & StoreDefinition;
|
|
5
5
|
export type DefineAugmentedStore<TStore, TState> = (args?: any) => CustomStore<TStore, TState>;
|
|
6
6
|
export type ObjectBaseProperty = Record<string, OptionBaseProperty | OptionBaseProperty[]>;
|
|
7
7
|
export type OptionBaseProperty = string | number | boolean | null | undefined | object | Function;
|
|
@@ -12,4 +12,3 @@ export type StatePropertyValue = StdStatePropertyValue | Ref<StdStatePropertyVal
|
|
|
12
12
|
type StdStatePropertyValue = AnyObject | boolean | null | number | string | undefined;
|
|
13
13
|
export type StoreOptions = Record<string, StoreOptionsPropertyValue>;
|
|
14
14
|
export type StoreOptionsPropertyValue = OptionBaseProperty | OptionBaseProperty[] | ObjectBaseProperty | ObjectBaseProperty[];
|
|
15
|
-
export {};
|