echoes/frontend/hooks/extensionService.tsx

41 lines
1.4 KiB
TypeScript
Raw Normal View History

// File path: /d:/data/echoes/frontend/hooks/extensionService.tsx
/**
* Hook
*
* 访 Hook
*/
import { createContext, useContext, ReactNode } from 'react';
import { ExtensionService } from 'service/extensionService';
// 创建扩展服务上下文
const ExtensionContext = createContext<ExtensionService | undefined>(undefined);
/**
*
*
* @param children -
*/
export function ExtensionProvider({ children }: { children: ReactNode }) {
const extensionService = ExtensionService.getInstance(); // 获取扩展服务实例
return (
<ExtensionContext.Provider value={extensionService}>
{children}
</ExtensionContext.Provider>
)
}
/**
* Hook访
*
* @returns {ExtensionService} -
* @throws {Error} - ExtensionProvider
*/
export function useExtention(): ExtensionService {
const context = useContext(ExtensionContext); // 获取扩展服务上下文
if (context === undefined) {
throw new Error('useTheme must be used within a ThemeProvider'); // 抛出错误
}
return context; // 返回扩展服务实例
}