ccstate-vue 4.13.0 → 5.0.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/CHANGELOG.md +16 -0
- package/dist/index.cjs +5 -8
- package/dist/index.js +5 -8
- package/package.json +2 -2
- package/src/__tests__/get-set.test.ts +12 -14
- package/src/provider.ts +1 -2
- package/src/useGet.ts +6 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# ccstate-vue
|
|
2
2
|
|
|
3
|
+
## 5.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 2fdba09: feat: provide watch method to replace sub
|
|
8
|
+
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- 52c52fd: refactor: remove defaultStore
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [2fdba09]
|
|
16
|
+
- Updated dependencies [52c52fd]
|
|
17
|
+
- ccstate@5.0.0
|
|
18
|
+
|
|
3
19
|
## 4.13.0
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var vue = require('vue');
|
|
4
|
-
var ccstate = require('ccstate');
|
|
5
4
|
|
|
6
5
|
var StoreKey = Symbol('ccstate-vue-store');
|
|
7
6
|
var provideStore = function provideStore(store) {
|
|
@@ -9,19 +8,17 @@ var provideStore = function provideStore(store) {
|
|
|
9
8
|
};
|
|
10
9
|
var useStore = function useStore() {
|
|
11
10
|
return vue.inject(StoreKey, function () {
|
|
12
|
-
|
|
11
|
+
throw new Error('useStore must be used within a provideStore');
|
|
13
12
|
}, true);
|
|
14
13
|
};
|
|
15
14
|
|
|
16
15
|
function useGet(atom) {
|
|
17
16
|
var store = useStore();
|
|
18
|
-
var
|
|
19
|
-
var vueState = vue.shallowRef(initialValue);
|
|
17
|
+
var vueState = vue.shallowRef(store.get(atom));
|
|
20
18
|
var controller = new AbortController();
|
|
21
|
-
store.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}), {
|
|
19
|
+
store.watch(function (get) {
|
|
20
|
+
vueState.value = get(atom);
|
|
21
|
+
}, {
|
|
25
22
|
signal: controller.signal
|
|
26
23
|
});
|
|
27
24
|
if (vue.getCurrentInstance()) {
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { provide, inject, shallowRef, getCurrentInstance, onScopeDispose, shallowReadonly } from 'vue';
|
|
2
|
-
import { getDefaultStore, command } from 'ccstate';
|
|
3
2
|
|
|
4
3
|
var StoreKey = Symbol('ccstate-vue-store');
|
|
5
4
|
var provideStore = function provideStore(store) {
|
|
@@ -7,19 +6,17 @@ var provideStore = function provideStore(store) {
|
|
|
7
6
|
};
|
|
8
7
|
var useStore = function useStore() {
|
|
9
8
|
return inject(StoreKey, function () {
|
|
10
|
-
|
|
9
|
+
throw new Error('useStore must be used within a provideStore');
|
|
11
10
|
}, true);
|
|
12
11
|
};
|
|
13
12
|
|
|
14
13
|
function useGet(atom) {
|
|
15
14
|
var store = useStore();
|
|
16
|
-
var
|
|
17
|
-
var vueState = shallowRef(initialValue);
|
|
15
|
+
var vueState = shallowRef(store.get(atom));
|
|
18
16
|
var controller = new AbortController();
|
|
19
|
-
store.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}), {
|
|
17
|
+
store.watch(function (get) {
|
|
18
|
+
vueState.value = get(atom);
|
|
19
|
+
}, {
|
|
23
20
|
signal: controller.signal
|
|
24
21
|
});
|
|
25
22
|
if (getCurrentInstance()) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ccstate-vue",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "CCState Vue Hooks",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"vue": ">=3.2.0"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"ccstate": "^
|
|
23
|
+
"ccstate": "^5.0.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@babel/preset-env": "^7.26.0",
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import '@testing-library/jest-dom/vitest';
|
|
3
3
|
import { fireEvent, render, cleanup, screen } from '@testing-library/vue';
|
|
4
4
|
import { afterEach, expect, it } from 'vitest';
|
|
5
|
-
import { command, createStore,
|
|
5
|
+
import { command, createStore, state } from 'ccstate';
|
|
6
6
|
import { provideStore } from '../provider';
|
|
7
7
|
import { useGet, useSet } from '..';
|
|
8
8
|
|
|
@@ -87,26 +87,24 @@ it('call command by useSet', async () => {
|
|
|
87
87
|
expect(screen.getByText('Times clicked: 30')).toBeInTheDocument();
|
|
88
88
|
});
|
|
89
89
|
|
|
90
|
-
it('should
|
|
90
|
+
it('should throw error if no store provide', () => {
|
|
91
91
|
const count$ = state(0);
|
|
92
|
-
|
|
92
|
+
const store = createStore();
|
|
93
|
+
store.set(count$, 10);
|
|
93
94
|
|
|
94
95
|
const Component = {
|
|
95
96
|
setup() {
|
|
96
|
-
|
|
97
|
-
return { count };
|
|
97
|
+
useGet(count$);
|
|
98
98
|
},
|
|
99
99
|
template: `
|
|
100
|
-
<div>
|
|
101
|
-
<p>{{ count }}</p>
|
|
102
|
-
</div>
|
|
100
|
+
<div></div>
|
|
103
101
|
`,
|
|
104
102
|
};
|
|
105
103
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
104
|
+
expect(() => {
|
|
105
|
+
render({
|
|
106
|
+
components: { Component },
|
|
107
|
+
template: `<div><Component /></div>`,
|
|
108
|
+
});
|
|
109
|
+
}).toThrowError('useStore must be used within a provideStore');
|
|
112
110
|
});
|
package/src/provider.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { inject, provide, type InjectionKey } from 'vue';
|
|
2
|
-
import { getDefaultStore } from 'ccstate';
|
|
3
2
|
import type { Store } from 'ccstate';
|
|
4
3
|
|
|
5
4
|
export const StoreKey = Symbol('ccstate-vue-store') as InjectionKey<Store>;
|
|
@@ -12,7 +11,7 @@ export const useStore = (): Store => {
|
|
|
12
11
|
return inject(
|
|
13
12
|
StoreKey,
|
|
14
13
|
() => {
|
|
15
|
-
|
|
14
|
+
throw new Error('useStore must be used within a provideStore');
|
|
16
15
|
},
|
|
17
16
|
true,
|
|
18
17
|
);
|
package/src/useGet.ts
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
import { getCurrentInstance, onScopeDispose, shallowReadonly, shallowRef, type ShallowRef } from 'vue';
|
|
2
2
|
import { useStore } from './provider';
|
|
3
|
-
import {
|
|
3
|
+
import { type Computed, type State } from 'ccstate';
|
|
4
4
|
|
|
5
5
|
export function useGet<Value>(atom: Computed<Value> | State<Value>): Readonly<ShallowRef<Value>> {
|
|
6
6
|
const store = useStore();
|
|
7
|
-
const initialValue = store.get(atom);
|
|
8
7
|
|
|
9
|
-
const vueState = shallowRef(
|
|
8
|
+
const vueState = shallowRef(store.get(atom));
|
|
10
9
|
|
|
11
10
|
const controller = new AbortController();
|
|
12
|
-
store.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
vueState.value = nextValue;
|
|
17
|
-
}),
|
|
11
|
+
store.watch(
|
|
12
|
+
(get) => {
|
|
13
|
+
vueState.value = get(atom);
|
|
14
|
+
},
|
|
18
15
|
{
|
|
19
16
|
signal: controller.signal,
|
|
20
17
|
},
|