feat(Shard): add eval context (#7011)

Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
This commit is contained in:
dai
2021-11-23 10:26:46 +01:00
committed by GitHub
parent c1f2fe29ef
commit 77aff08345
2 changed files with 4 additions and 2 deletions

View File

@@ -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));

1
typings/index.d.ts vendored
View File

@@ -1867,6 +1867,7 @@ export class Shard extends EventEmitter {
public worker: Worker | null;
public eval(script: string): Promise<unknown>;
public eval<T>(fn: (client: Client) => T): Promise<T>;
public eval<T, P>(fn: (client: Client, context: Serialized<P>) => T, context: P): Promise<T>;
public fetchClientValue(prop: string): Promise<unknown>;
public kill(): void;
public respawn(options?: { delay?: number; timeout?: number }): Promise<ChildProcess>;