@limitlesspc/konbini 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) 2026 limitlesspc
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,5 @@
1
+ <h1 align="center">konbini</h1>
2
+ <p align="center">Convenient stores</p>
3
+ <p align="center"><sup>コンビニ</sup></p>
4
+
5
+ Simple little observables that work well in Svelte
package/dist/index.cjs ADDED
@@ -0,0 +1,75 @@
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ computed: () => computed,
24
+ konbini: () => konbini
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var computationStack = [];
28
+ function konbini(value) {
29
+ const subscribers = [];
30
+ const store = (...args) => {
31
+ if (!args.length) {
32
+ const outerCompute = computationStack.at(-1);
33
+ if (outerCompute) {
34
+ subscribers.push(outerCompute);
35
+ }
36
+ return value;
37
+ }
38
+ const newValue = args[0];
39
+ if (newValue !== value) {
40
+ value = args[0];
41
+ for (const subscriber of subscribers) {
42
+ subscriber(value);
43
+ }
44
+ }
45
+ return value;
46
+ };
47
+ store.set = (newValue) => store(newValue);
48
+ store.subscribe = (subscriber) => {
49
+ subscriber(value);
50
+ subscribers.push(subscriber);
51
+ return () => {
52
+ const index = subscribers.indexOf(subscriber);
53
+ if (index !== -1) {
54
+ subscribers.splice(index, 1);
55
+ }
56
+ };
57
+ };
58
+ return store;
59
+ }
60
+ function computed(fn) {
61
+ const store = konbini();
62
+ function compute() {
63
+ const result = fn();
64
+ store(result);
65
+ }
66
+ computationStack.push(compute);
67
+ compute();
68
+ computationStack.pop();
69
+ return store;
70
+ }
71
+ // Annotate the CommonJS export names for ESM import in node:
72
+ 0 && (module.exports = {
73
+ computed,
74
+ konbini
75
+ });
@@ -0,0 +1,64 @@
1
+ /**
2
+ * @module konbini
3
+ * A simple single-like reactive store that can be used in Svelte
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { konbini, computed } from "@limitlesspc/konbini";
8
+ *
9
+ * const count = konbini(0);
10
+ * const doubled = computed(() => count() * 2);
11
+ */
12
+ /** Called when the store's value changes */
13
+ type Subscriber<T> = (newValue: T, oldValue?: T) => any;
14
+ /** Stop listening for changes to a store's value */
15
+ type Unsubscriber = () => void;
16
+ /**
17
+ * A store that can be subscribed to
18
+ *
19
+ * This follows the [Svelte store contract](https://svelte.dev/docs/svelte/stores#Store-contract) so it can be prefixed a `$` in Svelte components the same way Svelte stores can.
20
+ */
21
+ interface Konbini<T> {
22
+ /** Get the value of the store */
23
+ (): T;
24
+ /** Set the value of the store */
25
+ (newValue: T): T;
26
+ /** Set the value of the store */
27
+ set(newValue: T): T;
28
+ /**
29
+ * Listen for changes to the store's value.
30
+ * @param subscriber called when the store's value changes
31
+ * @returns a function that can be called to stop listening for changes
32
+ */
33
+ subscribe(subscriber: Subscriber<T>): Unsubscriber;
34
+ }
35
+ /**
36
+ * Create a new store.
37
+ * @param value The initial value of the store
38
+ * @returns A store
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * const count = konbini(0);
43
+ * count() // 0
44
+ * count(2);
45
+ * count() // 2
46
+ * ```
47
+ */
48
+ declare function konbini<T>(value?: T): Konbini<T>;
49
+ /**
50
+ * Creates a computed store, where its value is derived from other stores
51
+ * @param executor A function that returns the value of the store
52
+ * @returns A store
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const count = konbini(0);
57
+ * const doubled = computed(() => count() * 2);
58
+ * count(2);
59
+ * doubled() // 4
60
+ * ```
61
+ */
62
+ declare function computed<T>(fn: () => T): Konbini<T>;
63
+
64
+ export { type Konbini, type Subscriber, type Unsubscriber, computed, konbini };
@@ -0,0 +1,64 @@
1
+ /**
2
+ * @module konbini
3
+ * A simple single-like reactive store that can be used in Svelte
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { konbini, computed } from "@limitlesspc/konbini";
8
+ *
9
+ * const count = konbini(0);
10
+ * const doubled = computed(() => count() * 2);
11
+ */
12
+ /** Called when the store's value changes */
13
+ type Subscriber<T> = (newValue: T, oldValue?: T) => any;
14
+ /** Stop listening for changes to a store's value */
15
+ type Unsubscriber = () => void;
16
+ /**
17
+ * A store that can be subscribed to
18
+ *
19
+ * This follows the [Svelte store contract](https://svelte.dev/docs/svelte/stores#Store-contract) so it can be prefixed a `$` in Svelte components the same way Svelte stores can.
20
+ */
21
+ interface Konbini<T> {
22
+ /** Get the value of the store */
23
+ (): T;
24
+ /** Set the value of the store */
25
+ (newValue: T): T;
26
+ /** Set the value of the store */
27
+ set(newValue: T): T;
28
+ /**
29
+ * Listen for changes to the store's value.
30
+ * @param subscriber called when the store's value changes
31
+ * @returns a function that can be called to stop listening for changes
32
+ */
33
+ subscribe(subscriber: Subscriber<T>): Unsubscriber;
34
+ }
35
+ /**
36
+ * Create a new store.
37
+ * @param value The initial value of the store
38
+ * @returns A store
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * const count = konbini(0);
43
+ * count() // 0
44
+ * count(2);
45
+ * count() // 2
46
+ * ```
47
+ */
48
+ declare function konbini<T>(value?: T): Konbini<T>;
49
+ /**
50
+ * Creates a computed store, where its value is derived from other stores
51
+ * @param executor A function that returns the value of the store
52
+ * @returns A store
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const count = konbini(0);
57
+ * const doubled = computed(() => count() * 2);
58
+ * count(2);
59
+ * doubled() // 4
60
+ * ```
61
+ */
62
+ declare function computed<T>(fn: () => T): Konbini<T>;
63
+
64
+ export { type Konbini, type Subscriber, type Unsubscriber, computed, konbini };
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ // index.ts
2
+ var computationStack = [];
3
+ function konbini(value) {
4
+ const subscribers = [];
5
+ const store = (...args) => {
6
+ if (!args.length) {
7
+ const outerCompute = computationStack.at(-1);
8
+ if (outerCompute) {
9
+ subscribers.push(outerCompute);
10
+ }
11
+ return value;
12
+ }
13
+ const newValue = args[0];
14
+ if (newValue !== value) {
15
+ value = args[0];
16
+ for (const subscriber of subscribers) {
17
+ subscriber(value);
18
+ }
19
+ }
20
+ return value;
21
+ };
22
+ store.set = (newValue) => store(newValue);
23
+ store.subscribe = (subscriber) => {
24
+ subscriber(value);
25
+ subscribers.push(subscriber);
26
+ return () => {
27
+ const index = subscribers.indexOf(subscriber);
28
+ if (index !== -1) {
29
+ subscribers.splice(index, 1);
30
+ }
31
+ };
32
+ };
33
+ return store;
34
+ }
35
+ function computed(fn) {
36
+ const store = konbini();
37
+ function compute() {
38
+ const result = fn();
39
+ store(result);
40
+ }
41
+ computationStack.push(compute);
42
+ compute();
43
+ computationStack.pop();
44
+ return store;
45
+ }
46
+ export {
47
+ computed,
48
+ konbini
49
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@limitlesspc/konbini",
3
+ "type": "module",
4
+ "version": "1.0.0",
5
+ "description": "Convenient stores",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/limitlesspc/konbini.git"
10
+ },
11
+ "exports": {
12
+ "./package.json": "./package.json",
13
+ ".": {
14
+ "import": "./dist/index.js",
15
+ "default": "./dist/index.cjs"
16
+ }
17
+ },
18
+ "main": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "prettier": "@limitlesspc/prettier-config",
24
+ "devDependencies": {
25
+ "@limitlesspc/prettier-config": "^1.2.1",
26
+ "tsup": "^8.3.5",
27
+ "typescript": "^5.6.3",
28
+ "vitest": "^4.0.18"
29
+ },
30
+ "scripts": {
31
+ "test": "vitest",
32
+ "build": "tsup",
33
+ "fmt": "prettier . --write"
34
+ }
35
+ }