UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

151 lines (150 loc) 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExtensionContextImpl = void 0; const TemplateMetadataImpl_1 = require("./TemplateMetadataImpl"); const utils_1 = require("../../utils"); class ExtensionContextImpl { constructor(node, moduleContext) { this.moduleScanFilesMaster = []; this.perTargetState = new Map(); this.node = node; this.moduleContext = moduleContext; this.templateMetadata = new TemplateMetadataImpl_1.TemplateMetadataImpl(); } getActiveTargetKey() { return this.currentTarget?.getTargetName() ?? '__global__'; } getOrCreateState(key) { let state = this.perTargetState.get(key); if (!state) { state = { scanFiles: [], analyzeResults: new Set(), currentView: [], currentFilePath: undefined, routerMap: [], generatedPaths: new Map(), }; this.perTargetState.set(key, state); } return state; } getModuleScanFiles() { return this.moduleScanFilesMaster; } get currentView() { const key = this.getActiveTargetKey(); return this.getOrCreateState(key).currentView; } addAnalyzeResults(results) { const key = this.getActiveTargetKey(); const state = this.getOrCreateState(key); if (Array.isArray(results)) { results.forEach((result) => { if (!this.validateAnalyzeResult(result)) { utils_1.Logger.warn(this.node.getNodeName(), `Invalid analyze result: ${JSON.stringify(result)}`); return; } state.analyzeResults.add(result); }); } else { if (!this.validateAnalyzeResult(results)) { utils_1.Logger.warn(this.node.getNodeName(), `Invalid analyze result: ${JSON.stringify(results)}`); return; } state.analyzeResults.add(results); } this.syncToCurrentView(results); } getAnalyzeResults() { const key = this.getActiveTargetKey(); return this.getOrCreateState(key).analyzeResults; } getTemplateData(componentName) { return this.templateMetadata.getTemplateData(componentName); } getTemplateDataMap() { return this.templateMetadata.templateDataMap; } addTemplateData(componentName, data) { this.templateMetadata.addTemplateData(componentName, data); } validateAnalyzeResult(result) { return result.name !== undefined && result.sourceFilePath !== undefined && result.annotation !== undefined; } syncToCurrentView(results) { const key = this.getActiveTargetKey(); const state = this.getOrCreateState(key); if (!state.currentFilePath) { return; } ; const resultsArray = Array.isArray(results) ? results : [results]; const currentFileResults = resultsArray.filter(result => result.sourceFilePath === state.currentFilePath); state.currentView.push(...currentFileResults); } setCurrentFilePath(filePath) { const key = this.getActiveTargetKey(); const state = this.getOrCreateState(key); state.currentFilePath = filePath; state.currentView = []; } get scanFiles() { const key = this.getActiveTargetKey(); if (key === '__global__') { return this.moduleScanFilesMaster; } return this.getOrCreateState(key).scanFiles; } set scanFiles(files) { const key = this.getActiveTargetKey(); if (key === '__global__') { this.moduleScanFilesMaster = files; } else { this.getOrCreateState(key).scanFiles = files; } } get analyzeResults() { const key = this.getActiveTargetKey(); return this.getOrCreateState(key).analyzeResults; } set analyzeResults(v) { const key = this.getActiveTargetKey(); this.getOrCreateState(key).analyzeResults = v; } get _currentView() { const key = this.getActiveTargetKey(); return this.getOrCreateState(key).currentView; } set _currentView(v) { const key = this.getActiveTargetKey(); this.getOrCreateState(key).currentView = v; } get _currentFilePath() { const key = this.getActiveTargetKey(); return this.getOrCreateState(key).currentFilePath; } set _currentFilePath(v) { const key = this.getActiveTargetKey(); this.getOrCreateState(key).currentFilePath = v; } get routerMap() { const key = this.getActiveTargetKey(); return this.getOrCreateState(key).routerMap; } set routerMap(v) { const key = this.getActiveTargetKey(); this.getOrCreateState(key).routerMap = v; } get generatedPaths() { const key = this.getActiveTargetKey(); return this.getOrCreateState(key).generatedPaths; } set generatedPaths(v) { const key = this.getActiveTargetKey(); this.getOrCreateState(key).generatedPaths = v; } } exports.ExtensionContextImpl = ExtensionContextImpl;