@live-change/vue3-components 0.2.14 → 0.2.16

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/data.js ADDED
@@ -0,0 +1,74 @@
1
+ import { getCurrentInstance } from 'vue'
2
+
3
+ export function defaultData(definition, otherSrc) {
4
+ if(!definition) return undefined
5
+ let result = definition.defaultValue || otherSrc
6
+ if(definition.properties) {
7
+ result = result || {}
8
+ for(let name in definition.properties) {
9
+ result[name] = defaultData(definition.properties[name], result?.[name])
10
+ }
11
+ } else if(definition.type == 'Array') {
12
+ result = result || []
13
+ for(let i = 0; i < result.length; i++) {
14
+ result[i] = defaultData(definition.of, result[i])
15
+ }
16
+ }
17
+ return result
18
+ }
19
+
20
+ export function validateData(definition, data, validationType = 'validation',
21
+ context = undefined, propName = '', props = data) {
22
+ context = context || getCurrentInstance().appContext
23
+ //console.log("VALIDATIE DATA", definition, data, validationType, context, propName, props)
24
+ if(!context) throw new Error("No context")
25
+ const validators = context.config.globalProperties.$validators
26
+ const validationContext = { source: data, props, propName, definition, validators }
27
+ if(!definition) return undefined
28
+ const validations = definition[validationType]
29
+ //console.log("VALIDATIONS!", validations)
30
+ if(validations) {
31
+ for(const validation of validations) {
32
+ const validator = typeof validation == 'string'
33
+ ? validators[validation]({}, validationContext)
34
+ : validators[validation.name](validation.params, validationContext)
35
+ if(!validator) throw new Error(`Validator ${validation.name || validation} not found`)
36
+ const error = validator(data, context)
37
+ if(error) return error
38
+ }
39
+ }
40
+ if(definition.properties) {
41
+ const propertyErrors = {}
42
+ for(let name in definition.properties) {
43
+ const error = validateData(definition.properties[name], data?.[name], validationType, context,
44
+ propName ? propName + '.' + name: name, props)
45
+ if(error) {
46
+ if(error.propertyErrors) {
47
+ for(let internalName in error.propertyErrors) {
48
+ propertyErrors[name + '.' + internalName] = error.propertyErrors[internalName]
49
+ }
50
+ } else {
51
+ propertyErrors[name] = error
52
+ }
53
+ }
54
+ }
55
+ if(Object.keys(propertyErrors).length > 0) return { propertyErrors }
56
+ } else if(definition.type == 'Array') {
57
+ const propertyErrors = {}
58
+ for(let i = 0; i < data.length; i++) {
59
+ const error = validateData(definition.of, data[i], validationType, context,
60
+ propName ? propName + '.' + name : name, props)
61
+ const name = '' + i
62
+ if(error) {
63
+ if(error.propertyErrors) {
64
+ for(let internalName in error.propertyErrors) {
65
+ propertyErrors[name + '.' + internalName] = error.propertyErrors[internalName]
66
+ }
67
+ } else {
68
+ propertyErrors[name] = error
69
+ }
70
+ }
71
+ }
72
+ if(Object.keys(propertyErrors).length > 0) return { propertyErrors }
73
+ }
74
+ }
package/logic/index.js CHANGED
@@ -14,6 +14,9 @@ export { synchronized }
14
14
  import { synchronizedList } from "./synchronizedList.js"
15
15
  export { synchronizedList }
16
16
 
