mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 00:03:23 +00:00
✨ IMPROVE: Clean up async initialization pattern
- Remove unnecessary initializeTemplateEngine() from constructor - Rename initializeLiquidJS() to ensureLiquidJSInitialized() for clarity - Make template engine loading truly lazy (only on first template render) - Eliminate potential timing issues with constructor async calls - Improve code clarity and maintainability Now template engines are only loaded when actually needed for rendering. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@xtr-dev/payload-mailing",
|
||||
"version": "0.0.11",
|
||||
"version": "0.0.12",
|
||||
"description": "Template-based email system with scheduling and job processing for PayloadCMS",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
|
||||
@@ -31,7 +31,6 @@ export class MailingService implements IMailingService {
|
||||
this.emailsCollection = typeof emailsConfig === 'string' ? emailsConfig : 'emails'
|
||||
|
||||
this.initializeTransporter()
|
||||
this.initializeTemplateEngine()
|
||||
}
|
||||
|
||||
private initializeTransporter(): void {
|
||||
@@ -63,31 +62,11 @@ export class MailingService implements IMailingService {
|
||||
return fromEmail || ''
|
||||
}
|
||||
|
||||
private initializeTemplateEngine(): void {
|
||||
// Skip initialization if custom template renderer is provided
|
||||
if (this.config.templateRenderer) {
|
||||
return
|
||||
}
|
||||
|
||||
// Use specified template engine or default to 'liquidjs'
|
||||
const engine = this.config.templateEngine || 'liquidjs'
|
||||
|
||||
if (engine === 'liquidjs') {
|
||||
// LiquidJS will be initialized lazily on first use
|
||||
this.liquid = null
|
||||
} else if (engine === 'mustache') {
|
||||
// Mustache will be loaded dynamically on first use
|
||||
this.liquid = null
|
||||
} else if (engine === 'simple') {
|
||||
this.liquid = null
|
||||
}
|
||||
}
|
||||
|
||||
private async initializeLiquidJS(): Promise<void> {
|
||||
private async ensureLiquidJSInitialized(): Promise<void> {
|
||||
if (this.liquid !== null) return // Already initialized or failed
|
||||
|
||||
try {
|
||||
const liquidModule = await import('liquidjs') as any
|
||||
const liquidModule = await import('liquidjs')
|
||||
const { Liquid: LiquidEngine } = liquidModule
|
||||
this.liquid = new LiquidEngine()
|
||||
|
||||
@@ -403,7 +382,7 @@ export class MailingService implements IMailingService {
|
||||
// Use LiquidJS if configured
|
||||
if (engine === 'liquidjs') {
|
||||
try {
|
||||
await this.initializeLiquidJS()
|
||||
await this.ensureLiquidJSInitialized()
|
||||
if (this.liquid && typeof this.liquid !== 'boolean') {
|
||||
return await this.liquid.parseAndRender(template, variables)
|
||||
}
|
||||
@@ -430,7 +409,7 @@ export class MailingService implements IMailingService {
|
||||
|
||||
private async renderWithMustache(template: string, variables: Record<string, any>): Promise<string | null> {
|
||||
try {
|
||||
const mustacheModule = await import('mustache') as any
|
||||
const mustacheModule = await import('mustache')
|
||||
const Mustache = mustacheModule.default || mustacheModule
|
||||
return Mustache.render(template, variables)
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user