tracked-instance 1.0.12 → 1.0.13
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 +7 -3
- package/dist/index.mjs +10 -5
- package/dist/types/collection.d.ts +5 -5
- package/dist/types/tracked-instance.d.ts +5 -7
- package/dist/types/utils.d.ts +15 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -187,7 +187,7 @@ console.log(items.value[0].meta.isValidName.value) // false
|
|
|
187
187
|
```
|
|
188
188
|
|
|
189
189
|
### Real-world example
|
|
190
|
-
[Try on playground](https://play.vuejs.org/#
|
|
190
|
+
[Try on playground](https://play.vuejs.org/#eNp9VcFy0zAQ/ZWtL3ZnUvsAp5BmKG0P9FCYFk6Yg2sriRpZ9khymk7G38DADMNwob/BDDN8DD9AP4GVZDuym3BJLO1q973dp9XGOynLcFURb+xNZCpoqUASVZXTmNO8LISCjSCzGmaiyMFHR39rqCQ5LRgjqaIFb12USNIlyY4olyrhqfaPeVrgCjZUnlGh7keQZNkIqCK5HIEgebEi+h8Tj4AVSXaWqKSGY+glCA51pNYcfNjwJCdj8C8wn1+PoFsXC95fE7/+aA5bGJzcvZdEXKIZcyC7wPcds0xW5HSR8DmRaA4O4XgKm5hDhyzQC7Dww1XCKmI3AMIZZYqIQJv0sQP9EVJ5ZShm1hlTNd55UgbG1+Swvk3VwgwTOf74U8d8EtkWYXNwgf4lSxTBFcDkplKqwG0deZxRmdwwkh3H3kFT9Niztpcpo+kSDQ5PazNxAB4fvvyGazRC2lh1+MjGd3NtQ5nWxd70z/dff39+giu97J9YCIgMavzO6GragBrDZgPNN9T1JNI2x80iOsky3TQtBzG2WxPKy0q1lVwd5UVGGCJxetsSBlD3JUGbImsEaTc1GBNnSAWVGTTKcWLVhy8GsvF9zffHVwNOA+vzdYlUrE3WdgwBzwqB2Uz38SbwjKwP8c+KCiO30CeMojOdmUbuEJPjOixKvzD71BVqrttS7SmXW7KdZbOXOLBEENTjw7fPqAO92StM73gfaY/kJblrCbpeXb4+HaM/nA+Or5MNwNWlg+IpMJPYinKAA9XZ9SRitO1o1L+Ekem1/my6ZBtqROvowDTVKsCMirbx7fwIqJ0IT0fHtt0dxj09dQEPu7VLSajpWcKkVlRbLhy9NzhcB3Xq6DdsezXwRp59HY5wuIW3suD4sJjpiVmNAdWNBbahUGSD10IbY2+hVCnHUVTxcjkP0yKPnvrpCMiwxoxK4uSe0fkgH54rKSPiTamfj37ehLHi7sLsKVGRUbufLki63LF/K9cW2lutNbFCAJ1NJWKux582n19fmmvTGfH+VayhtceIsixYpTFat1cVzxC242fQvjblo3z+Tp6vFeGyJaWBmmoY/9jDR/r0P9S3cJ+Fz7sq1v8AiAKmmg==)
|
|
191
191
|
```vue
|
|
192
192
|
<script setup>
|
|
193
193
|
import {ref} from 'vue'
|
|
@@ -200,7 +200,11 @@ console.log(items.value[0].meta.isValidName.value) // false
|
|
|
200
200
|
const newUserName = ref('')
|
|
201
201
|
|
|
202
202
|
const saveChanges = () => {
|
|
203
|
-
loadData(
|
|
203
|
+
loadData(
|
|
204
|
+
items.value
|
|
205
|
+
.filter(item => !item.isRemoved.value)
|
|
206
|
+
.map((item) => item.instance.data.value)
|
|
207
|
+
)
|
|
204
208
|
}
|
|
205
209
|
</script>
|
|
206
210
|
|
|
@@ -222,7 +226,7 @@ console.log(items.value[0].meta.isValidName.value) // false
|
|
|
222
226
|
v-model="newUserName"
|
|
223
227
|
type="text"
|
|
224
228
|
/>
|
|
225
|
-
<button @click="add({name: newUserName})">➕ Add user</button>
|
|
229
|
+
<button @click="add({name: newUserName}); newUserName = ''">➕ Add user</button>
|
|
226
230
|
</div>
|
|
227
231
|
|
|
228
232
|
<ul>
|
package/dist/index.mjs
CHANGED
|
@@ -708,7 +708,10 @@ function unset(object, path) {
|
|
|
708
708
|
var unset_default = unset;
|
|
709
709
|
|
|
710
710
|
// src/tracked-instance.ts
|
|
711
|
-
import { computed
|
|
711
|
+
import { computed } from "vue";
|
|
712
|
+
|
|
713
|
+
// src/utils.ts
|
|
714
|
+
import { customRef } from "vue";
|
|
712
715
|
var isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof Date) && !(value instanceof File) && !(value instanceof Map) && !(value instanceof Set);
|
|
713
716
|
var isEmpty = (value) => Object.keys(value).length === 0;
|
|
714
717
|
var iterateObject = function* (source, params = {}) {
|
|
@@ -765,6 +768,8 @@ var createNestedRef = (source, handler) => customRef((track, trigger) => {
|
|
|
765
768
|
}
|
|
766
769
|
};
|
|
767
770
|
});
|
|
771
|
+
|
|
772
|
+
// src/tracked-instance.ts
|
|
768
773
|
var ArrayInOriginalData = class {
|
|
769
774
|
length;
|
|
770
775
|
constructor(length) {
|
|
@@ -845,7 +850,7 @@ var snapshotValueToOriginalData = (originalData, path, value) => {
|
|
|
845
850
|
}
|
|
846
851
|
}
|
|
847
852
|
};
|
|
848
|
-
|
|
853
|
+
function useTrackedInstance(initialData) {
|
|
849
854
|
const _originalData = createNestedRef({}, (path) => ({
|
|
850
855
|
deleteProperty(target, property) {
|
|
851
856
|
const result = Reflect.deleteProperty(target, property);
|
|
@@ -941,11 +946,11 @@ var useTrackedInstance = (initialData) => {
|
|
|
941
946
|
loadData,
|
|
942
947
|
reset
|
|
943
948
|
};
|
|
944
|
-
}
|
|
949
|
+
}
|
|
945
950
|
|
|
946
951
|
// src/collection.ts
|
|
947
|
-
import { computed as computed2, shallowRef, triggerRef
|
|
948
|
-
var useCollection = (createItemMeta = () =>
|
|
952
|
+
import { computed as computed2, ref, shallowRef, triggerRef } from "vue";
|
|
953
|
+
var useCollection = (createItemMeta = () => void 0) => {
|
|
949
954
|
const items = shallowRef([]);
|
|
950
955
|
const isDirty = computed2(
|
|
951
956
|
() => items.value.some(({ instance, isRemoved, isNew }) => instance.isDirty.value || isNew.value || isRemoved.value)
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ComputedRef, Ref, ShallowRef } from 'vue';
|
|
2
2
|
import { TrackedInstance } from './tracked-instance';
|
|
3
|
-
export interface CollectionItem<Item
|
|
3
|
+
export interface CollectionItem<Item, Meta> {
|
|
4
4
|
instance: TrackedInstance<Item>;
|
|
5
5
|
meta: Meta;
|
|
6
6
|
isRemoved: Ref<boolean>;
|
|
7
7
|
isNew: Ref<boolean>;
|
|
8
8
|
}
|
|
9
|
-
export interface Collection<Item
|
|
9
|
+
export interface Collection<Item, Meta> {
|
|
10
10
|
items: ShallowRef<CollectionItem<Item, Meta>[]>;
|
|
11
11
|
isDirty: ComputedRef<boolean>;
|
|
12
|
-
add: (item:
|
|
12
|
+
add: (item: Item, afterIndex?: number) => CollectionItem<Item, Meta>;
|
|
13
13
|
remove: (index: number, isHardRemove?: boolean) => void;
|
|
14
14
|
loadData: (items: Item[]) => void;
|
|
15
15
|
reset: () => void;
|
|
16
16
|
}
|
|
17
|
-
export declare const useCollection: <Item
|
|
17
|
+
export declare const useCollection: <Item = any, Meta = any>(createItemMeta?: (instance: TrackedInstance<Item>) => Meta) => Collection<Item, Meta>;
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { Ref } from 'vue';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} : T;
|
|
5
|
-
export interface TrackedInstance<Data extends Record<string, any>> {
|
|
2
|
+
import { DeepPartial } from './utils';
|
|
3
|
+
export interface TrackedInstance<Data> {
|
|
6
4
|
data: Ref<Data>;
|
|
7
5
|
isDirty: Ref<boolean>;
|
|
8
6
|
changedData: Ref<DeepPartial<Data>>;
|
|
9
|
-
loadData: (newData:
|
|
7
|
+
loadData: (newData: Data) => void;
|
|
10
8
|
reset: () => void;
|
|
11
9
|
}
|
|
12
|
-
export declare
|
|
13
|
-
export
|
|
10
|
+
export declare function useTrackedInstance<Data = any>(): TrackedInstance<Data | undefined>;
|
|
11
|
+
export declare function useTrackedInstance<Data>(value: Data): TrackedInstance<Data>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type DeepPartial<T> = T extends object ? {
|
|
2
|
+
[P in keyof T]?: DeepPartial<T[P]>;
|
|
3
|
+
} : T;
|
|
4
|
+
export interface NestedProxyPathItem {
|
|
5
|
+
target: Record<string, any>;
|
|
6
|
+
property: string;
|
|
7
|
+
receiver?: Record<string, any>;
|
|
8
|
+
}
|
|
9
|
+
export declare const isObject: (value: unknown) => boolean;
|
|
10
|
+
export declare const isEmpty: (value: object) => boolean;
|
|
11
|
+
export declare const iterateObject: (source: Record<string, any>, params?: {
|
|
12
|
+
goDeepCondition?: ((path: string[], value: any) => boolean) | undefined;
|
|
13
|
+
includeParent?: boolean | undefined;
|
|
14
|
+
}) => Generator<[string[], any], void, unknown>;
|
|
15
|
+
export declare const createNestedRef: <Source extends Record<string, any>>(source: Source, handler: <InnerSource extends Record<string, any>>(path: NestedProxyPathItem[]) => ProxyHandler<InnerSource>) => import("vue").Ref<Source>;
|