@coaction/vue 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Michael Lin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # @coaction/vue
2
+
3
+ ![Node CI](https://github.com/unadlib/coaction/workflows/Node%20CI/badge.svg)
4
+ [![npm](https://img.shields.io/npm/v/@coaction/vue.svg)](https://www.npmjs.com/package/@coaction/vue)
5
+ ![license](https://img.shields.io/npm/l/@coaction/vue)
6
+
7
+ A Coaction integration tool for Vue
8
+
9
+ ## Installation
10
+
11
+ You can install it via npm, yarn or pnpm.
12
+
13
+ ```sh
14
+ npm install coaction @coaction/vue
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```jsx
20
+ import { create } from '@coaction/vue';
21
+
22
+ const useStore = create((set) => ({
23
+ count: 0,
24
+ increment() {
25
+ set((draft) => {
26
+ draft.count += 1;
27
+ });
28
+ }
29
+ }));
30
+
31
+ export default {
32
+ setup() {
33
+ const state = useStore();
34
+ const count = useStore((current) => current.count);
35
+ return {
36
+ state,
37
+ count
38
+ };
39
+ }
40
+ };
41
+ ```
42
+
43
+ Use `autoSelector` to receive computed refs for each field:
44
+
45
+ ```tsx
46
+ const selectors = useStore({ autoSelector: true });
47
+ selectors.increment();
48
+ console.log(selectors.count.value);
49
+ ```
50
+
51
+ ## Documentation
52
+
53
+ You can find the documentation [here](https://github.com/unadlib/coaction).
@@ -0,0 +1,34 @@
1
+ import { ISlices, Slice, StoreOptions, Store, SliceState, ClientStoreOptions, Asyncify } from 'coaction';
2
+ export * from 'coaction';
3
+ import { ComputedRef } from 'vue';
4
+
5
+ type SelectorOptions = {
6
+ autoSelector?: boolean;
7
+ };
8
+ type AutoSelector<T> = {
9
+ [K in keyof T]: T[K] extends (...args: any[]) => any ? T[K] : T[K] extends readonly any[] ? ComputedRef<T[K]> : T[K] extends object ? AutoSelector<T[K]> : ComputedRef<T[K]>;
10
+ };
11
+ type StoreReturn<T extends object> = Store<T> & {
12
+ <P>(selector: (state: T) => P): ComputedRef<P>;
13
+ (options: {
14
+ autoSelector: true;
15
+ }): AutoSelector<T>;
16
+ (options?: SelectorOptions): T;
17
+ };
18
+ type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & {
19
+ <P>(selector: (state: Asyncify<T, D>) => P): ComputedRef<P>;
20
+ (options: {
21
+ autoSelector: true;
22
+ }): AutoSelector<Asyncify<T, D>>;
23
+ (options?: SelectorOptions): Asyncify<T, D>;
24
+ };
25
+ type CreateState = ISlices | Record<string, Slice<any>>;
26
+ type Creator = {
27
+ <T extends Record<string, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
28
+ <T extends ISlices>(createState: Slice<T>, options?: StoreOptions<T>): StoreReturn<T>;
29
+ <T extends Record<string, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
30
+ <T extends ISlices>(createState: Slice<T>, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
31
+ };
32
+ declare const create: Creator;
33
+
34
+ export { type CreateState, type Creator, type StoreReturn, type StoreWithAsyncFunction, create };
@@ -0,0 +1,34 @@
1
+ import { ISlices, Slice, StoreOptions, Store, SliceState, ClientStoreOptions, Asyncify } from 'coaction';
2
+ export * from 'coaction';
3
+ import { ComputedRef } from 'vue';
4
+
5
+ type SelectorOptions = {
6
+ autoSelector?: boolean;
7
+ };
8
+ type AutoSelector<T> = {
9
+ [K in keyof T]: T[K] extends (...args: any[]) => any ? T[K] : T[K] extends readonly any[] ? ComputedRef<T[K]> : T[K] extends object ? AutoSelector<T[K]> : ComputedRef<T[K]>;
10
+ };
11
+ type StoreReturn<T extends object> = Store<T> & {
12
+ <P>(selector: (state: T) => P): ComputedRef<P>;
13
+ (options: {
14
+ autoSelector: true;
15
+ }): AutoSelector<T>;
16
+ (options?: SelectorOptions): T;
17
+ };
18
+ type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & {
19
+ <P>(selector: (state: Asyncify<T, D>) => P): ComputedRef<P>;
20
+ (options: {
21
+ autoSelector: true;
22
+ }): AutoSelector<Asyncify<T, D>>;
23
+ (options?: SelectorOptions): Asyncify<T, D>;
24
+ };
25
+ type CreateState = ISlices | Record<string, Slice<any>>;
26
+ type Creator = {
27
+ <T extends Record<string, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
28
+ <T extends ISlices>(createState: Slice<T>, options?: StoreOptions<T>): StoreReturn<T>;
29
+ <T extends Record<string, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
30
+ <T extends ISlices>(createState: Slice<T>, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
31
+ };
32
+ declare const create: Creator;
33
+
34
+ export { type CreateState, type Creator, type StoreReturn, type StoreWithAsyncFunction, create };
package/dist/index.js ADDED
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ create: () => create
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/index.ts
29
+ var src_exports = {};
30
+ __export(src_exports, {
31
+ create: () => create
32
+ });
33
+ var import_coaction = require("coaction");
34
+ var import_vue = require("vue");
35
+ __reExport(src_exports, require("coaction"));
36
+ var createStateProxy = (store, version) => new Proxy({}, {
37
+ get(_, key) {
38
+ version.value;
39
+ const state = store.getState();
40
+ const value = state[key];
41
+ if (typeof value === "function") {
42
+ return value.bind(store.getState());
43
+ }
44
+ return value;
45
+ },
46
+ has(_, key) {
47
+ version.value;
48
+ return key in store.getState();
49
+ },
50
+ ownKeys() {
51
+ version.value;
52
+ return Reflect.ownKeys(store.getState());
53
+ },
54
+ getOwnPropertyDescriptor(_, key) {
55
+ version.value;
56
+ const descriptor = Object.getOwnPropertyDescriptor(store.getState(), key);
57
+ if (!descriptor) {
58
+ return void 0;
59
+ }
60
+ return {
61
+ ...descriptor,
62
+ configurable: true
63
+ };
64
+ }
65
+ });
66
+ var createAutoSelector = (store, version) => {
67
+ const state = store.getState();
68
+ if (!store.isSliceStore) {
69
+ const autoSelector2 = {};
70
+ const descriptors = Object.getOwnPropertyDescriptors(state);
71
+ for (const key in descriptors) {
72
+ const descriptor = descriptors[key];
73
+ if (typeof descriptor.value === "function") {
74
+ autoSelector2[key] = descriptor.value.bind(state);
75
+ } else {
76
+ autoSelector2[key] = (0, import_vue.computed)(() => {
77
+ version.value;
78
+ return store.getState()[key];
79
+ });
80
+ }
81
+ }
82
+ return autoSelector2;
83
+ }
84
+ const autoSelector = {};
85
+ for (const sliceKey in state) {
86
+ const slice = state[sliceKey];
87
+ if (typeof slice !== "object" || slice === null) {
88
+ continue;
89
+ }
90
+ const sliceAutoSelector = {};
91
+ const descriptors = Object.getOwnPropertyDescriptors(slice);
92
+ for (const key in descriptors) {
93
+ const descriptor = descriptors[key];
94
+ if (typeof descriptor.value === "function") {
95
+ sliceAutoSelector[key] = descriptor.value.bind(slice);
96
+ } else {
97
+ sliceAutoSelector[key] = (0, import_vue.computed)(() => {
98
+ version.value;
99
+ return store.getState()[sliceKey][key];
100
+ });
101
+ }
102
+ }
103
+ autoSelector[sliceKey] = sliceAutoSelector;
104
+ }
105
+ return autoSelector;
106
+ };
107
+ var create = (createState, options) => {
108
+ const store = (0, import_coaction.create)(createState, options);
109
+ const version = (0, import_vue.ref)(0);
110
+ const unsubscribe = store.subscribe(() => {
111
+ version.value += 1;
112
+ });
113
+ const baseDestroy = store.destroy;
114
+ store.destroy = () => {
115
+ unsubscribe();
116
+ baseDestroy();
117
+ };
118
+ const stateProxy = createStateProxy(store, version);
119
+ let autoSelector;
120
+ const useStore = (0, import_coaction.wrapStore)(store, (selector) => {
121
+ if (typeof selector === "function") {
122
+ return (0, import_vue.computed)(() => {
123
+ version.value;
124
+ return selector(store.getState());
125
+ });
126
+ }
127
+ if (selector?.autoSelector) {
128
+ if (!autoSelector) {
129
+ autoSelector = createAutoSelector(store, version);
130
+ }
131
+ return autoSelector;
132
+ }
133
+ return stateProxy;
134
+ });
135
+ return useStore;
136
+ };
137
+
138
+ // index.ts
139
+ __reExport(index_exports, src_exports, module.exports);
140
+ // Annotate the CommonJS export names for ESM import in node:
141
+ 0 && (module.exports = {
142
+ create
143
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,140 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
+
19
+ // index.ts
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ create: () => create
23
+ });
24
+
25
+ // src/index.ts
26
+ var src_exports = {};
27
+ __export(src_exports, {
28
+ create: () => create
29
+ });
30
+ __reExport(src_exports, coaction_star);
31
+ import { create as createVanilla, wrapStore } from "coaction";
32
+ import { computed, ref } from "vue";
33
+ import * as coaction_star from "coaction";
34
+ var createStateProxy = (store, version) => new Proxy({}, {
35
+ get(_, key) {
36
+ version.value;
37
+ const state = store.getState();
38
+ const value = state[key];
39
+ if (typeof value === "function") {
40
+ return value.bind(store.getState());
41
+ }
42
+ return value;
43
+ },
44
+ has(_, key) {
45
+ version.value;
46
+ return key in store.getState();
47
+ },
48
+ ownKeys() {
49
+ version.value;
50
+ return Reflect.ownKeys(store.getState());
51
+ },
52
+ getOwnPropertyDescriptor(_, key) {
53
+ version.value;
54
+ const descriptor = Object.getOwnPropertyDescriptor(store.getState(), key);
55
+ if (!descriptor) {
56
+ return void 0;
57
+ }
58
+ return {
59
+ ...descriptor,
60
+ configurable: true
61
+ };
62
+ }
63
+ });
64
+ var createAutoSelector = (store, version) => {
65
+ const state = store.getState();
66
+ if (!store.isSliceStore) {
67
+ const autoSelector2 = {};
68
+ const descriptors = Object.getOwnPropertyDescriptors(state);
69
+ for (const key in descriptors) {
70
+ const descriptor = descriptors[key];
71
+ if (typeof descriptor.value === "function") {
72
+ autoSelector2[key] = descriptor.value.bind(state);
73
+ } else {
74
+ autoSelector2[key] = computed(() => {
75
+ version.value;
76
+ return store.getState()[key];
77
+ });
78
+ }
79
+ }
80
+ return autoSelector2;
81
+ }
82
+ const autoSelector = {};
83
+ for (const sliceKey in state) {
84
+ const slice = state[sliceKey];
85
+ if (typeof slice !== "object" || slice === null) {
86
+ continue;
87
+ }
88
+ const sliceAutoSelector = {};
89
+ const descriptors = Object.getOwnPropertyDescriptors(slice);
90
+ for (const key in descriptors) {
91
+ const descriptor = descriptors[key];
92
+ if (typeof descriptor.value === "function") {
93
+ sliceAutoSelector[key] = descriptor.value.bind(slice);
94
+ } else {
95
+ sliceAutoSelector[key] = computed(() => {
96
+ version.value;
97
+ return store.getState()[sliceKey][key];
98
+ });
99
+ }
100
+ }
101
+ autoSelector[sliceKey] = sliceAutoSelector;
102
+ }
103
+ return autoSelector;
104
+ };
105
+ var create = (createState, options) => {
106
+ const store = createVanilla(createState, options);
107
+ const version = ref(0);
108
+ const unsubscribe = store.subscribe(() => {
109
+ version.value += 1;
110
+ });
111
+ const baseDestroy = store.destroy;
112
+ store.destroy = () => {
113
+ unsubscribe();
114
+ baseDestroy();
115
+ };
116
+ const stateProxy = createStateProxy(store, version);
117
+ let autoSelector;
118
+ const useStore = wrapStore(store, (selector) => {
119
+ if (typeof selector === "function") {
120
+ return computed(() => {
121
+ version.value;
122
+ return selector(store.getState());
123
+ });
124
+ }
125
+ if (selector?.autoSelector) {
126
+ if (!autoSelector) {
127
+ autoSelector = createAutoSelector(store, version);
128
+ }
129
+ return autoSelector;
130
+ }
131
+ return stateProxy;
132
+ });
133
+ return useStore;
134
+ };
135
+
136
+ // index.ts
137
+ __reExport(index_exports, src_exports);
138
+ export {
139
+ create
140
+ };
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@coaction/vue",
3
+ "version": "1.0.0",
4
+ "description": "A Coaction integration tool for Vue",
5
+ "keywords": [
6
+ "coaction",
7
+ "vue",
8
+ "multithreading",
9
+ "mutative",
10
+ "data-transport"
11
+ ],
12
+ "authors": [
13
+ "Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
14
+ ],
15
+ "homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-vue#readme",
16
+ "license": "MIT",
17
+ "main": "dist/index.js",
18
+ "module": "dist/index.mjs",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.mjs",
23
+ "require": "./dist/index.js",
24
+ "default": "./dist/index.mjs"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "types": "dist/index.d.ts",
29
+ "sideEffects": false,
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/unadlib/coaction.git",
36
+ "directory": "packages/coaction-vue"
37
+ },
38
+ "bugs": {
39
+ "url": "https://github.com/unadlib/coaction/issues"
40
+ },
41
+ "peerDependencies": {
42
+ "coaction": "^0.2.0",
43
+ "vue": "^3.0.0"
44
+ },
45
+ "peerDependenciesMeta": {
46
+ "coaction": {
47
+ "optional": true
48
+ }
49
+ },
50
+ "devDependencies": {
51
+ "@testing-library/vue": "^8.1.0",
52
+ "coaction": "^0.2.0",
53
+ "vue": "^3.5.28"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public",
57
+ "provenance": true
58
+ },
59
+ "author": "unadlib",
60
+ "scripts": {
61
+ "clean": "rm -rf dist",
62
+ "test": "vitest run test",
63
+ "build": "tsup index.ts --format cjs,esm --dts --clean --out-dir dist",
64
+ "dev": "tsup index.ts --format cjs,esm --dts --watch --out-dir dist"
65
+ }
66
+ }