[adapters] 增加主线程的类型提示文件,优化多线程通信方式,增加对多线程自定义扩展的支持

This commit is contained in:
SmallMain 2024-11-07 17:36:59 +08:00
parent 6333ac01d2
commit b97579d9c0
No known key found for this signature in database
9 changed files with 103 additions and 24 deletions

View File

@ -111,19 +111,19 @@ var cacheManager = {
init() {
this._cacheDir = getUserDataPath() + '/' + this.cacheDir;
worker.cacheManager.init(null, ([cachedFiles]) => {
worker.cacheManager.init(([cachedFiles]) => {
this.cachedFiles = new cc.AssetManager.Cache(cachedFiles);
});
},
clearCache() {
worker.cacheManager.clearCache(null, () => {
worker.cacheManager.clearCache(() => {
this.cachedFiles.clear();
});
},
clearLRU() {
worker.cacheManager.clearLRU(null, ([deletedFiles]) => {
worker.cacheManager.clearLRU(([deletedFiles]) => {
for (let i = 0, l = deletedFiles.length; i < l; i++) {
this.cachedFiles.remove(deletedFiles[i]);
}

View File

@ -5,7 +5,7 @@ var audio_worker = {
map: {},
timer: null,
create(callback, id) {
create(id) {
this.map[id] = {
audio: worker.createInnerAudioContext({ useWebAudioImplement: true }),
cache: {
@ -20,7 +20,7 @@ var audio_worker = {
}
},
call(callback, id, type, arg) {
call(id, type, arg) {
const audio = this.map[id].audio;
switch (type) {
case 0:
@ -73,20 +73,20 @@ var audio_worker = {
}
},
on(callback, id, type) {
on(id, type) {
const data = this.map[id];
data.audio["on" + type]((data.callbacks[type] = data => {
main.audioAdapter.onCallback(id, type, data);
}));
},
off(callback, id, type) {
off(id, type) {
const data = this.map[id];
data.audio["off" + type](data.callbacks[type]);
delete data.callbacks[type];
},
destroy(callback, id) {
destroy(id) {
this.map[id].destroy();
delete this.map[id];
},

View File

@ -88,12 +88,12 @@ var cacheManager_worker = {
},
download(
callback,
url,
options_reload,
options_header,
options_cacheEnabled,
options___cacheBundleRoot__,
callback,
) {
var result = this.transformUrl(url, options_reload);
if (result.inLocal) {
@ -109,17 +109,17 @@ var cacheManager_worker = {
return;
}
this.tempFiles[url] = path;
this.cacheFile(null, url, path, options_cacheEnabled, options___cacheBundleRoot__, true);
this.cacheFile(url, path, options_cacheEnabled, options___cacheBundleRoot__, true);
callback(null, path);
});
}
},
handleZip(
callback,
url,
options_header,
options___cacheBundleRoot__,
callback,
) {
let cachedUnzip = this.cachedFiles[url];
if (cachedUnzip) {
@ -138,7 +138,7 @@ var cacheManager_worker = {
}
},
getTemp(callback, url) {
getTemp(url, callback) {
callback(this.tempFiles.has(url) ? this.tempFiles.get(url) : '');
},
@ -208,7 +208,7 @@ var cacheManager_worker = {
checkNextPeriod = false;
},
cacheFile(callback, id, srcUrl, cacheEnabled, cacheBundleRoot, isCopy) {
cacheFile(id, srcUrl, cacheEnabled, cacheBundleRoot, isCopy, callback) {
cacheEnabled = cacheEnabled != null ? cacheEnabled : this.cacheEnabled;
if (!cacheEnabled || this.cacheQueue[id] || this.cachedFiles[id]) {
if (callback) callback(null);
@ -293,7 +293,7 @@ var cacheManager_worker = {
});
},
removeCache(callback, url) {
removeCache(url, callback) {
if (this.cachedFiles[url]) {
var self = this;
var path = this.cachedFiles[url].url;

View File

@ -1,6 +1,14 @@
require("./macro.js");
const { init } = require("./ipc-worker.js");
const { CC_CUSTOM_WORKER } = globalThis;
init(() => {
require("./handlers.js");
if (CC_CUSTOM_WORKER) {
try {
require("./custom/index.js");
} catch (error) {
console.error("worker init custom extension error:", error);
}
}
});

View File

@ -7,10 +7,10 @@
// 1.在 worker 端调用 registerHandler(name, obj) 注册处理对象。
// 2.所有非函数属性会生成 `get_xxx()`、`set_xxx(v)`、`write_xxx(v)` 三个函数,
// 其中,`write_` 函数会在设置完毕后回调到主线程。
// 3.所有函数属性请确保第一个参数是 callback用于回调到主线程,
// 3.在函数被调用时,会在最后一个参数传入 callback用于回调结果到主线程,
// 用法是 callback(...args)。
//
// 注册好函数后,在主线程通过 worker.name.key(args | null, (args)=>{}) 调用。
// 注册好函数后,在主线程通过 worker.name.key(args, args => {}) 或 worker.name.key(args => {}) 调用。
// 注意在主线程调用时传入和返回的都是参数数组。
//
// - 从 Worker 调用 主线程:
@ -19,9 +19,9 @@
// 1.在 main 端调用 registerHandler(name, obj) 注册处理对象。
// 2.所有非函数属性会生成 `get_xxx()`、`set_xxx(v)`、`write_xxx(v)` 三个函数,
// 其中,`write_` 函数会在设置完毕后回调到 Worker。
// 3.所有函数属性请确保参数是 [args, cmdId, callback]用于回调到 Worker
// 3.所有函数属性请确保参数是 [args, cmdId, callback]callback 用于回调到 Worker
// 用法是 callback(cmdId, args)。
// 注意在主线程回调时传入的是参数数组。
// 注意主线程注册的函数、callback 传入的都是参数数组。
//
// 注册好函数后,在 Worker 通过 main.name.key(...args, (...args)=>{}) 调用。
// 最后一个参数如果是函数的话则会当作 callback 处理。
@ -83,10 +83,10 @@ function registerHandler(name, obj) {
key,
func: (id, cmd, args) => {
obj[key](
...(args ? args : []),
(...args) => {
callbackToMain(id, cmd, args);
},
...(args ? args : []),
);
},
};
@ -232,5 +232,13 @@ const sendScheduler = {
};
const main = {};
const ipcWorker = {
get inited() {
return _inited;
},
main,
init,
registerHandler,
};
module.exports = { init, registerHandler, main };
module.exports = ipcWorker;

View File

@ -1,9 +1,12 @@
// 是否启用 Worker 调度模式,这会减少通信次数(必须一致)
// 是否启用 Worker 调度模式,这会减少通信次数
globalThis.CC_WORKER_SCHEDULER = true;
// 是否启用 Worker 调试模式
globalThis.CC_WORKER_DEBUG = false;
// 是否启用自定义 Worker
globalThis.CC_CUSTOM_WORKER = false;
// --- 以下从主线程同步值 ---
// 是否启用 Worker 使用同步版本的文件系统 API

View File

@ -121,7 +121,11 @@ const ipcMain = {
worker[name] = {};
}
worker[name][key] = (args, callback) => {
this.callToWorker(cmd, args, callback);
if (typeof args === "function") {
this.callToWorker(cmd, null, args);
} else {
this.callToWorker(cmd, args, callback);
}
};
}

View File

@ -28,9 +28,14 @@ if (!("CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL" in globalThis)) {
globalThis.CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL = 500;
}
// 是否启用自定义 Worker
if (!("CC_CUSTOM_WORKER" in globalThis)) {
globalThis.CC_CUSTOM_WORKER = false;
}
// 是否启用 Worker
if (!("CC_USE_WORKER" in globalThis)) {
globalThis.CC_USE_WORKER = (CC_WORKER_ASSET_PIPELINE || CC_WORKER_AUDIO_SYSTEM) && hasWorker && !isSubContext;
globalThis.CC_USE_WORKER = (CC_WORKER_ASSET_PIPELINE || CC_WORKER_AUDIO_SYSTEM || CC_CUSTOM_WORKER) && hasWorker && !isSubContext;
}
// 是否启用 Worker 调试模式
@ -38,7 +43,7 @@ if (!("CC_WORKER_DEBUG" in globalThis)) {
globalThis.CC_WORKER_DEBUG = false;
}
// 是否启用 Worker 调度模式,这也许能减少通信次数带来的性能消耗(必须一致)
// 是否启用 Worker 调度模式,这也许能减少通信次数带来的性能消耗
globalThis.CC_WORKER_SCHEDULER = true;
// 是否启用 Worker 使用同步版本的文件系统 API

51
creator-sp.d.ts vendored
View File

@ -935,3 +935,54 @@ declare module sp {
}
/**
* 访 Worker
*/
declare const worker: any;
declare module ipcMain {
/**
* Worker 访
*/
export function registerHandler(name: string, handler: object): Promise<void>;
}
/**
* Worker 线
*/
declare var CC_WORKER_ASSET_PIPELINE: boolean;
/**
* Worker
*/
declare var CC_WORKER_AUDIO_SYSTEM: boolean;
/**
* Worker
*/
declare var CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL: number;
/**
* Worker
*/
declare var CC_CUSTOM_WORKER: boolean;
/**
* Worker
*/
declare var CC_WORKER_DEBUG: boolean;
/**
* Worker 使 API
*/
declare var CC_WORKER_FS_SYNC: boolean;
/**
* Worker
*/
declare var CC_WORKER_SUB_PACKAGE: boolean;
/**
* Worker
*/
declare var CC_USE_WORKER: boolean;