Vue-ditto will create a Ditto ref for the original ref, you can add any metadata to it, even if the original ref changes, these metadata will be retained.
- Support primitive, plain object, and array types
- Support the complete operation method of array, namely
push
,pop
,unshift
,shift
,reverse
,sort
,lookup set
,delete
,length
and sparse array are all supported.
It works for Vue 2 & 3 by the power of Vue Demi!
$ npm i vue-ditto # yarn add vue-ditto
import { dittoRef } from "vue-ditto";
<script src="https://unpkg.com/vue-ditto"></script>
const dittoRef = VueDitto.dittoRef;
To create a Ditto, you just need to pass ref to dittoRef
:
const original = ref("foo");
console.log(dittoRef({ original }).value);
// {}
It also works with nesting plain objects and arrays:
const original = ref({ foo: 42 });
console.log(dittoRef({ original }).value);
// { foo: {} }
For arrays, each item must be a plain object, and the object must contain a unique id
property to hint Vue-ditto to diff the new item against the old item.
const original = ref([{ id: 0, foo: 42 }]);
console.log(dittoRef({ original }).value);
// [ { id: {}, foo: {} } ]
There are several callbacks that can be used to track creation and updates:
const original = ref({ foo: 42 });
const ditto = dittoRef({
original,
onCreated: ({ ditto, path, original }) => {
console.log(`${["$"].concat(path as string[]).join(".")} is created`);
// $ is created
// $.foo is created
},
});
You can add any metadata for each Ditto in these callbacks, and you must also provide metaKeys
to preserve these metadata during the operation.
const original = ref({ foo: 42 });
const ditto = dittoRef({
original,
metaKeys: ["$original"],
onCreated: ({ ditto, path, original }) => {
ditto.$original = original.value;
},
});
console.log(ditto.value);
// { '$original': { foo: 42 }, foo: { '$original': 42 } }
This also applies to the array itself:
const original = ref([
{ id: 1, value: "foo" },
{ id: 2, value: "bar" },
]);
const ditto = dittoRef({
original,
metaKeys: ["$original"],
onCreated: ({ ditto, path, original }) => {
ditto.$original = original.value;
},
});
console.log(ditto.value);
/*
[
{
'$original': { id: 1, value: 'foo' },
id: { '$original': 1 },
value: { '$original': 'foo' }
},
{
'$original': { id: 2, value: 'bar' },
id: { '$original': 2 },
value: { '$original': 'bar' }
},
'$original': [ { id: 1, value: 'foo' }, { id: 2, value: 'bar' } ]
]
*/
When the original ref changes, the corresponding callback will be called, which gives you the opportunity to update the metadata:
const original = ref<{ foo: number }>({ foo: 42 });
const ditto = dittoRef({
original,
metaKeys: ["$updateCount"],
flush: "sync",
onCreated: ({ ditto, path, original }) => {
ditto.$updateCount = 0;
},
onUpdated: ({ ditto, path, original }) => {
ditto.$updateCount++;
},
});
console.log(ditto.value);
// { '$updateCount': 0, foo: { '$updateCount': 0 } }
original.value.foo++;
console.log(ditto.value);
// { '$updateCount': 0, foo: { '$updateCount': 1 } }
For TypeScript, you must extend the type for the Ditto interface:
declare module "vue-ditto" {
interface DittoMeta<T> {
$original: T;
$updateCount: number;
}
}
function dittoRef<T>({
original,
metaKeys,
flush,
onCreated,
onChildrenCreated,
onUpdated,
onChildrenUpdated,
}: {
original: Ref<T>;
metaKeys?: Key[];
flush?: WatchOptions["flush"];
onCreated?: DittoCallback;
onChildrenCreated?: DittoCallback;
onUpdated?: DittoCallback;
onChildrenUpdated?: DittoCallback;
}): Ref<Ditto<T>>;
type Ditto<T> = T extends any[]
? Ditto<T[number]>[] & DittoMeta<T>
: T extends object
? {
[P in keyof T]: Ditto<T[P]>;
} & DittoMeta<T>
: DittoMeta<T>;
interface DittoMeta<T> {}
type DittoCallback = <T>({
ditto,
path,
original,
}: {
ditto: Ditto<T>;
path: Path;
original: Ref<any>;
}) => void;
type Path = Key[];
type Key = string | number | symbol;
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
This project use SemVer for versioning. For the versions available, see the tags on this repository.
This project is licensed under the MIT License - see the LICENSE file for details