|
|
@@ -166,7 +166,9 @@ const onMessage = (thread, message) => { |
|
|
|
messageHandlers[message.method](thread, message); |
|
|
|
}; |
|
|
|
|
|
|
|
const spawnThread = async ({ map: { name, path, instanced, destroyWhenEmptyForMs = -1 }, obj }) => { |
|
|
|
const spawnThread = async ({ map: { name, path, instanced, destroyWhenEmptyForMs }, obj }) => { |
|
|
|
destroyWhenEmptyForMs = destroyWhenEmptyForMs ?? consts.destroyThreadWhenEmptyForMs; |
|
|
|
|
|
|
|
let cbOnInitialized; |
|
|
|
|
|
|
|
const promise = new Promise(resolveOnReady => { |
|
|
@@ -188,7 +190,8 @@ const spawnThread = async ({ map: { name, path, instanced, destroyWhenEmptyForMs |
|
|
|
birthEpoch: +new Date(), |
|
|
|
destroyWhenEmptyForMs, |
|
|
|
emptySinceEpoch: null, |
|
|
|
sendArgsToWorker: ['name', 'id'] |
|
|
|
sendArgsToWorker: ['name', 'id'], |
|
|
|
workerArgs: null |
|
|
|
}; |
|
|
|
|
|
|
|
const emBeforeSpawnThread = { |
|
|
@@ -197,13 +200,12 @@ const spawnThread = async ({ map: { name, path, instanced, destroyWhenEmptyForMs |
|
|
|
}; |
|
|
|
events.emit('beforeSpawnThread', emBeforeSpawnThread); |
|
|
|
|
|
|
|
const workerArgs = JSON.stringify( |
|
|
|
Object.fromEntries( |
|
|
|
thread.sendArgsToWorker.map(a => [a, thread[a]]) |
|
|
|
) |
|
|
|
thread.workerArgs = Object.fromEntries( |
|
|
|
thread.sendArgsToWorker.map(a => [a, thread[a]]) |
|
|
|
); |
|
|
|
|
|
|
|
thread.worker = childProcess.fork('./world/worker', [workerArgs]); |
|
|
|
_.log(`Spawning: ${JSON.stringify(thread.workerArgs, null, '\t')}`); |
|
|
|
thread.worker = childProcess.fork('./world/worker', [JSON.stringify(thread.workerArgs)]); |
|
|
|
|
|
|
|
thread.worker.on('message', onMessage.bind(null, thread)); |
|
|
|
|
|
|
@@ -248,6 +250,8 @@ const getThread = async ({ zoneName, zoneId, obj }) => { |
|
|
|
}; |
|
|
|
|
|
|
|
const killThread = thread => { |
|
|
|
_.log(`Killing: ${thread.workerArgs.id}`); |
|
|
|
|
|
|
|
thread.worker.kill(); |
|
|
|
threads.spliceWhere(t => t === thread); |
|
|
|
}; |
|
|
|