|
| 1 | +const workerCode = () => { |
| 2 | + // eslint-disable-next-line no-undef |
| 3 | + self.onmessage = function (event) { |
| 4 | + // The object that the web page sent is stored in the event.data property. |
| 5 | + const Utils = { |
| 6 | + getMetadataField (key, field = null) { |
| 7 | + if (key === undefined) { |
| 8 | + return null; |
| 9 | + } |
| 10 | + |
| 11 | + let currentObject; |
| 12 | + let nextObject = event.data.params.metadata; |
| 13 | + let skipped = false; |
| 14 | + |
| 15 | + // eslint-disable-next-line no-restricted-syntax |
| 16 | + key.split('.').forEach((item) => { |
| 17 | + if ( |
| 18 | + currentObject != null |
| 19 | + && currentObject?.container === true |
| 20 | + && !(item in nextObject) |
| 21 | + ) { |
| 22 | + if (skipped) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + // skip the list element, e.g. "E"! |
| 27 | + // console.debug(`Skip ${item} at ${nextObject.label}`); |
| 28 | + skipped = true; |
| 29 | + } else { |
| 30 | + skipped = false; |
| 31 | + |
| 32 | + if (item in nextObject) { |
| 33 | + currentObject = nextObject[item]; |
| 34 | + if ('children' in currentObject) { |
| 35 | + nextObject = currentObject.children; |
| 36 | + } |
| 37 | + } else { |
| 38 | + currentObject = null; |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + if (currentObject) { |
| 44 | + return field ? currentObject[field] : currentObject; |
| 45 | + } |
| 46 | + |
| 47 | + return null; |
| 48 | + }, |
| 49 | + |
| 50 | + flatten (obj, path = '') { |
| 51 | + if (!(obj instanceof Object)) { |
| 52 | + // eslint-disable-next-line no-new-object |
| 53 | + const newObj = new Object(); |
| 54 | + newObj[path.replace(/\.$/g, '')] = obj; |
| 55 | + return newObj; |
| 56 | + } |
| 57 | + |
| 58 | + return Object.keys(obj).reduce( |
| 59 | + (output, key) => (obj instanceof Array |
| 60 | + ? Object.assign(output, Utils.flatten(obj[key], `${path}[${key}].`)) |
| 61 | + : Object.assign(output, Utils.flatten(obj[key], `${path + key}.`))), |
| 62 | + {}, |
| 63 | + ); |
| 64 | + }, |
| 65 | + }; |
| 66 | + |
| 67 | + const REAL_TYPE = { |
| 68 | + INT: 'int', |
| 69 | + FLOAT: 'float', |
| 70 | + BOOL: 'bool', |
| 71 | + STR: 'str', |
| 72 | + FUNC: 'func', |
| 73 | + DICT: 'dict', |
| 74 | + DICT_DICT: 'dict(dict)', |
| 75 | + }; |
| 76 | + const SUPPORTED_TYPES = [ |
| 77 | + REAL_TYPE.INT, |
| 78 | + REAL_TYPE.FLOAT, |
| 79 | + REAL_TYPE.STR, |
| 80 | + REAL_TYPE.BOOL, |
| 81 | + ]; |
| 82 | + const { data } = event.data.params; |
| 83 | + const flattened = Utils.flatten(data); |
| 84 | + const paramKeys = Object.keys(flattened); |
| 85 | + const filteredKeys = paramKeys.filter((key) => { |
| 86 | + // TODO: avoid to fetch field twice! |
| 87 | + const field = Utils.getMetadataField(`netParams.${key}`); |
| 88 | + if (field && SUPPORTED_TYPES.includes(field.type)) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + return false; |
| 92 | + }); |
| 93 | + |
| 94 | + // eslint-disable-next-line no-undef |
| 95 | + postMessage({ resultMessage: 'OK', params: { results: filteredKeys } }); |
| 96 | + }; |
| 97 | +}; |
| 98 | + |
| 99 | +let code = workerCode.toString(); |
| 100 | +code = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}')); |
| 101 | + |
| 102 | +const blob = new Blob([code], { type: 'application/javascript' }); |
| 103 | +const workerScript = window.URL.createObjectURL(blob); |
| 104 | + |
| 105 | +export default workerScript; |
0 commit comments