@coaction/valtio 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,43 @@
1
+ # @coaction/valtio
2
+
3
+ ![Node CI](https://github.com/unadlib/coaction/workflows/Node%20CI/badge.svg)
4
+ [![npm](https://img.shields.io/npm/v/@coaction/valtio.svg)](https://www.npmjs.com/package/@coaction/valtio)
5
+ ![license](https://img.shields.io/npm/l/@coaction/valtio)
6
+
7
+ A Coaction integration tool for Valtio.
8
+
9
+ ## Installation
10
+
11
+ You can install it via npm, yarn or pnpm.
12
+
13
+ ```sh
14
+ npm install coaction @coaction/valtio valtio
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import { create } from 'coaction';
21
+ import { adapt, bindValtio, proxy } from '@coaction/valtio';
22
+
23
+ const state = proxy(
24
+ bindValtio({
25
+ count: 0,
26
+ increment() {
27
+ this.count += 1;
28
+ }
29
+ })
30
+ );
31
+
32
+ const store = create(() => adapt(state));
33
+ store.getState().increment();
34
+ ```
35
+
36
+ ## Limitations
37
+
38
+ - `@coaction/valtio` only supports binding a whole Valtio store.
39
+ - Coaction `Slices` mode is not supported in this adapter.
40
+
41
+ ## Documentation
42
+
43
+ You can find the documentation [here](https://github.com/unadlib/coaction).
@@ -0,0 +1,16 @@
1
+ export * from 'valtio/vanilla';
2
+ export { proxy } from 'valtio/vanilla';
3
+
4
+ interface BindValtio {
5
+ <T extends object>(target: T): T;
6
+ }
7
+ /**
8
+ * Bind a store to Valtio.
9
+ */
10
+ declare const bindValtio: BindValtio;
11
+ /**
12
+ * Adapt a Valtio store type to state type.
13
+ */
14
+ declare const adapt: <T extends object>(store: T) => T;
15
+
16
+ export { adapt, bindValtio };
@@ -0,0 +1,16 @@
1
+ export * from 'valtio/vanilla';
2
+ export { proxy } from 'valtio/vanilla';
3
+
4
+ interface BindValtio {
5
+ <T extends object>(target: T): T;
6
+ }
7
+ /**
8
+ * Bind a store to Valtio.
9
+ */
10
+ declare const bindValtio: BindValtio;
11
+ /**
12
+ * Adapt a Valtio store type to state type.
13
+ */
14
+ declare const adapt: <T extends object>(store: T) => T;
15
+
16
+ export { adapt, bindValtio };
package/dist/index.js ADDED
@@ -0,0 +1,85 @@
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
+ adapt: () => adapt,
25
+ bindValtio: () => bindValtio,
26
+ proxy: () => import_vanilla.proxy
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ adapt: () => adapt,
34
+ bindValtio: () => bindValtio,
35
+ proxy: () => import_vanilla.proxy
36
+ });
37
+ var import_mutability = require("mutability");
38
+ var import_coaction = require("coaction");
39
+ var import_vanilla = require("valtio/vanilla");
40
+ __reExport(src_exports, require("valtio/vanilla"));
41
+ var instancesMap = /* @__PURE__ */ new WeakMap();
42
+ var handleStore = (store, rawState, state, internal) => {
43
+ if (!internal.toMutableRaw) {
44
+ internal.toMutableRaw = (key) => instancesMap.get(key);
45
+ const getMutableState = () => internal.toMutableRaw?.(rawState) ?? rawState;
46
+ Object.assign(store, {
47
+ subscribe: (listener) => (0, import_vanilla.subscribe)(getMutableState(), listener)
48
+ });
49
+ store.apply = (state2 = store.getState(), patches) => {
50
+ if (!patches) {
51
+ Object.assign(store.getState(), state2);
52
+ return;
53
+ }
54
+ (0, import_mutability.apply)(state2, patches);
55
+ };
56
+ }
57
+ };
58
+ var bindValtio = (0, import_coaction.createBinder)({
59
+ handleStore,
60
+ handleState: (options) => {
61
+ const descriptors = Object.getOwnPropertyDescriptors(options);
62
+ const copyState = Object.defineProperties(
63
+ {},
64
+ descriptors
65
+ );
66
+ const rawState = Object.defineProperties({}, descriptors);
67
+ return {
68
+ copyState,
69
+ bind: (state) => {
70
+ instancesMap.set(rawState, state);
71
+ return rawState;
72
+ }
73
+ };
74
+ }
75
+ });
76
+ var adapt = (store) => store;
77
+
78
+ // index.ts
79
+ __reExport(index_exports, src_exports, module.exports);
80
+ // Annotate the CommonJS export names for ESM import in node:
81
+ 0 && (module.exports = {
82
+ adapt,
83
+ bindValtio,
84
+ proxy
85
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,82 @@
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
+ adapt: () => adapt,
23
+ bindValtio: () => bindValtio,
24
+ proxy: () => proxy
25
+ });
26
+
27
+ // src/index.ts
28
+ var src_exports = {};
29
+ __export(src_exports, {
30
+ adapt: () => adapt,
31
+ bindValtio: () => bindValtio,
32
+ proxy: () => proxy
33
+ });
34
+ __reExport(src_exports, vanilla_star);
35
+ import { apply } from "mutability";
36
+ import { createBinder } from "coaction";
37
+ import { proxy, subscribe } from "valtio/vanilla";
38
+ import * as vanilla_star from "valtio/vanilla";
39
+ var instancesMap = /* @__PURE__ */ new WeakMap();
40
+ var handleStore = (store, rawState, state, internal) => {
41
+ if (!internal.toMutableRaw) {
42
+ internal.toMutableRaw = (key) => instancesMap.get(key);
43
+ const getMutableState = () => internal.toMutableRaw?.(rawState) ?? rawState;
44
+ Object.assign(store, {
45
+ subscribe: (listener) => subscribe(getMutableState(), listener)
46
+ });
47
+ store.apply = (state2 = store.getState(), patches) => {
48
+ if (!patches) {
49
+ Object.assign(store.getState(), state2);
50
+ return;
51
+ }
52
+ apply(state2, patches);
53
+ };
54
+ }
55
+ };
56
+ var bindValtio = createBinder({
57
+ handleStore,
58
+ handleState: (options) => {
59
+ const descriptors = Object.getOwnPropertyDescriptors(options);
60
+ const copyState = Object.defineProperties(
61
+ {},
62
+ descriptors
63
+ );
64
+ const rawState = Object.defineProperties({}, descriptors);
65
+ return {
66
+ copyState,
67
+ bind: (state) => {
68
+ instancesMap.set(rawState, state);
69
+ return rawState;
70
+ }
71
+ };
72
+ }
73
+ });
74
+ var adapt = (store) => store;
75
+
76
+ // index.ts
77
+ __reExport(index_exports, src_exports);
78
+ export {
79
+ adapt,
80
+ bindValtio,
81
+ proxy
82
+ };
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "@coaction/valtio",
3
+ "version": "1.0.0",
4
+ "description": "A Coaction integration tool for Valtio",
5
+ "keywords": [
6
+ "state",
7
+ "coaction",
8
+ "valtio",
9
+ "proxy"
10
+ ],
11
+ "authors": [
12
+ "Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
13
+ ],
14
+ "homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-valtio#readme",
15
+ "license": "MIT",
16
+ "main": "dist/index.js",
17
+ "module": "dist/index.mjs",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.js",
23
+ "default": "./dist/index.mjs"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
27
+ "types": "dist/index.d.ts",
28
+ "sideEffects": false,
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/unadlib/coaction.git",
35
+ "directory": "packages/coaction-valtio"
36
+ },
37
+ "bugs": {
38
+ "url": "https://github.com/unadlib/coaction/issues"
39
+ },
40
+ "peerDependencies": {
41
+ "coaction": "^0.2.0",
42
+ "mutative": "^1.1.0",
43
+ "valtio": "^2.0.0"
44
+ },
45
+ "peerDependenciesMeta": {
46
+ "coaction": {
47
+ "optional": true
48
+ },
49
+ "mutative": {
50
+ "optional": true
51
+ },
52
+ "valtio": {
53
+ "optional": true
54
+ }
55
+ },
56
+ "devDependencies": {
57
+ "coaction": "^0.2.0",
58
+ "mutative": "^1.3.0",
59
+ "valtio": "^2.1.9"
60
+ },
61
+ "dependencies": {
62
+ "mutability": "^1.2.1"
63
+ },
64
+ "publishConfig": {
65
+ "access": "public",
66
+ "provenance": true
67
+ },
68
+ "author": "unadlib",
69
+ "scripts": {
70
+ "clean": "rm -rf dist",
71
+ "test": "vitest run test",
72
+ "build": "tsup index.ts --format cjs,esm --dts --clean --out-dir dist",
73
+ "dev": "tsup index.ts --format cjs,esm --dts --watch --out-dir dist"
74
+ }
75
+ }