@hoosk/minisignals-vue 0.0.0-stage → 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/README.md +85 -2
- package/dist/index.cjs +111 -0
- package/dist/index.d.cts +52 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +80 -0
- package/package.json +52 -4
package/README.md
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hoosk/minisignals-vue
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Vue 3 adapter for `@hoosk/minisignals`.
|
|
4
|
+
|
|
5
|
+
This package exposes minisignals values as native Vue refs, so they auto-unwrap in templates, work with `v-model`, `watch`, and `computed`, and are cleaned up automatically with the component or `effectScope`.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @hoosk/minisignals @hoosk/minisignals-vue
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Basic Usage
|
|
14
|
+
|
|
15
|
+
```vue
|
|
16
|
+
<script setup lang="ts">
|
|
17
|
+
import { useSignal } from '@hoosk/minisignals-vue';
|
|
18
|
+
|
|
19
|
+
const count = useSignal(0);
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<template>
|
|
23
|
+
<p>Count: {{ count }}</p>
|
|
24
|
+
<button @click="count++">Increment</button>
|
|
25
|
+
</template>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## API
|
|
29
|
+
|
|
30
|
+
### `useSignal(initialValue): Ref<T>`
|
|
31
|
+
|
|
32
|
+
Creates a local signal and returns a writable Vue ref backed by it. Usable directly with `v-model`.
|
|
33
|
+
|
|
34
|
+
### `useSignalRef(sig): Ref<T>`
|
|
35
|
+
|
|
36
|
+
Bridges an existing `Signal<T>` to a writable ref. Useful when the signal lives in a store outside the component:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { signal } from '@hoosk/minisignals';
|
|
40
|
+
import { useSignalRef } from '@hoosk/minisignals-vue';
|
|
41
|
+
|
|
42
|
+
export const count = signal(0);
|
|
43
|
+
|
|
44
|
+
// inside setup()
|
|
45
|
+
const countRef = useSignalRef(count);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### `useSignalValue(sig): Readonly<Ref<T>>`
|
|
49
|
+
|
|
50
|
+
Subscribes to any `Signal` or `Computed` and returns a read-only ref.
|
|
51
|
+
|
|
52
|
+
### `useComputed(fn): Readonly<Ref<T>>`
|
|
53
|
+
|
|
54
|
+
Creates a computed signal and returns a read-only ref. The computed instance is disposed automatically when the component or `effectScope` stops.
|
|
55
|
+
|
|
56
|
+
```vue
|
|
57
|
+
<script setup lang="ts">
|
|
58
|
+
import { signal } from '@hoosk/minisignals';
|
|
59
|
+
import { useComputed } from '@hoosk/minisignals-vue';
|
|
60
|
+
|
|
61
|
+
const price = signal(100);
|
|
62
|
+
const quantity = signal(2);
|
|
63
|
+
const total = useComputed(() => price.value * quantity.value);
|
|
64
|
+
</script>
|
|
65
|
+
|
|
66
|
+
<template>
|
|
67
|
+
<p>Total: {{ total }}</p>
|
|
68
|
+
</template>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### `useSignalEffect(fn): () => void`
|
|
72
|
+
|
|
73
|
+
Runs a core `effect` bound to the Vue lifecycle. The callback may return a cleanup function, which runs before each re-execution and when the scope stops. Returns a manual stop function.
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { useSignalEffect } from '@hoosk/minisignals-vue';
|
|
77
|
+
|
|
78
|
+
useSignalEffect(() => {
|
|
79
|
+
document.title = `Count: ${count.value}`;
|
|
80
|
+
return () => console.log('cleanup');
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
All composables register their teardown through `onScopeDispose`, so they also work inside `effectScope` outside of components.
|
|
85
|
+
|
|
86
|
+
For full documentation, please refer to the [main repository README](https://github.com/Hoosk/minisignals#readme).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
useComputed: () => useComputed,
|
|
24
|
+
useSignal: () => useSignal,
|
|
25
|
+
useSignalEffect: () => useSignalEffect,
|
|
26
|
+
useSignalRef: () => useSignalRef,
|
|
27
|
+
useSignalValue: () => useSignalValue
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
|
|
31
|
+
// src/useSignalValue.ts
|
|
32
|
+
var import_vue2 = require("vue");
|
|
33
|
+
var import_minisignals2 = require("@hoosk/minisignals");
|
|
34
|
+
|
|
35
|
+
// src/bridge.ts
|
|
36
|
+
var import_vue = require("vue");
|
|
37
|
+
var import_minisignals = require("@hoosk/minisignals");
|
|
38
|
+
function disposeOnScope(stop) {
|
|
39
|
+
if ((0, import_vue.getCurrentScope)()) (0, import_vue.onScopeDispose)(stop);
|
|
40
|
+
}
|
|
41
|
+
function signalToRef(sig) {
|
|
42
|
+
return (0, import_vue.customRef)((track, trigger) => {
|
|
43
|
+
const stop = (0, import_minisignals.effect)(() => {
|
|
44
|
+
sig.value;
|
|
45
|
+
trigger();
|
|
46
|
+
});
|
|
47
|
+
disposeOnScope(stop);
|
|
48
|
+
return {
|
|
49
|
+
get() {
|
|
50
|
+
track();
|
|
51
|
+
return sig.value;
|
|
52
|
+
},
|
|
53
|
+
set(value) {
|
|
54
|
+
sig.value = value;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/useSignalValue.ts
|
|
61
|
+
function useSignalValue(sig) {
|
|
62
|
+
const ref = (0, import_vue2.customRef)((track, trigger) => {
|
|
63
|
+
const stop = (0, import_minisignals2.effect)(() => {
|
|
64
|
+
sig.value;
|
|
65
|
+
trigger();
|
|
66
|
+
});
|
|
67
|
+
disposeOnScope(stop);
|
|
68
|
+
return {
|
|
69
|
+
get() {
|
|
70
|
+
track();
|
|
71
|
+
return sig.value;
|
|
72
|
+
},
|
|
73
|
+
set() {
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
return ref;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/useSignal.ts
|
|
81
|
+
var import_minisignals3 = require("@hoosk/minisignals");
|
|
82
|
+
function useSignal(initialValue) {
|
|
83
|
+
return useSignalRef((0, import_minisignals3.signal)(initialValue));
|
|
84
|
+
}
|
|
85
|
+
function useSignalRef(sig) {
|
|
86
|
+
return signalToRef(sig);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/useComputed.ts
|
|
90
|
+
var import_minisignals4 = require("@hoosk/minisignals");
|
|
91
|
+
function useComputed(fn) {
|
|
92
|
+
const derived = (0, import_minisignals4.computed)(fn);
|
|
93
|
+
disposeOnScope(() => derived.dispose());
|
|
94
|
+
return useSignalValue(derived);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/useSignalEffect.ts
|
|
98
|
+
var import_minisignals5 = require("@hoosk/minisignals");
|
|
99
|
+
function useSignalEffect(fn) {
|
|
100
|
+
const stop = (0, import_minisignals5.effect)(fn);
|
|
101
|
+
disposeOnScope(stop);
|
|
102
|
+
return stop;
|
|
103
|
+
}
|
|
104
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
105
|
+
0 && (module.exports = {
|
|
106
|
+
useComputed,
|
|
107
|
+
useSignal,
|
|
108
|
+
useSignalEffect,
|
|
109
|
+
useSignalRef,
|
|
110
|
+
useSignalValue
|
|
111
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
import { ReadonlySignal, Signal } from '@hoosk/minisignals';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Subscribes to a Signal or Computed and exposes its value as a read-only Vue
|
|
6
|
+
* Ref. The subscription is torn down automatically when the component (or
|
|
7
|
+
* effectScope) is disposed.
|
|
8
|
+
*
|
|
9
|
+
* @param sig Any ReadonlySignal (Signal or Computed) to observe.
|
|
10
|
+
* @returns A read-only Ref that updates with the signal.
|
|
11
|
+
*/
|
|
12
|
+
declare function useSignalValue<T>(sig: ReadonlySignal<T>): Readonly<Ref<T>>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates a local signal and exposes it as a writable Vue Ref. The ref can be
|
|
16
|
+
* used directly in templates (auto-unwrapping) and with `v-model`.
|
|
17
|
+
*
|
|
18
|
+
* @param initialValue The starting value of the signal.
|
|
19
|
+
* @returns A writable Ref backed by a stable Signal.
|
|
20
|
+
*/
|
|
21
|
+
declare function useSignal<T>(initialValue: T): Ref<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Bridges an existing Signal to a writable Vue Ref.
|
|
24
|
+
*/
|
|
25
|
+
declare function useSignalRef<T>(sig: Signal<T>): Ref<T>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates a computed signal derived from other signals and exposes it as a
|
|
29
|
+
* read-only Vue Ref.
|
|
30
|
+
*
|
|
31
|
+
* The computed instance is disposed automatically when the component or
|
|
32
|
+
* effectScope is torn down, preventing source signals from retaining
|
|
33
|
+
* subscriptions to unmounted components.
|
|
34
|
+
*
|
|
35
|
+
* @param fn The derivation function.
|
|
36
|
+
* @returns A read-only Ref that updates when dependencies change.
|
|
37
|
+
*/
|
|
38
|
+
declare function useComputed<T>(fn: () => T): Readonly<Ref<T>>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Runs a reactive effect tied to the component's lifecycle.
|
|
42
|
+
*
|
|
43
|
+
* The effect is stopped automatically when the component (or effectScope) is
|
|
44
|
+
* disposed. The callback may return a cleanup function, which runs before each
|
|
45
|
+
* re-execution and when the effect is stopped.
|
|
46
|
+
*
|
|
47
|
+
* @param fn The effect callback.
|
|
48
|
+
* @returns A stop function for manual teardown.
|
|
49
|
+
*/
|
|
50
|
+
declare function useSignalEffect(fn: () => void | (() => void)): () => void;
|
|
51
|
+
|
|
52
|
+
export { useComputed, useSignal, useSignalEffect, useSignalRef, useSignalValue };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
import { ReadonlySignal, Signal } from '@hoosk/minisignals';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Subscribes to a Signal or Computed and exposes its value as a read-only Vue
|
|
6
|
+
* Ref. The subscription is torn down automatically when the component (or
|
|
7
|
+
* effectScope) is disposed.
|
|
8
|
+
*
|
|
9
|
+
* @param sig Any ReadonlySignal (Signal or Computed) to observe.
|
|
10
|
+
* @returns A read-only Ref that updates with the signal.
|
|
11
|
+
*/
|
|
12
|
+
declare function useSignalValue<T>(sig: ReadonlySignal<T>): Readonly<Ref<T>>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates a local signal and exposes it as a writable Vue Ref. The ref can be
|
|
16
|
+
* used directly in templates (auto-unwrapping) and with `v-model`.
|
|
17
|
+
*
|
|
18
|
+
* @param initialValue The starting value of the signal.
|
|
19
|
+
* @returns A writable Ref backed by a stable Signal.
|
|
20
|
+
*/
|
|
21
|
+
declare function useSignal<T>(initialValue: T): Ref<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Bridges an existing Signal to a writable Vue Ref.
|
|
24
|
+
*/
|
|
25
|
+
declare function useSignalRef<T>(sig: Signal<T>): Ref<T>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates a computed signal derived from other signals and exposes it as a
|
|
29
|
+
* read-only Vue Ref.
|
|
30
|
+
*
|
|
31
|
+
* The computed instance is disposed automatically when the component or
|
|
32
|
+
* effectScope is torn down, preventing source signals from retaining
|
|
33
|
+
* subscriptions to unmounted components.
|
|
34
|
+
*
|
|
35
|
+
* @param fn The derivation function.
|
|
36
|
+
* @returns A read-only Ref that updates when dependencies change.
|
|
37
|
+
*/
|
|
38
|
+
declare function useComputed<T>(fn: () => T): Readonly<Ref<T>>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Runs a reactive effect tied to the component's lifecycle.
|
|
42
|
+
*
|
|
43
|
+
* The effect is stopped automatically when the component (or effectScope) is
|
|
44
|
+
* disposed. The callback may return a cleanup function, which runs before each
|
|
45
|
+
* re-execution and when the effect is stopped.
|
|
46
|
+
*
|
|
47
|
+
* @param fn The effect callback.
|
|
48
|
+
* @returns A stop function for manual teardown.
|
|
49
|
+
*/
|
|
50
|
+
declare function useSignalEffect(fn: () => void | (() => void)): () => void;
|
|
51
|
+
|
|
52
|
+
export { useComputed, useSignal, useSignalEffect, useSignalRef, useSignalValue };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// src/useSignalValue.ts
|
|
2
|
+
import { customRef as customRef2 } from "vue";
|
|
3
|
+
import { effect as effect2 } from "@hoosk/minisignals";
|
|
4
|
+
|
|
5
|
+
// src/bridge.ts
|
|
6
|
+
import { customRef, getCurrentScope, onScopeDispose } from "vue";
|
|
7
|
+
import { effect } from "@hoosk/minisignals";
|
|
8
|
+
function disposeOnScope(stop) {
|
|
9
|
+
if (getCurrentScope()) onScopeDispose(stop);
|
|
10
|
+
}
|
|
11
|
+
function signalToRef(sig) {
|
|
12
|
+
return customRef((track, trigger) => {
|
|
13
|
+
const stop = effect(() => {
|
|
14
|
+
sig.value;
|
|
15
|
+
trigger();
|
|
16
|
+
});
|
|
17
|
+
disposeOnScope(stop);
|
|
18
|
+
return {
|
|
19
|
+
get() {
|
|
20
|
+
track();
|
|
21
|
+
return sig.value;
|
|
22
|
+
},
|
|
23
|
+
set(value) {
|
|
24
|
+
sig.value = value;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/useSignalValue.ts
|
|
31
|
+
function useSignalValue(sig) {
|
|
32
|
+
const ref = customRef2((track, trigger) => {
|
|
33
|
+
const stop = effect2(() => {
|
|
34
|
+
sig.value;
|
|
35
|
+
trigger();
|
|
36
|
+
});
|
|
37
|
+
disposeOnScope(stop);
|
|
38
|
+
return {
|
|
39
|
+
get() {
|
|
40
|
+
track();
|
|
41
|
+
return sig.value;
|
|
42
|
+
},
|
|
43
|
+
set() {
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
return ref;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/useSignal.ts
|
|
51
|
+
import { signal } from "@hoosk/minisignals";
|
|
52
|
+
function useSignal(initialValue) {
|
|
53
|
+
return useSignalRef(signal(initialValue));
|
|
54
|
+
}
|
|
55
|
+
function useSignalRef(sig) {
|
|
56
|
+
return signalToRef(sig);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/useComputed.ts
|
|
60
|
+
import { computed as coreComputed } from "@hoosk/minisignals";
|
|
61
|
+
function useComputed(fn) {
|
|
62
|
+
const derived = coreComputed(fn);
|
|
63
|
+
disposeOnScope(() => derived.dispose());
|
|
64
|
+
return useSignalValue(derived);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/useSignalEffect.ts
|
|
68
|
+
import { effect as effect3 } from "@hoosk/minisignals";
|
|
69
|
+
function useSignalEffect(fn) {
|
|
70
|
+
const stop = effect3(fn);
|
|
71
|
+
disposeOnScope(stop);
|
|
72
|
+
return stop;
|
|
73
|
+
}
|
|
74
|
+
export {
|
|
75
|
+
useComputed,
|
|
76
|
+
useSignal,
|
|
77
|
+
useSignalEffect,
|
|
78
|
+
useSignalRef,
|
|
79
|
+
useSignalValue
|
|
80
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hoosk/minisignals-vue",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Vue 3 adapter for @hoosk/minisignals",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "dist/index.cjs",
|
|
8
|
+
"module": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"vue": ">=3.0.0"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@hoosk/minisignals": "^1.2.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@vue/test-utils": "^2.4.6",
|
|
29
|
+
"tsup": "^8.5.0",
|
|
30
|
+
"vue": "^3.4.0"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
34
|
+
"dev": "tsup src/index.ts --format cjs,esm --watch --dts",
|
|
35
|
+
"prepare": "npm run build",
|
|
36
|
+
"test": "vitest run"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"signals",
|
|
40
|
+
"reactive",
|
|
41
|
+
"reactivity",
|
|
42
|
+
"state",
|
|
43
|
+
"vue",
|
|
44
|
+
"composables",
|
|
45
|
+
"signal"
|
|
46
|
+
],
|
|
47
|
+
"author": "Hoosk",
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/Hoosk/minisignals.git",
|
|
52
|
+
"directory": "packages/vue"
|
|
53
|
+
}
|
|
54
|
+
}
|