@coaction/svelte 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,39 @@
1
+ # @coaction/svelte
2
+
3
+ ![Node CI](https://github.com/unadlib/coaction/workflows/Node%20CI/badge.svg)
4
+ [![npm](https://img.shields.io/npm/v/@coaction/svelte.svg)](https://www.npmjs.com/package/@coaction/svelte)
5
+ ![license](https://img.shields.io/npm/l/@coaction/svelte)
6
+
7
+ A Coaction integration tool for Svelte.
8
+
9
+ ## Installation
10
+
11
+ You can install it via npm, yarn or pnpm.
12
+
13
+ ```sh
14
+ npm install coaction @coaction/svelte
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import { create } from '@coaction/svelte';
21
+
22
+ const store = create((set) => ({
23
+ count: 0,
24
+ increment() {
25
+ set((draft) => {
26
+ draft.count += 1;
27
+ });
28
+ }
29
+ }));
30
+
31
+ const count = store((state) => state.count);
32
+ count.subscribe((value) => {
33
+ console.log(value);
34
+ });
35
+ ```
36
+
37
+ ## Documentation
38
+
39
+ You can find the documentation [here](https://github.com/unadlib/coaction).
@@ -0,0 +1,36 @@
1
+ import { ISlices, Slice, StoreOptions, Store, SliceState, ClientStoreOptions, Asyncify } from 'coaction';
2
+ export * from 'coaction';
3
+
4
+ type Unsubscriber = () => void;
5
+ type Listener = () => void;
6
+ type Readable<T> = {
7
+ subscribe: (run: (value: T) => void, invalidate?: (value?: T) => void) => Unsubscriber;
8
+ };
9
+ type StoreReturn<T extends object> = Store<T> & {
10
+ (): T;
11
+ <P>(selector: (state: T) => P): Readable<P>;
12
+ subscribe: {
13
+ (listener: Listener): Unsubscriber;
14
+ (run: (value: T) => void, invalidate?: (value?: T) => void): Unsubscriber;
15
+ };
16
+ select: <P>(selector: (state: T) => P) => Readable<P>;
17
+ };
18
+ type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & {
19
+ (): Asyncify<T, D>;
20
+ <P>(selector: (state: Asyncify<T, D>) => P): Readable<P>;
21
+ subscribe: {
22
+ (listener: Listener): Unsubscriber;
23
+ (run: (value: Asyncify<T, D>) => void, invalidate?: (value?: Asyncify<T, D>) => void): Unsubscriber;
24
+ };
25
+ select: <P>(selector: (state: Asyncify<T, D>) => P) => Readable<P>;
26
+ };
27
+ type CreateState = ISlices | Record<string, Slice<any>>;
28
+ type Creator = {
29
+ <T extends Record<string, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
30
+ <T extends ISlices>(createState: Slice<T>, options?: StoreOptions<T>): StoreReturn<T>;
31
+ <T extends Record<string, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
32
+ <T extends ISlices>(createState: Slice<T>, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
33
+ };
34
+ declare const create: Creator;
35
+
36
+ export { type CreateState, type Creator, type StoreReturn, type StoreWithAsyncFunction, create };
@@ -0,0 +1,36 @@
1
+ import { ISlices, Slice, StoreOptions, Store, SliceState, ClientStoreOptions, Asyncify } from 'coaction';
2
+ export * from 'coaction';
3
+
4
+ type Unsubscriber = () => void;
5
+ type Listener = () => void;
6
+ type Readable<T> = {
7
+ subscribe: (run: (value: T) => void, invalidate?: (value?: T) => void) => Unsubscriber;
8
+ };
9
+ type StoreReturn<T extends object> = Store<T> & {
10
+ (): T;
11
+ <P>(selector: (state: T) => P): Readable<P>;
12
+ subscribe: {
13
+ (listener: Listener): Unsubscriber;
14
+ (run: (value: T) => void, invalidate?: (value?: T) => void): Unsubscriber;
15
+ };
16
+ select: <P>(selector: (state: T) => P) => Readable<P>;
17
+ };
18
+ type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & {
19
+ (): Asyncify<T, D>;
20
+ <P>(selector: (state: Asyncify<T, D>) => P): Readable<P>;
21
+ subscribe: {
22
+ (listener: Listener): Unsubscriber;
23
+ (run: (value: Asyncify<T, D>) => void, invalidate?: (value?: Asyncify<T, D>) => void): Unsubscriber;
24
+ };
25
+ select: <P>(selector: (state: Asyncify<T, D>) => P) => Readable<P>;
26
+ };
27
+ type CreateState = ISlices | Record<string, Slice<any>>;
28
+ type Creator = {
29
+ <T extends Record<string, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
30
+ <T extends ISlices>(createState: Slice<T>, options?: StoreOptions<T>): StoreReturn<T>;
31
+ <T extends Record<string, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
32
+ <T extends ISlices>(createState: Slice<T>, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
33
+ };
34
+ declare const create: Creator;
35
+
36
+ export { type CreateState, type Creator, type StoreReturn, type StoreWithAsyncFunction, create };
package/dist/index.js ADDED
@@ -0,0 +1,78 @@
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
+ __reExport(src_exports, require("coaction"));
35
+ var createReadable = (store, selector) => ({
36
+ subscribe(run) {
37
+ run(selector(store.getState()));
38
+ return store.subscribe(() => {
39
+ run(selector(store.getState()));
40
+ });
41
+ }
42
+ });
43
+ var create = (createState, options) => {
44
+ const store = (0, import_coaction.create)(createState, options);
45
+ const baseSubscribe = store.subscribe.bind(store);
46
+ function select(selector) {
47
+ return createReadable(store, selector);
48
+ }
49
+ const subscribe = ((listener, invalidate) => {
50
+ if (typeof invalidate === "function") {
51
+ invalidate();
52
+ }
53
+ if (typeof listener === "function" && listener.length > 0) {
54
+ listener(store.getState());
55
+ return baseSubscribe(() => {
56
+ listener(store.getState());
57
+ });
58
+ }
59
+ return baseSubscribe(listener);
60
+ });
61
+ Object.assign(store, {
62
+ subscribe,
63
+ select
64
+ });
65
+ return (0, import_coaction.wrapStore)(store, (selector) => {
66
+ if (typeof selector === "function") {
67
+ return select(selector);
68
+ }
69
+ return store.getState();
70
+ });
71
+ };
72
+
73
+ // index.ts
74
+ __reExport(index_exports, src_exports, module.exports);
75
+ // Annotate the CommonJS export names for ESM import in node:
76
+ 0 && (module.exports = {
77
+ create
78
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,75 @@
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 * as coaction_star from "coaction";
33
+ var createReadable = (store, selector) => ({
34
+ subscribe(run) {
35
+ run(selector(store.getState()));
36
+ return store.subscribe(() => {
37
+ run(selector(store.getState()));
38
+ });
39
+ }
40
+ });
41
+ var create = (createState, options) => {
42
+ const store = createVanilla(createState, options);
43
+ const baseSubscribe = store.subscribe.bind(store);
44
+ function select(selector) {
45
+ return createReadable(store, selector);
46
+ }
47
+ const subscribe = ((listener, invalidate) => {
48
+ if (typeof invalidate === "function") {
49
+ invalidate();
50
+ }
51
+ if (typeof listener === "function" && listener.length > 0) {
52
+ listener(store.getState());
53
+ return baseSubscribe(() => {
54
+ listener(store.getState());
55
+ });
56
+ }
57
+ return baseSubscribe(listener);
58
+ });
59
+ Object.assign(store, {
60
+ subscribe,
61
+ select
62
+ });
63
+ return wrapStore(store, (selector) => {
64
+ if (typeof selector === "function") {
65
+ return select(selector);
66
+ }
67
+ return store.getState();
68
+ });
69
+ };
70
+
71
+ // index.ts
72
+ __reExport(index_exports, src_exports);
73
+ export {
74
+ create
75
+ };
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@coaction/svelte",
3
+ "version": "1.0.0",
4
+ "description": "A Coaction integration tool for Svelte",
5
+ "keywords": [
6
+ "state",
7
+ "coaction",
8
+ "svelte",
9
+ "store"
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-svelte#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-svelte"
36
+ },
37
+ "bugs": {
38
+ "url": "https://github.com/unadlib/coaction/issues"
39
+ },
40
+ "peerDependencies": {
41
+ "coaction": "^0.2.0",
42
+ "svelte": "^4.0.0 || ^5.0.0"
43
+ },
44
+ "peerDependenciesMeta": {
45
+ "coaction": {
46
+ "optional": true
47
+ },
48
+ "svelte": {
49
+ "optional": true
50
+ }
51
+ },
52
+ "devDependencies": {
53
+ "coaction": "^0.2.0",
54
+ "svelte": "^5.43.11"
55
+ },
56
+ "publishConfig": {
57
+ "access": "public",
58
+ "provenance": true
59
+ },
60
+ "author": "unadlib",
61
+ "scripts": {
62
+ "clean": "rm -rf dist",
63
+ "test": "vitest run test",
64
+ "build": "tsup index.ts --format cjs,esm --dts --clean --out-dir dist",
65
+ "dev": "tsup index.ts --format cjs,esm --dts --watch --out-dir dist"
66
+ }
67
+ }