UNPKG

@truenewx/tnxet

Version:

互联网技术解决方案:Electron扩展支持

57 lines (47 loc) 1.59 kB
/** * 对Electron渲染进程的扩展支持 */ import Tnx from '../../tnxcore/src/tnxcore.ts'; import * as meta from '../../tnxcore/src/api/meta.ts'; import * as XmlUtil from './util/xml.ts'; import ipc from './ipc/index.ts'; import constants from './constants.ts'; export default class TnxEt extends Tnx { ipc = ipc; declare util: typeof Tnx.prototype.util & { xml?: typeof XmlUtil }; private command = { listeners: {}, subscribe(name: string, listener: (...args: any[]) => void): void { this.listeners[name] = listener; }, unsubscribe(name: string): void { delete this.listeners[name]; }, } constructor(baseUrl: string, id: string = 'tnxet') { super(baseUrl, id); this.util.xml = Object.assign({}, XmlUtil); meta.addEnumType({ name: 'Boolean', items: [{ key: 'true', caption: '是', }, { key: 'false', caption: '否', }], }); const {ipcRenderer} = require('electron'); // 在此处导入才能避免初始化加载electron时加载主进程库而出错 ipcRenderer.on(constants.channel.command, (event: any, data: { name: string, args?: any[] }): void => { let listener = this.command.listeners[data.name]; if (listener) { let args = data.args || []; listener(...args); } } ); } }