vue-computed-eager 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 +47 -0
- package/dist/index.cjs +18 -0
- package/dist/index.d.cts +23 -0
- package/dist/index.d.mts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.mjs +15 -0
- package/package.json +47 -0
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# vue-computed-eager
|
|
2
|
+
|
|
3
|
+
Eager computed
|
|
4
|
+
|
|
5
|
+
> Extracted from [VueUse](https://vueuse.org/) for standalone use.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install vue-computed-eager
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { computedEager } from 'vue-computed-eager';
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Eager computed without lazy evaluation.
|
|
20
|
+
|
|
21
|
+
> **Info:** This function will be removed in future version.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
> **Tip:** Note💡: If you are using Vue 3.4+, you can use `computed` right away, you no longer need this function.
|
|
25
|
+
In Vue 3.4+, if the computed new value does not change, `computed`, `effect`, `watch`, `watchEffect`, `render` dependencies will not be triggered.
|
|
26
|
+
See: https://github.com/vuejs/core/pull/5912
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
Learn more at [Vue: When a computed property can be the wrong tool](https://dev.to/linusborg/vue-when-a-computed-property-can-be-the-wrong-tool-195j).
|
|
30
|
+
|
|
31
|
+
- Use `computed()` when you have a complex calculation going on, which can actually profit from caching and lazy evaluation and should only be (re-)calculated if really necessary.
|
|
32
|
+
- Use `computedEager()` when you have a simple operation, with a rarely changing return value – often a boolean.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { computedEager } from 'vue-computed-eager'
|
|
36
|
+
|
|
37
|
+
const todos = ref([])
|
|
38
|
+
const hasOpenTodos = computedEager(() => !!todos.length)
|
|
39
|
+
|
|
40
|
+
console.log(hasOpenTodos.value) // false
|
|
41
|
+
toTodos.value.push({ title: 'Learn Vue' })
|
|
42
|
+
console.log(hasOpenTodos.value) // true
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const vue = require('vue');
|
|
4
|
+
|
|
5
|
+
function computedEager(fn, options) {
|
|
6
|
+
const result = vue.shallowRef();
|
|
7
|
+
vue.watchEffect(() => {
|
|
8
|
+
result.value = fn();
|
|
9
|
+
}, {
|
|
10
|
+
...options,
|
|
11
|
+
flush: options?.flush ?? "sync"
|
|
12
|
+
});
|
|
13
|
+
return vue.readonly(result);
|
|
14
|
+
}
|
|
15
|
+
const eagerComputed = computedEager;
|
|
16
|
+
|
|
17
|
+
exports.computedEager = computedEager;
|
|
18
|
+
exports.eagerComputed = eagerComputed;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { WatchOptionsBase, ShallowRef } from 'vue';
|
|
2
|
+
|
|
3
|
+
type ComputedEagerOptions = WatchOptionsBase;
|
|
4
|
+
type ComputedEagerReturn<T = any> = Readonly<ShallowRef<T>>;
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @deprecated This function will be removed in future version.
|
|
8
|
+
*
|
|
9
|
+
* Note: If you are using Vue 3.4+, you can straight use computed instead.
|
|
10
|
+
* Because in Vue 3.4+, if computed new value does not change,
|
|
11
|
+
* computed, effect, watch, watchEffect, render dependencies will not be triggered.
|
|
12
|
+
* refer: https://github.com/vuejs/core/pull/5912
|
|
13
|
+
*
|
|
14
|
+
* @param fn effect function
|
|
15
|
+
* @param options WatchOptionsBase
|
|
16
|
+
* @returns readonly shallowRef
|
|
17
|
+
*/
|
|
18
|
+
declare function computedEager<T>(fn: () => T, options?: ComputedEagerOptions): ComputedEagerReturn<T>;
|
|
19
|
+
/** @deprecated use `computedEager` instead */
|
|
20
|
+
declare const eagerComputed: typeof computedEager;
|
|
21
|
+
|
|
22
|
+
export { computedEager, eagerComputed };
|
|
23
|
+
export type { ComputedEagerOptions, ComputedEagerReturn };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { WatchOptionsBase, ShallowRef } from 'vue';
|
|
2
|
+
|
|
3
|
+
type ComputedEagerOptions = WatchOptionsBase;
|
|
4
|
+
type ComputedEagerReturn<T = any> = Readonly<ShallowRef<T>>;
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @deprecated This function will be removed in future version.
|
|
8
|
+
*
|
|
9
|
+
* Note: If you are using Vue 3.4+, you can straight use computed instead.
|
|
10
|
+
* Because in Vue 3.4+, if computed new value does not change,
|
|
11
|
+
* computed, effect, watch, watchEffect, render dependencies will not be triggered.
|
|
12
|
+
* refer: https://github.com/vuejs/core/pull/5912
|
|
13
|
+
*
|
|
14
|
+
* @param fn effect function
|
|
15
|
+
* @param options WatchOptionsBase
|
|
16
|
+
* @returns readonly shallowRef
|
|
17
|
+
*/
|
|
18
|
+
declare function computedEager<T>(fn: () => T, options?: ComputedEagerOptions): ComputedEagerReturn<T>;
|
|
19
|
+
/** @deprecated use `computedEager` instead */
|
|
20
|
+
declare const eagerComputed: typeof computedEager;
|
|
21
|
+
|
|
22
|
+
export { computedEager, eagerComputed };
|
|
23
|
+
export type { ComputedEagerOptions, ComputedEagerReturn };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { WatchOptionsBase, ShallowRef } from 'vue';
|
|
2
|
+
|
|
3
|
+
type ComputedEagerOptions = WatchOptionsBase;
|
|
4
|
+
type ComputedEagerReturn<T = any> = Readonly<ShallowRef<T>>;
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @deprecated This function will be removed in future version.
|
|
8
|
+
*
|
|
9
|
+
* Note: If you are using Vue 3.4+, you can straight use computed instead.
|
|
10
|
+
* Because in Vue 3.4+, if computed new value does not change,
|
|
11
|
+
* computed, effect, watch, watchEffect, render dependencies will not be triggered.
|
|
12
|
+
* refer: https://github.com/vuejs/core/pull/5912
|
|
13
|
+
*
|
|
14
|
+
* @param fn effect function
|
|
15
|
+
* @param options WatchOptionsBase
|
|
16
|
+
* @returns readonly shallowRef
|
|
17
|
+
*/
|
|
18
|
+
declare function computedEager<T>(fn: () => T, options?: ComputedEagerOptions): ComputedEagerReturn<T>;
|
|
19
|
+
/** @deprecated use `computedEager` instead */
|
|
20
|
+
declare const eagerComputed: typeof computedEager;
|
|
21
|
+
|
|
22
|
+
export { computedEager, eagerComputed };
|
|
23
|
+
export type { ComputedEagerOptions, ComputedEagerReturn };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { shallowRef, watchEffect, readonly } from 'vue';
|
|
2
|
+
|
|
3
|
+
function computedEager(fn, options) {
|
|
4
|
+
const result = shallowRef();
|
|
5
|
+
watchEffect(() => {
|
|
6
|
+
result.value = fn();
|
|
7
|
+
}, {
|
|
8
|
+
...options,
|
|
9
|
+
flush: options?.flush ?? "sync"
|
|
10
|
+
});
|
|
11
|
+
return readonly(result);
|
|
12
|
+
}
|
|
13
|
+
const eagerComputed = computedEager;
|
|
14
|
+
|
|
15
|
+
export { computedEager, eagerComputed };
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vue-computed-eager",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Eager computed",
|
|
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
|
+
"computedEager"
|
|
28
|
+
],
|
|
29
|
+
"author": "VueFrag",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/vuefrag/vue-computed-eager.git"
|
|
34
|
+
},
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/vuefrag/vue-computed-eager/issues"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/vuefrag/vue-computed-eager#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
|
+
}
|