From a4fc7237db14962d2f8cb7332171fecceed63ed5 Mon Sep 17 00:00:00 2001 From: DCloud_MQ Date: Wed, 8 May 2024 12:32:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E4=BA=91=E7=AB=AF?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E6=A1=86=E6=9E=B6=E4=BB=85=E5=9C=A8=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E5=88=B0=E5=8F=AF=E5=86=99=E7=9A=84=E4=BA=91=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E6=97=B6=E6=89=8D=E8=BE=93=E5=87=BA=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cloudfunctions/common/uni-im-ext/index.js | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/uniCloud/cloudfunctions/common/uni-im-ext/index.js b/uniCloud/cloudfunctions/common/uni-im-ext/index.js index 5015171..aa4fd39 100644 --- a/uniCloud/cloudfunctions/common/uni-im-ext/index.js +++ b/uniCloud/cloudfunctions/common/uni-im-ext/index.js @@ -67,6 +67,7 @@ const fs = require('fs'); const path = require('path'); const REGISTER_EXT_CACHE = './_registered_ext.js' +let isFsWritable = false function getSubdirs(parent) { try { @@ -166,18 +167,26 @@ function loadExtensions() { requires.push(`const e_${extSeq} = require('${modDir.replace(/\\/g, '/')}');`) extSeq ++ } - moduleExports = Object.keys(moduleExports).map(exp => { - return `"${exp}": [${moduleExports[exp].join(', ')}]` - }).join(',\n') - // 输出缓存文件 - let registeredExtCache = ` + // 把扫描到的扩展模块写入缓存文件 + if (isFsWritable) { + moduleExports = Object.keys(moduleExports).map(exp => { + return `"${exp}": [${moduleExports[exp].join(', ')}]` + }).join(',\n') + + try { + // 输出缓存文件 + let registeredExtCache = ` ${requires.join('\n')} module.exports = { ${moduleExports} } ` - fs.writeFileSync(path.join(__dirname, REGISTER_EXT_CACHE), registeredExtCache) + fs.writeFileSync(path.join(__dirname, REGISTER_EXT_CACHE), registeredExtCache) + } catch (e) { + // 无法写缓存文件? + } + } return registeredExtensionPoints } @@ -191,12 +200,19 @@ let _registeredExtensionPointsPromise * @returns {any[]} 该扩展点上挂接的所有扩展程序各自的返回值拼装在一个数组里。 */ async function invokeExts(extensionPointName, ...args) { + isFsWritable = (this.getCloudInfo().provider === 'aliyun') + // 尝试从生成的缓存文件中加载 - let registeredExtensionPoints = {} - try { - registeredExtensionPoints = require(REGISTER_EXT_CACHE) - } catch (e) { - // 尚无缓存 + let registeredExtensionPoints + if (isFsWritable) { + try { + registeredExtensionPoints = require(REGISTER_EXT_CACHE) + } catch (e) { + // 尚未生成缓存文件 + } + } + + if (!registeredExtensionPoints) { // 懒加载,避免在调用接口重入时重复解析扩展模块 if (!_registeredExtensionPointsPromise) { _registeredExtensionPointsPromise = new Promise(resolve => { @@ -207,6 +223,7 @@ async function invokeExts(extensionPointName, ...args) { } // console.log('invokeExts:', Object.keys(registeredExtensionPoints), extensionPointName, args) + // 找到指定的扩展点并调用 if (!registeredExtensionPoints[extensionPointName]) return [] return Promise.all( registeredExtensionPoints[extensionPointName] -- GitLab