vuse-rx
Version:
First-class rxjs support for Vue 3
33 lines • 1.09 kB
JavaScript
import { ref, watch } from 'vue';
export function syncRef(ref1, maps, _ref2) {
const ref2 = ref(_ref2 == null
? maps.to
? maps.to(ref1.value)
: ref1.value
: _ref2);
for (const key in maps)
(ref2[key] = bind(ref1, ref2, maps, key, this)).bind();
return ref2;
}
syncRef.with = (...options) => {
const opts = Object.assign({}, ...options);
const f = syncRef.bind(opts);
f.with = syncRef.with.bind(opts);
return f;
};
const bind = (refBase, refDest, maps, dir, options) => ({
bind: (bindOptions) => {
const { ref, map, watch: opts } = {
ref: refBase,
map: maps[dir],
watch: options,
...bindOptions,
};
refDest[dir].stop();
refDest[dir].stop = dir === 'to'
? watch(ref, v => refDest.value = map(v), Object.assign({}, options, opts))
: watch(refDest, v => ref.value = map(v), Object.assign({}, options, opts));
},
stop: () => { }
});
//# sourceMappingURL=sync-ref.js.map