From 77aff08345cd2b76ca350ba8086717623f028534 Mon Sep 17 00:00:00 2001 From: dai <72147841+daimond113@users.noreply.github.com> Date: Tue, 23 Nov 2021 10:26:46 +0100 Subject: [PATCH] feat(Shard): add eval context (#7011) Co-authored-by: SpaceEEC --- src/sharding/Shard.js | 5 +++-- typings/index.d.ts | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/sharding/Shard.js b/src/sharding/Shard.js index 297ef9ec69a7..5be37ff04e8e 100644 --- a/src/sharding/Shard.js +++ b/src/sharding/Shard.js @@ -266,11 +266,12 @@ class Shard extends EventEmitter { /** * Evaluates a script or function on the shard, in the context of the {@link Client}. * @param {string|Function} script JavaScript to run on the shard + * @param {*} [context] The context for the eval * @returns {Promise<*>} Result of the script execution */ - eval(script) { + eval(script, context) { // Stringify the script if it's a Function - const _eval = typeof script === 'function' ? `(${script})(this)` : script; + const _eval = typeof script === 'function' ? `(${script})(this, ${JSON.stringify(context)})` : script; // Shard is dead (maybe respawning), don't cache anything and error immediately if (!this.process && !this.worker) return Promise.reject(new Error('SHARDING_NO_CHILD_EXISTS', this.id)); diff --git a/typings/index.d.ts b/typings/index.d.ts index 42a545a549cd..d163c4554bda 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1867,6 +1867,7 @@ export class Shard extends EventEmitter { public worker: Worker | null; public eval(script: string): Promise; public eval(fn: (client: Client) => T): Promise; + public eval(fn: (client: Client, context: Serialized

) => T, context: P): Promise; public fetchClientValue(prop: string): Promise; public kill(): void; public respawn(options?: { delay?: number; timeout?: number }): Promise;