From 930c2ca229d0f4e960058e2e7245adde0ac9b159 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sun, 3 Apr 2022 02:22:51 -0500 Subject: [PATCH] refactor(context.ts) Oops forgot .map existed.... Much easier code to read now --- src/handler/structures/context.ts | 48 +++++++++++++++++++------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/handler/structures/context.ts b/src/handler/structures/context.ts index de0ca9a..6bca59b 100644 --- a/src/handler/structures/context.ts +++ b/src/handler/structures/context.ts @@ -54,53 +54,53 @@ export default class Context { */ public mapInteraction( - cb : ( ctx: I ) => B + cb : ( ctx: I ) => Context ) : Context { if (this.oInterac.none) return Context.empty(); - return Context.wrap(cb(this.oInterac.val)); + return cb(this.oInterac.val); } public get id() : Snowflake { return firstSome( - this.oInterac.andThen( i => Some(i.id)), - this.oMsg.andThen(m => Some(m.id)) + this.oInterac.map( i => i.id), + this.oMsg.map(m => m.id) )!; } public get channel() : Nullish { return firstSome( - this.oMsg.andThen(m => Some(m.channel)), - this.oInterac.andThen(i => Some(i.channel)) + this.oMsg.map(m => m.channel), + this.oInterac.map(i => i.channel) ); } public get user(): Nullish { return firstSome( - this.oMsg.andThen(m => Some(m.author)), - this.oInterac.andThen(i => Some(i.user)) + this.oMsg.map(m => m.author), + this.oInterac.map(i => i.user) ); } public get createdTimestamp() : number { return firstSome( - this.oMsg.andThen(m => Some(m.createdTimestamp)), - this.oInterac.andThen(i => Some(i.createdTimestamp)) + this.oMsg.map(m => m.createdTimestamp), + this.oInterac.map(i => i.createdTimestamp) )!; } public get guild() : Nullish { return firstSome( - this.oMsg.andThen(m => Some(m.guild)), - this.oInterac.andThen(i => Some(i.guild)) + this.oMsg.map(m => m.guild), + this.oInterac.map(i => i.guild) ); } public get guildId() : Nullish { return firstSome( - this.oMsg.andThen(m => Some(m.guildId)), - this.oInterac.andThen(i => Some(i.guildId)) + this.oMsg.map(m => m.guildId), + this.oInterac.map(i => i.guildId) ); } public get member() : Nullish { return firstSome( - this.oMsg.andThen(m => Some(m.member)), - this.oInterac.andThen(i => Some(i.member)) + this.oMsg.map(m => m.member), + this.oInterac.map(i => i.member) ); } /* @@ -110,8 +110,8 @@ export default class Context { onInteraction : ( interaction : I ) => Awaitable, onMsg? : (message : Message) => Awaitable ): Context { - this.oInterac.andThen(i => Some(onInteraction(i))); - this.oMsg.andThen(m => Some(onMsg?.(m))); + this.oInterac.map(onInteraction); + this.oMsg.map(m => onMsg?.(m)); return this; } public extractInteraction( @@ -121,11 +121,21 @@ export default class Context { return extract(this.oInterac.val); } public extractMessage( - extract : (interaction : Message) => T + extract : (message: Message) => T ): Nullish { if(this.oMsg.none) return null; return extract(this.oMsg.val); } + // extract either + public extractEither( + i : (interaction : I) => T, + m : (message : Message ) => V + ) { + const iExtract = this.extractMessage(m); + const mExtract = this.extractInteraction(i); + return iExtract ?? mExtract; + } + }