Skip to content

Commit

Permalink
Improve extractAssets to minimize memory and CPU consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
Ala Hawash committed Mar 21, 2024
1 parent 3f3cff9 commit 4737747
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/mdctl-core/streams/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const _ = require('lodash'),
},
},
NON_WRITABLE_KEYS = ['facet'],
SectionsCreated = [],
sectionsWithResources = [],
{ privatesAccessor } = require('@medable/mdctl-core-utils/privates'),
crypto = require('crypto')

Expand All @@ -51,16 +51,25 @@ class ExportSection {
scriptFiles: [],
extraFiles: [],
templateFiles: [],
resourcePaths: [],
id: uuid.v4()
})
if (new.target === ExportSection) {
Object.seal(this)
}
if (this.isWritable) {
SectionsCreated.push(this)
const nodes = jp.nodes(content, '$..resourceId')
if (nodes.length > 0) {
privatesAccessor(this).resourcePaths.push(...nodes)
sectionsWithResources.push(this)
}
}
}

static clearSectionsWithResources() {
sectionsWithResources.length = 0
}

get id() {
return privatesAccessor(this).id
}
Expand All @@ -85,6 +94,10 @@ class ExportSection {
return privatesAccessor(this).templateFiles
}

get resourcePaths() {
return privatesAccessor(this).resourcePaths
}

get name() {
const { content, key } = privatesAccessor(this),
{
Expand Down Expand Up @@ -183,10 +196,9 @@ class ExportSection {
extractAssets() {
const facet = privatesAccessor(this).content
let itemSource = null
for (let i = 0; i < SectionsCreated.length; i += 1) {
const sc = SectionsCreated[i],
nodes = jp.nodes(sc.content, '$..resourceId'),
item = _.find(nodes, n => n.value === facet.resourceId)
for (let i = 0; i < sectionsWithResources.length; i += 1) {
const sc = sectionsWithResources[i],
item = _.find(sc.resourcePaths, n => n.value === facet.resourceId)
if (item) {
// replace last path
const ETagPathItem = _.clone(item.path)
Expand Down

0 comments on commit 4737747

Please sign in to comment.