vue-computed-with-control 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 ADDED
@@ -0,0 +1,81 @@
1
+ # vue-computed-with-control
2
+
3
+ Computed with manual control
4
+
5
+ > Extracted from [VueUse](https://vueuse.org/) for standalone use.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install vue-computed-with-control
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { computedWithControl } from 'vue-computed-with-control';
17
+ ```
18
+
19
+ Explicitly define the dependencies of computed.
20
+
21
+ ```ts twoslash include main
22
+ import { computedWithControl } from 'vue-computed-with-control'
23
+
24
+ const source = ref('foo')
25
+ const counter = ref(0)
26
+
27
+ const computedRef = computedWithControl(
28
+ () => source.value, // watch source, same as `watch`
29
+ () => counter.value, // computed getter, same as `computed`
30
+ )
31
+ ```
32
+
33
+ With this, the changes of `counter` won't trigger `computedRef` to update but the `source` ref does.
34
+
35
+ ```ts
36
+ // @include: main
37
+ // ---cut---
38
+ console.log(computedRef.value) // 0
39
+
40
+ counter.value += 1
41
+
42
+ console.log(computedRef.value) // 0
43
+
44
+ source.value = 'bar'
45
+
46
+ console.log(computedRef.value) // 1
47
+ ```
48
+
49
+ ### Manual Triggering
50
+
51
+ You can also manually trigger the update of the computed by:
52
+
53
+ ```ts
54
+ // @include: main
55
+ // ---cut---
56
+ const computedRef = computedWithControl(
57
+ () => source.value,
58
+ () => counter.value,
59
+ )
60
+
61
+ computedRef.trigger()
62
+ ```
63
+
64
+ ### Deep Watch
65
+
66
+ Unlike `computed`, `computedWithControl` is shallow by default.
67
+ You can specify the same options as `watch` to control the behavior:
68
+
69
+ ```ts
70
+ const source = ref({ name: 'foo' })
71
+
72
+ const computedRef = computedWithControl(
73
+ source,
74
+ () => counter.value,
75
+ { deep: true },
76
+ )
77
+ ```
78
+
79
+ ## License
80
+
81
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ const vue = require('vue');
4
+
5
+ function computedWithControl(source, fn, options = {}) {
6
+ let v = void 0;
7
+ let track;
8
+ let trigger;
9
+ let dirty = true;
10
+ const update = () => {
11
+ dirty = true;
12
+ trigger();
13
+ };
14
+ vue.watch(source, update, { flush: "sync", ...options });
15
+ const get = typeof fn === "function" ? fn : fn.get;
16
+ const set = typeof fn === "function" ? void 0 : fn.set;
17
+ const result = vue.customRef((_track, _trigger) => {
18
+ track = _track;
19
+ trigger = _trigger;
20
+ return {
21
+ get() {
22
+ if (dirty) {
23
+ v = get(v);
24
+ dirty = false;
25
+ }
26
+ track();
27
+ return v;
28
+ },
29
+ set(v2) {
30
+ set?.(v2);
31
+ }
32
+ };
33
+ });
34
+ result.trigger = update;
35
+ return result;
36
+ }
37
+ const controlledComputed = computedWithControl;
38
+
39
+ exports.computedWithControl = computedWithControl;
40
+ exports.controlledComputed = controlledComputed;
@@ -0,0 +1,20 @@
1
+ import { ComputedRef, WritableComputedRef, WatchSource, MultiWatchSources, ComputedGetter, WatchOptions, WritableComputedOptions } from 'vue';
2
+
3
+ interface ComputedWithControlRefExtra {
4
+ /**
5
+ * Force update the computed value.
6
+ */
7
+ trigger: () => void;
8
+ }
9
+ interface ComputedRefWithControl<T> extends ComputedRef<T>, ComputedWithControlRefExtra {
10
+ }
11
+ interface WritableComputedRefWithControl<T> extends WritableComputedRef<T>, ComputedWithControlRefExtra {
12
+ }
13
+ type ComputedWithControlRef<T = any> = ComputedRefWithControl<T> | WritableComputedRefWithControl<T>;
14
+ declare function computedWithControl<T>(source: WatchSource | MultiWatchSources, fn: ComputedGetter<T>, options?: WatchOptions): ComputedRefWithControl<T>;
15
+ declare function computedWithControl<T>(source: WatchSource | MultiWatchSources, fn: WritableComputedOptions<T>, options?: WatchOptions): WritableComputedRefWithControl<T>;
16
+ /** @deprecated use `computedWithControl` instead */
17
+ declare const controlledComputed: typeof computedWithControl;
18
+
19
+ export { computedWithControl, controlledComputed };
20
+ export type { ComputedRefWithControl, ComputedWithControlRef, ComputedWithControlRefExtra, WritableComputedRefWithControl };
@@ -0,0 +1,20 @@
1
+ import { ComputedRef, WritableComputedRef, WatchSource, MultiWatchSources, ComputedGetter, WatchOptions, WritableComputedOptions } from 'vue';
2
+
3
+ interface ComputedWithControlRefExtra {
4
+ /**
5
+ * Force update the computed value.
6
+ */
7
+ trigger: () => void;
8
+ }
9
+ interface ComputedRefWithControl<T> extends ComputedRef<T>, ComputedWithControlRefExtra {
10
+ }
11
+ interface WritableComputedRefWithControl<T> extends WritableComputedRef<T>, ComputedWithControlRefExtra {
12
+ }
13
+ type ComputedWithControlRef<T = any> = ComputedRefWithControl<T> | WritableComputedRefWithControl<T>;
14
+ declare function computedWithControl<T>(source: WatchSource | MultiWatchSources, fn: ComputedGetter<T>, options?: WatchOptions): ComputedRefWithControl<T>;
15
+ declare function computedWithControl<T>(source: WatchSource | MultiWatchSources, fn: WritableComputedOptions<T>, options?: WatchOptions): WritableComputedRefWithControl<T>;
16
+ /** @deprecated use `computedWithControl` instead */
17
+ declare const controlledComputed: typeof computedWithControl;
18
+
19
+ export { computedWithControl, controlledComputed };
20
+ export type { ComputedRefWithControl, ComputedWithControlRef, ComputedWithControlRefExtra, WritableComputedRefWithControl };
@@ -0,0 +1,20 @@
1
+ import { ComputedRef, WritableComputedRef, WatchSource, MultiWatchSources, ComputedGetter, WatchOptions, WritableComputedOptions } from 'vue';
2
+
3
+ interface ComputedWithControlRefExtra {
4
+ /**
5
+ * Force update the computed value.
6
+ */
7
+ trigger: () => void;
8
+ }
9
+ interface ComputedRefWithControl<T> extends ComputedRef<T>, ComputedWithControlRefExtra {
10
+ }
11
+ interface WritableComputedRefWithControl<T> extends WritableComputedRef<T>, ComputedWithControlRefExtra {
12
+ }
13
+ type ComputedWithControlRef<T = any> = ComputedRefWithControl<T> | WritableComputedRefWithControl<T>;
14
+ declare function computedWithControl<T>(source: WatchSource | MultiWatchSources, fn: ComputedGetter<T>, options?: WatchOptions): ComputedRefWithControl<T>;
15
+ declare function computedWithControl<T>(source: WatchSource | MultiWatchSources, fn: WritableComputedOptions<T>, options?: WatchOptions): WritableComputedRefWithControl<T>;
16
+ /** @deprecated use `computedWithControl` instead */
17
+ declare const controlledComputed: typeof computedWithControl;
18
+
19
+ export { computedWithControl, controlledComputed };
20
+ export type { ComputedRefWithControl, ComputedWithControlRef, ComputedWithControlRefExtra, WritableComputedRefWithControl };
package/dist/index.mjs ADDED
@@ -0,0 +1,37 @@
1
+ import { watch, customRef } from 'vue';
2
+
3
+ function computedWithControl(source, fn, options = {}) {
4
+ let v = void 0;
5
+ let track;
6
+ let trigger;
7
+ let dirty = true;
8
+ const update = () => {
9
+ dirty = true;
10
+ trigger();
11
+ };
12
+ watch(source, update, { flush: "sync", ...options });
13
+ const get = typeof fn === "function" ? fn : fn.get;
14
+ const set = typeof fn === "function" ? void 0 : fn.set;
15
+ const result = customRef((_track, _trigger) => {
16
+ track = _track;
17
+ trigger = _trigger;
18
+ return {
19
+ get() {
20
+ if (dirty) {
21
+ v = get(v);
22
+ dirty = false;
23
+ }
24
+ track();
25
+ return v;
26
+ },
27
+ set(v2) {
28
+ set?.(v2);
29
+ }
30
+ };
31
+ });
32
+ result.trigger = update;
33
+ return result;
34
+ }
35
+ const controlledComputed = computedWithControl;
36
+
37
+ export { computedWithControl, controlledComputed };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "vue-computed-with-control",
3
+ "version": "1.0.0",
4
+ "description": "Computed with manual control",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.cjs",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "unbuild",
20
+ "prepublishOnly": "npm run build"
21
+ },
22
+ "keywords": [
23
+ "vue",
24
+ "vue3",
25
+ "composable",
26
+ "vueuse",
27
+ "computedWithControl"
28
+ ],
29
+ "author": "VueFrag",
30
+ "license": "MIT",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/vuefrag/vue-computed-with-control.git"
34
+ },
35
+ "bugs": {
36
+ "url": "https://github.com/vuefrag/vue-computed-with-control/issues"
37
+ },
38
+ "homepage": "https://github.com/vuefrag/vue-computed-with-control#readme",
39
+ "peerDependencies": {
40
+ "vue": ">=3.0.0"
41
+ },
42
+ "devDependencies": {
43
+ "unbuild": "^2.0.0",
44
+ "typescript": "^5.0.0",
45
+ "vue": "^3.4.0"
46
+ }
47
+ }