mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 04:23:31 +01:00
feat(Shard): add eval context (#7011)
Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
This commit is contained in:
@@ -266,11 +266,12 @@ class Shard extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Evaluates a script or function on the shard, in the context of the {@link Client}.
|
* 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 {string|Function} script JavaScript to run on the shard
|
||||||
|
* @param {*} [context] The context for the eval
|
||||||
* @returns {Promise<*>} Result of the script execution
|
* @returns {Promise<*>} Result of the script execution
|
||||||
*/
|
*/
|
||||||
eval(script) {
|
eval(script, context) {
|
||||||
// Stringify the script if it's a Function
|
// 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
|
// 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));
|
if (!this.process && !this.worker) return Promise.reject(new Error('SHARDING_NO_CHILD_EXISTS', this.id));
|
||||||
|
|||||||
1
typings/index.d.ts
vendored
1
typings/index.d.ts
vendored
@@ -1867,6 +1867,7 @@ export class Shard extends EventEmitter {
|
|||||||
public worker: Worker | null;
|
public worker: Worker | null;
|
||||||
public eval(script: string): Promise<unknown>;
|
public eval(script: string): Promise<unknown>;
|
||||||
public eval<T>(fn: (client: Client) => T): Promise<T>;
|
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 fetchClientValue(prop: string): Promise<unknown>;
|
||||||
public kill(): void;
|
public kill(): void;
|
||||||
public respawn(options?: { delay?: number; timeout?: number }): Promise<ChildProcess>;
|
public respawn(options?: { delay?: number; timeout?: number }): Promise<ChildProcess>;
|
||||||
|
|||||||
Reference in New Issue
Block a user