|
1 | | -import { transformExtensionCode } from '../utils/extensionCodeTransformer.js' |
2 | | - |
3 | 1 | export const useAppStore = defineStore("app", () => { |
4 | 2 | const stores = [] |
5 | 3 |
|
@@ -69,143 +67,10 @@ export const useAppStore = defineStore("app", () => { |
69 | 67 | console.log(`[AppStore] Imported ${importedCount} stores`) |
70 | 68 | } |
71 | 69 |
|
72 | | - const loadedExtensions = ref(new Map()) |
73 | | - const extensionAPI = ref(null) |
74 | | - |
75 | | - function setExtensionAPI(api) { |
76 | | - extensionAPI.value = api |
77 | | - } |
78 | | - |
79 | | - async function loadExtension(path) { |
80 | | - try { |
81 | | - if (loadedExtensions.value.has(path)) { |
82 | | - console.warn(`[AppStore] Extension already loaded from this path: ${path}`) |
83 | | - throw new Error('This extension file is already loaded') |
84 | | - } |
85 | | - |
86 | | - if (!extensionAPI.value) { |
87 | | - throw new Error("Extension API not initialized") |
88 | | - } |
89 | | - |
90 | | - let finalURL = path |
91 | | - |
92 | | - if (path.startsWith('blob:')) { |
93 | | - const response = await fetch(path) |
94 | | - const code = await response.text() |
95 | | - const transformedCode = transformExtensionCode(code) |
96 | | - |
97 | | - const newBlob = new Blob([transformedCode], { type: 'application/javascript' }) |
98 | | - finalURL = URL.createObjectURL(newBlob) |
99 | | - } |
100 | | - |
101 | | - const extensionModule = await import(finalURL) |
102 | | - |
103 | | - if (finalURL !== path && finalURL.startsWith('blob:')) { |
104 | | - URL.revokeObjectURL(finalURL) |
105 | | - } |
106 | | - |
107 | | - const extensionName = extensionModule.metadata?.name |
108 | | - if (extensionName) { |
109 | | - const alreadyLoaded = Array.from(loadedExtensions.value.values()).find( |
110 | | - ext => ext.metadata?.name === extensionName |
111 | | - ) |
112 | | - if (alreadyLoaded) { |
113 | | - console.warn(`[AppStore] Extension "${extensionName}" is already loaded`) |
114 | | - throw new Error(`Extension "${extensionName}" is already loaded.`) |
115 | | - } |
116 | | - } |
117 | | - |
118 | | - if (typeof extensionModule.install === 'function') { |
119 | | - await extensionModule.install(extensionAPI.value, path) |
120 | | - |
121 | | - const extensionData = { |
122 | | - module: extensionModule, |
123 | | - path, |
124 | | - loadedAt: new Date().toISOString(), |
125 | | - metadata: extensionModule.metadata || {}, |
126 | | - enabled: true, |
127 | | - } |
128 | | - loadedExtensions.value.set(path, extensionData) |
129 | | - |
130 | | - console.log(`[AppStore] Extension loaded successfully: ${path}`) |
131 | | - |
132 | | - return extensionModule |
133 | | - } else { |
134 | | - throw new Error('Extension must export an install function') |
135 | | - } |
136 | | - } catch (error) { |
137 | | - console.error(`[AppStore] Failed to load extension from ${path}:`, error) |
138 | | - throw error |
139 | | - } |
140 | | - } |
141 | | - |
142 | | - function getLoadedExtensions() { |
143 | | - return Array.from(loadedExtensions.value.values()) |
144 | | - } |
145 | | - |
146 | | - function unloadExtension(path) { |
147 | | - if (loadedExtensions.value.has(path)) { |
148 | | - const extensionData = loadedExtensions.value.get(path) |
149 | | - |
150 | | - if (extensionData.module && typeof extensionData.module.uninstall === 'function') { |
151 | | - try { |
152 | | - extensionData.module.uninstall(extensionAPI.value, path) |
153 | | - console.log(`[AppStore] Extension uninstall called: ${path}`) |
154 | | - } catch (error) { |
155 | | - console.error(`[AppStore] Error calling uninstall for ${path}:`, error) |
156 | | - } |
157 | | - } |
158 | | - |
159 | | - if (extensionAPI.value) { |
160 | | - extensionAPI.value.unregisterToolsByExtension(path) |
161 | | - } |
162 | | - |
163 | | - loadedExtensions.value.delete(path) |
164 | | - console.log(`[AppStore] Extension unloaded: ${path}`) |
165 | | - return true |
166 | | - } |
167 | | - return false |
168 | | - } |
169 | | - |
170 | | - function toggleExtension(path) { |
171 | | - if (loadedExtensions.value.has(path)) { |
172 | | - const extensionData = loadedExtensions.value.get(path) |
173 | | - extensionData.enabled = !extensionData.enabled |
174 | | - console.log(`[AppStore] Extension ${extensionData.enabled ? 'enabled' : 'disabled'}: ${path}`) |
175 | | - return extensionData.enabled |
176 | | - } |
177 | | - return false |
178 | | - } |
179 | | - |
180 | | - function setExtensionEnabled(path, enabled) { |
181 | | - if (loadedExtensions.value.has(path)) { |
182 | | - const extensionData = loadedExtensions.value.get(path) |
183 | | - extensionData.enabled = enabled |
184 | | - console.log(`[AppStore] Extension ${enabled ? 'enabled' : 'disabled'}: ${path}`) |
185 | | - return true |
186 | | - } |
187 | | - return false |
188 | | - } |
189 | | - |
190 | | - function getExtensionEnabled(path) { |
191 | | - if (loadedExtensions.value.has(path)) { |
192 | | - return loadedExtensions.value.get(path).enabled |
193 | | - } |
194 | | - return false |
195 | | - } |
196 | | - |
197 | 70 | return { |
198 | 71 | stores, |
199 | 72 | registerStore, |
200 | 73 | exportStores, |
201 | 74 | importStores, |
202 | | - loadedExtensions, |
203 | | - setExtensionAPI, |
204 | | - loadExtension, |
205 | | - getLoadedExtensions, |
206 | | - unloadExtension, |
207 | | - toggleExtension, |
208 | | - setExtensionEnabled, |
209 | | - getExtensionEnabled, |
210 | 75 | } |
211 | 76 | }) |
0 commit comments