17
+ import { defaultData, validateData } from "./data.js"
18
+ export { defaultData, validateData }
19
+
17
20
  function registerLogicComponents(app) {
18
21
  app.component("loading", Loading)
19
22
  app.component("loading-zone", LoadingZone)
@@ -21,4 +24,4 @@ function registerLogicComponents(app) {
21
24
  app.component("observe", Observe)
22
25
  }
23
26
 
24
- export { registerLogicComponents }
27
+ export { registerLogicComponents }
@@ -20,6 +20,8 @@ function synchronized(options) {
20
20
  timeSource = () => (new Date()).toISOString(),
21
21
  onChange = () => {},
22
22
  onSave = () => {},
23
+ onSaveError = (e) => { console.error("SAVE ERROR", e) },
24
+ resetOnError = true,
23
25
  recursive = false,
24
26
  throttle = 300,
25
27
  autoSave = true
@@ -49,8 +51,14 @@ function synchronized(options) {
49
51
  // console.log("LAST REMOTE UPDATE", ((source.value && source.value[timeField]) ?? ''))
50
52
  // console.log("SOURCE JSON", JSON.stringify(source.value))
51
53
  // console.log("SYNCHRONIZED JSON", JSON.stringify(synchronizedValue.value))
52
- await update({ ...data, [timeField]: lastLocalUpdate.value, ...identifiers })
53
- onSave()
54
+ try {
55
+ await update({ ...data, [timeField]: lastLocalUpdate.value, ...identifiers })
56
+ try { onSave() } catch(e) { console.error("ON SAVE HANDLER ERROR", e) }
57
+ } catch(e) {
58
+ if(resetOnError) synchronizedValue.value = copy(source.value)
59
+ console.error("SAVE ERROR", e)
60
+ onSaveError(e)
61
+ }
54
62
  return true
55
63
  }
56
64
  const throttledSave = throttle ? useThrottleFn(save, throttle) : save
@@ -83,8 +91,14 @@ function synchronized(options) {
83
91
  || ( ((local.value && local.value[timeField]) ?? '')
84
92
  <= ((source.value && source.value[timeField]) ?? ''))) return false // identical, no need to save
85
93
  const data = JSON.parse(JSON.stringify(local.value))
86
- await update({ ...data, ...identifiers })
87
- onSave()
94
+ try {
95
+ await update({...data, ...identifiers})
96
+ try { onSave() } catch(e) { console.error("ON SAVE HANDLER ERROR", e) }
97
+ } catch(e) {
98
+ if(resetOnError) synchronizedValue.value = source.value
99
+ console.error("SAVE ERROR", e)
100
+ onSaveError(e)
101
+ }
88
102
  return true
89
103
  }
90
104
  const throttledSave = throttle ? useThrottleFn(save, throttle) : save
@@ -105,4 +119,4 @@ function synchronized(options) {
105
119
  }
106
120
 
107
121
  export { synchronized }
108
- export default synchronized
122
+ export default synchronized
@@ -49,13 +49,16 @@ function synchronizedList(options) {
49
49
  timeSource = () => (new Date()).toISOString(),
50
50
  onChange = () => {},
51
51
  onSave = () => {},
52
+ onSaveError = (e) => { console.error("SAVE ERROR", e) },
53
+ resetOnError = true,
52
54
  recursive = false,
53
55
  throttle = 300,
54
56
  autoSave = true,
55
57
  mapper = source => synchronized({
56
- source, update: updateAction, identifiers: { ...identifiers, ...objectIdentifiers(source.value) }, timeField, timeSource,
57
- recursive, throttle, autoSave, onSave, onChange
58
- })
58
+ source, update: updateAction, identifiers: { ...identifiers, ...objectIdentifiers(source.value) },
59
+ timeField, timeSource,
60
+ recursive, throttle, autoSave, onSave, onSaveError, resetOnError, onChange
61
+ })
59
62
  } = options
60
63
  if(!source) throw new Error('source must be defined')
61
64
  const synchronizedList = ref([])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/vue3-components",
3
- "version": "0.2.14",
3
+ "version": "0.2.16",
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.14",
24
+ "@live-change/vue3-ssr": "^0.2.16",
25
25
  "debug": "^4.3.4",
26
26
  "mitt": "3.0.0",
27
- "vue": "^3.2.33"
27
+ "vue": "^3.2.37"
28
28
  },
29
- "gitHead": "f50a4c0ce7348ff25bcc59ec2de24a1da18a3210"
29
+ "gitHead": "a11c281e5913e7bc26955bbc1676dd8153ebc486"
30
30
  }