benrosen / pixel-chess

Multiplayer pixel art chess powered by Phaser, React, and AWS.
0 stars 0 forks source link

Refactor distinct getPlayers functions into a single function #19

Closed benrosen closed 3 years ago

benrosen commented 3 years ago

The Problem

Both pixelchessResolveCreateConnectionMutation and pixelchessResolveSetPlayerMutation contain identical functions called getPlayers. These two functions should be replaced with a single function which both pixelchessResolveCreateConnectionMutation and pixelchessResolveSetPlayerMutation can access.

There are at least two ways to accomplish this.

Option 1: Lambda Function

  1. Copy the getPlayers function into a lambda function.
  2. Configure the lambda function permissions.
  3. Invoke the lambda function from both pixelchessResolveCreateConnectionMutation and pixelchessResolveSetPlayerMutation.
  4. Remove the original getPlayers functions from both pixelchessResolveCreateConnectionMutation and pixelchessResolveSetPlayerMutation.

Pros:

  1. Simpler and more fool-proof (less work up front)

Cons:

  1. More boilerplate code in lambda functions
  2. More lambda function invocations (slower and more expensive)

Option 2: Utility Layer Package

  1. Refactor the getPlayers function into the aws-lambda-utility-layer package.
  2. Publish the newly updated package.
  3. Update the lambda layer with the newly updated package.
  4. Push changes to the lambda layer.
  5. Reconfigure lambda layer version for both pixelchessResolveCreateConnectionMutation and pixelchessResolveSetPlayerMutation.
  6. Require and invoke the utility function from both pixelchessResolveCreateConnectionMutation and pixelchessResolveSetPlayerMutation.
  7. Remove the original getPlayers functions from both pixelchessResolveCreateConnectionMutation and pixelchessResolveSetPlayerMutation.

Pros:

  1. Less boilerplate code in lambda functions (less work & cleaner to use)
  2. Fewer lambda invocations (faster and cheaper)

Cons:

  1. Updating lambda layers is not currently a good developer experience, and is fraught with issues. (more work up front)
  2. getPlayers is not generic enough to warrant its addition to the aws-lambda-utility-layer package.

Option 3: Utility Layer /opt/ directory

  1. Refactor the getPlayers function into the aws-lambda-utility-layer /opt/ folder.
  2. Push changes to the lambda layer.
  3. Reconfigure lambda layer version for both pixelchessResolveCreateConnectionMutation and pixelchessResolveSetPlayerMutation.
  4. Require and invoke the utility function from both pixelchessResolveCreateConnectionMutation and pixelchessResolveSetPlayerMutation.
  5. Remove the original getPlayers functions from both pixelchessResolveCreateConnectionMutation and pixelchessResolveSetPlayerMutation.

Pros:

  1. Less boilerplate code in lambda functions (less work & cleaner to use)
  2. Fewer lambda invocations (faster and cheaper)
  3. No updates to npm package are required.

Cons:

  1. Updating lambda layers is not currently a good developer experience, and is fraught with issues. (more work up front)