@live-change/vue3-components 0.2.5 → 0.2.8
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/logic/synchronizedList.js +32 -21
- package/package.json +3 -3
|
@@ -40,7 +40,7 @@ function synchronizedList(options) {
|
|
|
40
40
|
source,
|
|
41
41
|
update: updateAction,
|
|
42
42
|
insert: insertAction,
|
|
43
|
-
|
|
43
|
+
delete: deleteAction,
|
|
44
44
|
move: moveAction,
|
|
45
45
|
identifiers = {},
|
|
46
46
|
objectIdentifiers = object => ({ id: object.id }),
|
|
@@ -59,7 +59,7 @@ function synchronizedList(options) {
|
|
|
59
59
|
if(!source) throw new Error('source must be defined')
|
|
60
60
|
const synchronizedList = ref([])
|
|
61
61
|
const locallyAdded = ref([])
|
|
62
|
-
const
|
|
62
|
+
const locallyDeleted = ref([])
|
|
63
63
|
|
|
64
64
|
function createSynchronizedElement(sourceData) {
|
|
65
65
|
const elementSource = ref(sourceData)
|
|
@@ -69,55 +69,65 @@ function synchronizedList(options) {
|
|
|
69
69
|
return synchronizedElement
|
|
70
70
|
}
|
|
71
71
|
function synchronizeFromSource() {
|
|
72
|
+
//console.log("SYNCHRONIZE FROM SOURCE!")
|
|
72
73
|
let obsoleteLocallyAdded = new Set()
|
|
73
|
-
let
|
|
74
|
+
let obsoleteLocallyDeleted = new Set()
|
|
74
75
|
let newSynchronized = sortedArraysMerge(
|
|
75
|
-
(synchronizedElement, sourceElement, locallyAddedElement,
|
|
76
|
+
(synchronizedElement, sourceElement, locallyAddedElement, locallyDeletedElement) => {
|
|
77
|
+
|
|
78
|
+
/*console.log("MERGE ELEMENT", synchronizedElement)
|
|
79
|
+
console.log("SOURCE ELEMENT", sourceElement)
|
|
80
|
+
console.log("LOCALLY ADDED", locallyAddedElement)
|
|
81
|
+
console.log("LOCALLY DELETED", locallyDeletedElement)*/
|
|
82
|
+
|
|
76
83
|
if(locallyAddedElement && sourceElement) {
|
|
77
84
|
obsoleteLocallyAdded.add(locallyAddedElement.id)
|
|
78
85
|
}
|
|
79
|
-
if(
|
|
80
|
-
|
|
86
|
+
if(locallyDeletedElement && !sourceElement) {
|
|
87
|
+
obsoleteLocallyDeleted.add(locallyDeletedElement.id)
|
|
81
88
|
}
|
|
82
89
|
|
|
83
90
|
if(synchronizedElement) {
|
|
84
|
-
if(
|
|
91
|
+
if(locallyDeletedElement) {
|
|
85
92
|
return null // synchronized element locally
|
|
86
93
|
}
|
|
87
94
|
if(sourceElement) {
|
|
88
95
|
synchronizedElement.source.value = sourceElement
|
|
96
|
+
return synchronizedElement
|
|
89
97
|
} else if(locallyAddedElement) {
|
|
90
98
|
synchronizedElement.source.value = locallyAddedElement
|
|
99
|
+
return synchronizedElement
|
|
91
100
|
} else {
|
|
92
101
|
return null // synchronized element deleted
|
|
93
102
|
}
|
|
94
103
|
} else if(sourceElement) {
|
|
95
|
-
console.log("CREATE SYNCHRONIZED FROM SOURCE!")
|
|
104
|
+
//console.log("CREATE SYNCHRONIZED FROM SOURCE!")
|
|
96
105
|
return createSynchronizedElement(sourceElement)
|
|
97
106
|
} else if(locallyAddedElement) {
|
|
98
107
|
return createSynchronizedElement(locallyAddedElement)
|
|
99
108
|
}
|
|
100
|
-
}, synchronizedList.value, source.value || [], locallyAdded.value,
|
|
109
|
+
}, synchronizedList.value, source.value || [], locallyAdded.value, locallyDeleted.value)
|
|
101
110
|
if(obsoleteLocallyAdded.length > 0) {
|
|
102
111
|
locallyAdded.value = locallyAdded.value.filter(
|
|
103
112
|
locallyAddedElement => obsoleteLocallyAdded.has(locallyAddedElement.id)
|
|
104
113
|
)
|
|
105
114
|
}
|
|
106
|
-
if(
|
|
107
|
-
|
|
108
|
-
|
|
115
|
+
if(obsoleteLocallyDeleted.length > 0) {
|
|
116
|
+
locallyDeleted.value = locallyDeleted.value.filter(
|
|
117
|
+
locallyDeletedElement => obsoleteLocallyDeleted.has(locallyDeletedElement.id)
|
|
109
118
|
)
|
|
110
119
|
}
|
|
111
120
|
synchronizedList.value = newSynchronized
|
|
112
121
|
}
|
|
113
122
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
123
|
+
watch(() => (source.value ?? []).map(({ id }) => id), (sourceIds, oldSourceIds) => {
|
|
124
|
+
console.log("SOURCE IDs changed", oldSourceIds, '=>', sourceIds)
|
|
125
|
+
synchronizeFromSource()
|
|
126
|
+
})
|
|
117
127
|
synchronizeFromSource()
|
|
118
128
|
|
|
119
129
|
const changed = computed(() => (synchronizedList.value.some(({ changed }) => changed.value))
|
|
120
|
-
|| locallyAdded.length ||
|
|
130
|
+
|| locallyAdded.length || locallyDeleted.length)
|
|
121
131
|
|
|
122
132
|
async function save() {
|
|
123
133
|
const results = await Promise.app(synchronizedList.value.map(synchronizedElement => synchronizedElement.save()))
|
|
@@ -125,13 +135,14 @@ function synchronizedList(options) {
|
|
|
125
135
|
}
|
|
126
136
|
|
|
127
137
|
async function insert(element) {
|
|
128
|
-
locallyAdded.push(element)
|
|
138
|
+
locallyAdded.value.push(element)
|
|
129
139
|
await insertAction({ ...element, [timeField]: timeSource(), ...identifiers })
|
|
130
140
|
}
|
|
131
141
|
|
|
132
|
-
async function
|
|
133
|
-
|
|
134
|
-
|
|
142
|
+
async function deleteElement(element) {
|
|
143
|
+
const deleted = JSON.parse(JSON.stringify(element))
|
|
144
|
+
locallyDeleted.value.push(element)
|
|
145
|
+
await deleteAction({ ...element, ...identifiers, ...objectIdentifiers(element) })
|
|
135
146
|
}
|
|
136
147
|
|
|
137
148
|
async function move(element, toId) {
|
|
@@ -139,7 +150,7 @@ function synchronizedList(options) {
|
|
|
139
150
|
}
|
|
140
151
|
|
|
141
152
|
const synchronizedValue = computed(() => synchronizedList.value.map(synchronizedElement => synchronizedElement.value))
|
|
142
|
-
return { value: synchronizedValue, save, changed, insert,
|
|
153
|
+
return { value: synchronizedValue, save, changed, insert, delete: deleteElement, move }
|
|
143
154
|
}
|
|
144
155
|
|
|
145
156
|
export default synchronizedList
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/vue3-components",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"description": "Live Change Framework - vue components",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,10 +21,10 @@
|
|
|
21
21
|
},
|
|
22
22
|
"homepage": "https://github.com/live-change/live-change-framework-vue3",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@live-change/vue3-ssr": "^0.2.
|
|
24
|
+
"@live-change/vue3-ssr": "^0.2.7",
|
|
25
25
|
"debug": "^4.3.2",
|
|
26
26
|
"mitt": "3.0.0",
|
|
27
27
|
"vue": "^3.2.31"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "3d267cf03e99822c79c8954b12c1bdb5f388e269"
|
|
30
30
|
}
|