In gaming, every frame of 16ms is extremely valuable. Tasks that can be processed asynchronously should be run in a Worker, which can then return the results to the main thread once completed. Workers operate in a separate global context and thread, and cannot directly call methods from the main thread.
Data transfer between the Worker and the main thread is done using Worker.postMessage to send data, and Worker.onMessage to receive data. The data is copied, not shared directly. Directions
Step 1. Configure Worker information
In game.json
, you can specify the directory where Worker
code is located. All JavaScript code in this directory will be bundled into a single JS file:
Configuration example:
With this configuration, all JS files in the workers directory will be bundled into one JS file and included as part of the mini game's initial package.
The initial package size is limited (currently 4 MB). To prevent Worker code from taking up space in the initial package, starting from base library v2.0.10, Worker code can be packaged as a subpackage (requires the latest version of the developer tools).
Example of configuring Worker code as a subpackage:
{
"workers": {
"path": "workers",
"isSubpackage": true
}
}
Step 2. Add Worker code files
Based on the configuration in step 1, create the following entry files in the code directory: workers/request/index.js
workers/request/utils.js
workers/response/index.js
Once added, the directory structure should look like this:
├── game.js
├── game.json
├── project.config.json
└── workers
├── request
│ ├── index.js
│ └── utils.js
└── response
└── index.js
Step 3. Write Worker Code
In workers/request/index.js
, write the Worker response code.
const utils = require('./utils')
worker.onMessage(function (res) {
console.log(res)
})
Step 4. Initialize Worker in the main thread
In the main thread code game.js, initialize the Worker.
const worker = wx.createWorker('workers/request/index.js')
Starting from base library v2.0.10, if the Worker code is configured as a subpackage, you need to download the Worker code using wx.preDownloadSubpackage before initializing the Worker.
var task = wx.preDownloadSubpackage({
packageType: "workers",
success(res) {
console.log("load worker success", res)
var worker = wx.createWorker("workers/request/index.js")
},
fail(res) {
console.log("load worker fail", res)
}
})
task.onProgressUpdate(res => {
console.log(res.progress)
console.log(res.totalBytesWritten)
console.log(res.totalBytesExpectedToWrite)
})
Step 5. Send messages from main thread to Worker
worker.postMessage({
msg: 'hello worker'
})
Note
The maximum concurrent number of Workers is limited to 1. Use Worker.terminate() to end the current Worker before creating another. Code inside the Worker can only require files within the specified Worker path and cannot reference files from other paths.
The Worker entry file is specified when calling createWorker, allowing developers to dynamically specify the Worker entry file. The Worker does not support the wx
series of APIs.
Sending messages between Workers is not supported.
Only JS files are allowed inside the Worker directory; other types of static files must be placed outside the Worker directory.