Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8d7f35f

Browse files
author
Adrian Hurtado
committed
feat: add useValue and useStoredValue
1 parent 954226d commit 8d7f35f

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

‎src/composables/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import ValueFunctions from '../valueFunctions/valueFunctions'
2+
import { useCoreSimpleVolatileValue } from './useCoreVolatileValue'
3+
import { useCoreSimpleStoredValue } from './useCoreStoredValue'
24
import { bothComposableGenerator } from './composableGenerator'
35

6+
export const useValue = (options = {}) => useCoreSimpleVolatileValue(undefined, options)
7+
export const useStoredValue = (uid, options = {}) => useCoreSimpleStoredValue(uid, undefined, options)
8+
49
export const { volatile: useVueValue, stored: useVueStoredValue } = bothComposableGenerator()
510

611
export const { volatile: useVueBoolean, stored: useVueStoredBoolean } = bothComposableGenerator({

‎src/composables/useCoreStoredValue.js‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ import Store from '../store/store'
55
import { firstDefined } from './utils'
66
import { existsFieldInObject } from '../utils'
77

8-
export default function useCoreStoredValue (uid, emptyValue, options = {}) {
9-
const { disabled = false } = options
10-
8+
export function useCoreSimpleStoredValue (uid, emptyValue, options = {}) {
119
const initialOrDefaultValue = firstDefined(emptyValue, options, 'initialValue', 'defaultValue')
12-
13-
const refValue = customRef((track, trigger) => ({
10+
return customRef((track, trigger) => ({
1411
get () {
1512
const v = Store.value(uid).get(initialOrDefaultValue)
1613
track()
@@ -21,6 +18,12 @@ export default function useCoreStoredValue (uid, emptyValue, options = {}) {
2118
trigger()
2219
},
2320
}))
21+
}
22+
23+
export default function useCoreStoredValue (uid, emptyValue, options = {}) {
24+
const { disabled = false } = options
25+
26+
const refValue = useCoreSimpleStoredValue(uid, emptyValue, options)
2427

2528
const { set, clear } = useCoreCommonValue(refValue, { disabled, emptyValue })
2629

‎src/composables/useCoreVolatileValue.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import { ref } from 'vue'
33
import useCoreCommonValue from './useCoreCommonValue'
44
import { firstDefined } from './utils'
55

6+
export function useCoreSimpleVolatileValue (emptyValue, options = {}) {
7+
const initialOrDefaultValue = firstDefined(emptyValue, options, 'initialValue', 'defaultValue')
8+
return ref(initialOrDefaultValue)
9+
}
10+
611
export default function useCoreVolatileValue (emptyValue, options = {}) {
712
const { disabled = false } = options
813

914
const defaultValue = firstDefined(emptyValue, options, 'defaultValue')
1015
const initialValue = firstDefined(emptyValue, options, 'initialValue')
1116
const defaultOrInitialValue = firstDefined(emptyValue, options, 'defaultValue', 'initialValue')
12-
const initialOrDefaultValue = firstDefined(emptyValue, options, 'initialValue', 'defaultValue')
1317

14-
const refValue = ref(initialOrDefaultValue)
18+
const refValue = useCoreSimpleVolatileValue(emptyValue,options)
1519

1620
const { set, clear } = useCoreCommonValue(refValue, { disabled, emptyValue })
1721

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /