Skip to content

Commit

Permalink
Merge pull request #1014 from dbauszus-glx/clone-template
Browse files Browse the repository at this point in the history
strcturedClone template in getLayer
  • Loading branch information
RobAndrewHurst authored Nov 22, 2023
2 parents bd49a07 + 87f7ab3 commit dc22e27
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
7 changes: 3 additions & 4 deletions mod/utils/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,20 @@ module.exports = function mergeDeep(target, ...sources) {
// Call recursive merge for target key object.
Object.hasOwn(target, key) && mergeDeep(target[key], source[key]);

// Target and Source are both arrays.
// Target and Source are both arrays.
} else if (Array.isArray(source[key]) && Array.isArray(target[key])) {


// Target and Source arrays are equal.
if (source[key].every(item => target[key].includes(item))) {

// Do not merge.
target[key] = source[key]

} else {

// Merge unequal arrays.

target[key] = [...target[key]||[], ...source[key]]
target[key] = [...target[key] || [], ...source[key]]
}

} else {
Expand Down
32 changes: 17 additions & 15 deletions mod/workspace/getLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ module.exports = async (params) => {

const locale = await getLocale(params)

if (locale instanceof Error) {

return locale
}
if (locale instanceof Error) return locale

const roles = params.user?.roles || []

Expand All @@ -35,31 +32,36 @@ module.exports = async (params) => {

let layer = locale.layers[params.layer]

// Return already merged layer.
if (layer.merged) return layer

// Assign key value as key on layer object.
layer.key ??= params.layer

// Merge layer --> template
if (Object.hasOwn(workspace.templates, layer.template || layer.key)) {

// Merge the workspace template into the layer.
layer = merge(await getTemplate(workspace.templates[layer.template || layer.key]), layer)
}
let template = structuredClone(await getTemplate(workspace.templates[layer.template || layer.key]))

if (Array.isArray(layer.templates)) for (const key of layer.templates){

let template = Object.hasOwn(workspace.templates, key) && await getTemplate(workspace.templates[key])

// Merge the workspace template into the layer.
layer = merge(template, layer)
layer = merge(template, layer)
}

// Check for layer geom[s].
if ((layer.table || layer.tables) && (!layer.geom && !layer.geoms)) {
// Merge templates --> layer
for (const key of layer.templates || []){

if (!Object.hasOwn(workspace.templates, key)) continue;

console.warn(`Layer: ${layer.key},has a table or tables defined, but no geom or geoms.`)
let template = structuredClone(await getTemplate(workspace.templates[key]))

// Merge the workspace template into the layer.
layer = merge(layer, template)
}

// Assign layer key as name with no existing name on layer object.
layer.name ??= layer.key

layer.merged = true

return layer
}
4 changes: 3 additions & 1 deletion mod/workspace/getLocale.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ module.exports = async (params) => {
// A template exists for the locale key.
if (Object.hasOwn(workspace.templates, params.locale)) {

let template = structuredClone(await getTemplate(workspace.templates[params.locale]))

// Merge the workspace template into workspace.
locale = merge(await getTemplate(workspace.templates[params.locale]), locale)
locale = merge(template, locale)
}

return locale
Expand Down

0 comments on commit dc22e27

Please sign in to comment.