eventMerge.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. const fe = Editor.require('packages://simple-code/tools/tools.js');
  3. const fs = require('fs');
  4. const path = require("path");
  5. var eventFuncs =
  6. {
  7. // 合并事件
  8. eventMerge(old_msg,moduleFile="panel_ex.js"){
  9. let fileList = fe.getDirAllFiles(Editor.url("packages://simple-code/extensions"),[])
  10. let messages = {}
  11. let scripts = []
  12. fileList.forEach((v)=>
  13. {
  14. if(v.substr(v.lastIndexOf(path.sep)+1) == moduleFile){
  15. let obj = require(v);
  16. scripts.push(obj);
  17. for(let name in obj.messages){
  18. // electron.ipcRenderer.on(name,obj[name].bind(obj)); // 只能监听其它面板发送的事件
  19. // 合并监听函数
  20. if (messages[name])
  21. {
  22. let old_func = messages[name]
  23. let now_func = obj.messages[name].bind(obj)
  24. messages[name] = function(...args){
  25. old_func(...args,old_msg);
  26. now_func(...args,old_msg);
  27. }
  28. }else{
  29. let now_func = obj.messages[name].bind(obj)
  30. messages[name] = function(...args){ now_func(...args,old_msg)}
  31. }
  32. }
  33. }
  34. })
  35. for(let name in old_msg){
  36. if (messages[name])
  37. {
  38. let old_func = old_msg[name]
  39. let now_func = messages[name]
  40. messages[name] = function(...args){
  41. (old_func.bind(this))(...args,old_msg);
  42. now_func(...args,old_msg);
  43. }
  44. }else{
  45. messages[name] = old_msg[name]
  46. }
  47. }
  48. return {messages,scripts}
  49. },
  50. };
  51. module.exports = eventFuncs;