Skip to content

Commit 1fdaba4

Browse files
committed
refactor: simplify pushOnceTo tag
1 parent 07d41c9 commit 1fdaba4

File tree

2 files changed

+30
-41
lines changed

2 files changed

+30
-41
lines changed

src/edge/stacks.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import { EOL } from 'node:os'
1111

1212
export default class Stacks {
13+
#contentSources: Map<string, Set<string>> = new Map()
14+
1315
/**
1416
* Pre-seeded content before the placeholder has been
1517
* defined.
@@ -71,6 +73,28 @@ export default class Stacks {
7173
return this
7274
}
7375

76+
/**
77+
* Push contents to a stack with a unique source id. A
78+
* source can only push once to a given stack.
79+
*/
80+
pushOnceTo(name: string, sourceId: string, contents: string) {
81+
const contentSources = this.#contentSources.get(name)
82+
if (contentSources && contentSources.has(sourceId)) {
83+
return
84+
}
85+
86+
this.pushTo(name, contents)
87+
88+
/**
89+
* Track source
90+
*/
91+
if (contentSources) {
92+
contentSources.add(sourceId)
93+
} else {
94+
this.#contentSources.set(name, new Set([sourceId]))
95+
}
96+
}
97+
7498
/**
7599
* Fill placeholders with their actual content
76100
*/

src/tags/push_once_to.ts

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { expressions } from 'edge-parser'
1212

1313
import { nanoid } from '../utils.js'
1414
import { TagContract } from '../types.js'
15-
import type { Template } from '../template.js'
1615

1716
declare module '../template.js' {
1817
export interface Template {
@@ -32,29 +31,6 @@ export const pushOnceToTag: TagContract & { generateId(): string } = {
3231
generateId() {
3332
return `stack_${nanoid()}`
3433
},
35-
boot(template) {
36-
/**
37-
* Tracking stack sources to avoid duplicate calls
38-
* from the same file:line:col
39-
*/
40-
template.getter(
41-
'stackSources',
42-
() => {
43-
return {}
44-
},
45-
true
46-
)
47-
48-
template.macro('trackStackSource', function (this: Template, stack, filename, line, col) {
49-
const key = `${stack}_${filename}_${line}_${col}`
50-
if (this.stackSources[key]) {
51-
return false
52-
}
53-
54-
this.stackSources[key] = true
55-
return true
56-
})
57-
},
5834
compile(parser, buffer, token) {
5935
const parsed = parser.utils.transformAst(
6036
parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename),
@@ -80,23 +56,13 @@ export const pushOnceToTag: TagContract & { generateId(): string } = {
8056
* Each stack must be unique
8157
*/
8258
const stackId = this.generateId()
83-
const stackName = parser.utils.stringify(parsed)
8459

8560
/**
8661
* Create a custom buffer for the stack. Since we do not want to the write
8762
* to the main buffer
8863
*/
8964
const stackBuffer = buffer.create(token.filename, { outputVar: stackId })
9065

91-
const { line, col } = token.loc.start
92-
const normalizedFileName = token.filename.replace(/\\|\//g, '_')
93-
const conditional = `template.trackStackSource(${stackName}, '${normalizedFileName}', ${line}, ${col})`
94-
95-
/**
96-
* Start if block
97-
*/
98-
buffer.writeStatement(`if (${conditional}) {`, token.filename, line)
99-
10066
/**
10167
* Process children
10268
*/
@@ -114,18 +80,17 @@ export const pushOnceToTag: TagContract & { generateId(): string } = {
11480
.disableTryCatchBlock()
11581
.flush(),
11682
token.filename,
117-
line
83+
token.loc.start.line
11884
)
11985

86+
const { line, col } = token.loc.start
87+
const normalizedFileName = token.filename.replace(/\\|\//g, '_')
88+
const sourceId = `${normalizedFileName}-${line}-${col}`
89+
12090
buffer.writeExpression(
121-
`template.stacks.pushTo(${parser.utils.stringify(parsed)}, ${stackId})`,
91+
`template.stacks.pushOnceTo(${parser.utils.stringify(parsed)}, '${sourceId}', ${stackId})`,
12292
token.filename,
12393
line
12494
)
125-
126-
/**
127-
* End if block
128-
*/
129-
buffer.writeStatement('}', token.filename, line)
13095
},
13196
}

0 commit comments

Comments
 (0)