mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2024-12-26 03:38:29 +00:00
[adapters] 增加主线程的类型提示文件,优化多线程通信方式,增加对多线程自定义扩展的支持
This commit is contained in:
parent
6333ac01d2
commit
b97579d9c0
@ -111,19 +111,19 @@ var cacheManager = {
|
|||||||
|
|
||||||
init() {
|
init() {
|
||||||
this._cacheDir = getUserDataPath() + '/' + this.cacheDir;
|
this._cacheDir = getUserDataPath() + '/' + this.cacheDir;
|
||||||
worker.cacheManager.init(null, ([cachedFiles]) => {
|
worker.cacheManager.init(([cachedFiles]) => {
|
||||||
this.cachedFiles = new cc.AssetManager.Cache(cachedFiles);
|
this.cachedFiles = new cc.AssetManager.Cache(cachedFiles);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
clearCache() {
|
clearCache() {
|
||||||
worker.cacheManager.clearCache(null, () => {
|
worker.cacheManager.clearCache(() => {
|
||||||
this.cachedFiles.clear();
|
this.cachedFiles.clear();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
clearLRU() {
|
clearLRU() {
|
||||||
worker.cacheManager.clearLRU(null, ([deletedFiles]) => {
|
worker.cacheManager.clearLRU(([deletedFiles]) => {
|
||||||
for (let i = 0, l = deletedFiles.length; i < l; i++) {
|
for (let i = 0, l = deletedFiles.length; i < l; i++) {
|
||||||
this.cachedFiles.remove(deletedFiles[i]);
|
this.cachedFiles.remove(deletedFiles[i]);
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ var audio_worker = {
|
|||||||
map: {},
|
map: {},
|
||||||
timer: null,
|
timer: null,
|
||||||
|
|
||||||
create(callback, id) {
|
create(id) {
|
||||||
this.map[id] = {
|
this.map[id] = {
|
||||||
audio: worker.createInnerAudioContext({ useWebAudioImplement: true }),
|
audio: worker.createInnerAudioContext({ useWebAudioImplement: true }),
|
||||||
cache: {
|
cache: {
|
||||||
@ -20,7 +20,7 @@ var audio_worker = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
call(callback, id, type, arg) {
|
call(id, type, arg) {
|
||||||
const audio = this.map[id].audio;
|
const audio = this.map[id].audio;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 0:
|
case 0:
|
||||||
@ -73,20 +73,20 @@ var audio_worker = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
on(callback, id, type) {
|
on(id, type) {
|
||||||
const data = this.map[id];
|
const data = this.map[id];
|
||||||
data.audio["on" + type]((data.callbacks[type] = data => {
|
data.audio["on" + type]((data.callbacks[type] = data => {
|
||||||
main.audioAdapter.onCallback(id, type, data);
|
main.audioAdapter.onCallback(id, type, data);
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|
||||||
off(callback, id, type) {
|
off(id, type) {
|
||||||
const data = this.map[id];
|
const data = this.map[id];
|
||||||
data.audio["off" + type](data.callbacks[type]);
|
data.audio["off" + type](data.callbacks[type]);
|
||||||
delete data.callbacks[type];
|
delete data.callbacks[type];
|
||||||
},
|
},
|
||||||
|
|
||||||
destroy(callback, id) {
|
destroy(id) {
|
||||||
this.map[id].destroy();
|
this.map[id].destroy();
|
||||||
delete this.map[id];
|
delete this.map[id];
|
||||||
},
|
},
|
||||||
|
@ -88,12 +88,12 @@ var cacheManager_worker = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
download(
|
download(
|
||||||
callback,
|
|
||||||
url,
|
url,
|
||||||
options_reload,
|
options_reload,
|
||||||
options_header,
|
options_header,
|
||||||
options_cacheEnabled,
|
options_cacheEnabled,
|
||||||
options___cacheBundleRoot__,
|
options___cacheBundleRoot__,
|
||||||
|
callback,
|
||||||
) {
|
) {
|
||||||
var result = this.transformUrl(url, options_reload);
|
var result = this.transformUrl(url, options_reload);
|
||||||
if (result.inLocal) {
|
if (result.inLocal) {
|
||||||
@ -109,17 +109,17 @@ var cacheManager_worker = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.tempFiles[url] = path;
|
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);
|
callback(null, path);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
handleZip(
|
handleZip(
|
||||||
callback,
|
|
||||||
url,
|
url,
|
||||||
options_header,
|
options_header,
|
||||||
options___cacheBundleRoot__,
|
options___cacheBundleRoot__,
|
||||||
|
callback,
|
||||||
) {
|
) {
|
||||||
let cachedUnzip = this.cachedFiles[url];
|
let cachedUnzip = this.cachedFiles[url];
|
||||||
if (cachedUnzip) {
|
if (cachedUnzip) {
|
||||||
@ -138,7 +138,7 @@ var cacheManager_worker = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getTemp(callback, url) {
|
getTemp(url, callback) {
|
||||||
callback(this.tempFiles.has(url) ? this.tempFiles.get(url) : '');
|
callback(this.tempFiles.has(url) ? this.tempFiles.get(url) : '');
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -208,7 +208,7 @@ var cacheManager_worker = {
|
|||||||
checkNextPeriod = false;
|
checkNextPeriod = false;
|
||||||
},
|
},
|
||||||
|
|
||||||
cacheFile(callback, id, srcUrl, cacheEnabled, cacheBundleRoot, isCopy) {
|
cacheFile(id, srcUrl, cacheEnabled, cacheBundleRoot, isCopy, callback) {
|
||||||
cacheEnabled = cacheEnabled != null ? cacheEnabled : this.cacheEnabled;
|
cacheEnabled = cacheEnabled != null ? cacheEnabled : this.cacheEnabled;
|
||||||
if (!cacheEnabled || this.cacheQueue[id] || this.cachedFiles[id]) {
|
if (!cacheEnabled || this.cacheQueue[id] || this.cachedFiles[id]) {
|
||||||
if (callback) callback(null);
|
if (callback) callback(null);
|
||||||
@ -293,7 +293,7 @@ var cacheManager_worker = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
removeCache(callback, url) {
|
removeCache(url, callback) {
|
||||||
if (this.cachedFiles[url]) {
|
if (this.cachedFiles[url]) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var path = this.cachedFiles[url].url;
|
var path = this.cachedFiles[url].url;
|
||||||
|
@ -1,6 +1,14 @@
|
|||||||
require("./macro.js");
|
require("./macro.js");
|
||||||
const { init } = require("./ipc-worker.js");
|
const { init } = require("./ipc-worker.js");
|
||||||
|
const { CC_CUSTOM_WORKER } = globalThis;
|
||||||
|
|
||||||
init(() => {
|
init(() => {
|
||||||
require("./handlers.js");
|
require("./handlers.js");
|
||||||
|
if (CC_CUSTOM_WORKER) {
|
||||||
|
try {
|
||||||
|
require("./custom/index.js");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("worker init custom extension error:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
@ -7,10 +7,10 @@
|
|||||||
// 1.在 worker 端调用 registerHandler(name, obj) 注册处理对象。
|
// 1.在 worker 端调用 registerHandler(name, obj) 注册处理对象。
|
||||||
// 2.所有非函数属性会生成 `get_xxx()`、`set_xxx(v)`、`write_xxx(v)` 三个函数,
|
// 2.所有非函数属性会生成 `get_xxx()`、`set_xxx(v)`、`write_xxx(v)` 三个函数,
|
||||||
// 其中,`write_` 函数会在设置完毕后回调到主线程。
|
// 其中,`write_` 函数会在设置完毕后回调到主线程。
|
||||||
// 3.所有函数属性请确保第一个参数是 callback,用于回调到主线程,
|
// 3.在函数被调用时,会在最后一个参数传入 callback,用于回调结果到主线程,
|
||||||
// 用法是 callback(...args)。
|
// 用法是 callback(...args)。
|
||||||
//
|
//
|
||||||
// 注册好函数后,在主线程通过 worker.name.key(args | null, (args)=>{}) 调用。
|
// 注册好函数后,在主线程通过 worker.name.key(args, args => {}) 或 worker.name.key(args => {}) 调用。
|
||||||
// 注意在主线程调用时传入和返回的都是参数数组。
|
// 注意在主线程调用时传入和返回的都是参数数组。
|
||||||
//
|
//
|
||||||
// - 从 Worker 调用 主线程:
|
// - 从 Worker 调用 主线程:
|
||||||
@ -19,9 +19,9 @@
|
|||||||
// 1.在 main 端调用 registerHandler(name, obj) 注册处理对象。
|
// 1.在 main 端调用 registerHandler(name, obj) 注册处理对象。
|
||||||
// 2.所有非函数属性会生成 `get_xxx()`、`set_xxx(v)`、`write_xxx(v)` 三个函数,
|
// 2.所有非函数属性会生成 `get_xxx()`、`set_xxx(v)`、`write_xxx(v)` 三个函数,
|
||||||
// 其中,`write_` 函数会在设置完毕后回调到 Worker。
|
// 其中,`write_` 函数会在设置完毕后回调到 Worker。
|
||||||
// 3.所有函数属性请确保参数是 [args, cmdId, callback],用于回调到 Worker,
|
// 3.所有函数属性请确保参数是 [args, cmdId, callback],callback 用于回调到 Worker,
|
||||||
// 用法是 callback(cmdId, args)。
|
// 用法是 callback(cmdId, args)。
|
||||||
// 注意在主线程回调时传入的是参数数组。
|
// 注意主线程注册的函数、callback 传入的都是参数数组。
|
||||||
//
|
//
|
||||||
// 注册好函数后,在 Worker 通过 main.name.key(...args, (...args)=>{}) 调用。
|
// 注册好函数后,在 Worker 通过 main.name.key(...args, (...args)=>{}) 调用。
|
||||||
// 最后一个参数如果是函数的话则会当作 callback 处理。
|
// 最后一个参数如果是函数的话则会当作 callback 处理。
|
||||||
@ -83,10 +83,10 @@ function registerHandler(name, obj) {
|
|||||||
key,
|
key,
|
||||||
func: (id, cmd, args) => {
|
func: (id, cmd, args) => {
|
||||||
obj[key](
|
obj[key](
|
||||||
|
...(args ? args : []),
|
||||||
(...args) => {
|
(...args) => {
|
||||||
callbackToMain(id, cmd, args);
|
callbackToMain(id, cmd, args);
|
||||||
},
|
},
|
||||||
...(args ? args : []),
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -232,5 +232,13 @@ const sendScheduler = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const main = {};
|
const main = {};
|
||||||
|
const ipcWorker = {
|
||||||
|
get inited() {
|
||||||
|
return _inited;
|
||||||
|
},
|
||||||
|
main,
|
||||||
|
init,
|
||||||
|
registerHandler,
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = { init, registerHandler, main };
|
module.exports = ipcWorker;
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
// 是否启用 Worker 调度模式,这会减少通信次数(必须一致)
|
// 是否启用 Worker 调度模式,这会减少通信次数
|
||||||
globalThis.CC_WORKER_SCHEDULER = true;
|
globalThis.CC_WORKER_SCHEDULER = true;
|
||||||
|
|
||||||
// 是否启用 Worker 调试模式
|
// 是否启用 Worker 调试模式
|
||||||
globalThis.CC_WORKER_DEBUG = false;
|
globalThis.CC_WORKER_DEBUG = false;
|
||||||
|
|
||||||
|
// 是否启用自定义 Worker
|
||||||
|
globalThis.CC_CUSTOM_WORKER = false;
|
||||||
|
|
||||||
// --- 以下从主线程同步值 ---
|
// --- 以下从主线程同步值 ---
|
||||||
|
|
||||||
// 是否启用 Worker 使用同步版本的文件系统 API
|
// 是否启用 Worker 使用同步版本的文件系统 API
|
||||||
|
@ -121,7 +121,11 @@ const ipcMain = {
|
|||||||
worker[name] = {};
|
worker[name] = {};
|
||||||
}
|
}
|
||||||
worker[name][key] = (args, callback) => {
|
worker[name][key] = (args, callback) => {
|
||||||
|
if (typeof args === "function") {
|
||||||
|
this.callToWorker(cmd, null, args);
|
||||||
|
} else {
|
||||||
this.callToWorker(cmd, args, callback);
|
this.callToWorker(cmd, args, callback);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,9 +28,14 @@ if (!("CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL" in globalThis)) {
|
|||||||
globalThis.CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL = 500;
|
globalThis.CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL = 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 是否启用自定义 Worker
|
||||||
|
if (!("CC_CUSTOM_WORKER" in globalThis)) {
|
||||||
|
globalThis.CC_CUSTOM_WORKER = false;
|
||||||
|
}
|
||||||
|
|
||||||
// 是否启用 Worker
|
// 是否启用 Worker
|
||||||
if (!("CC_USE_WORKER" in globalThis)) {
|
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 调试模式
|
// 是否启用 Worker 调试模式
|
||||||
@ -38,7 +43,7 @@ if (!("CC_WORKER_DEBUG" in globalThis)) {
|
|||||||
globalThis.CC_WORKER_DEBUG = false;
|
globalThis.CC_WORKER_DEBUG = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 是否启用 Worker 调度模式,这也许能减少通信次数带来的性能消耗(必须一致)
|
// 是否启用 Worker 调度模式,这也许能减少通信次数带来的性能消耗
|
||||||
globalThis.CC_WORKER_SCHEDULER = true;
|
globalThis.CC_WORKER_SCHEDULER = true;
|
||||||
|
|
||||||
// 是否启用 Worker 使用同步版本的文件系统 API
|
// 是否启用 Worker 使用同步版本的文件系统 API
|
||||||
|
51
creator-sp.d.ts
vendored
51
creator-sp.d.ts
vendored
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user