Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _events and cb to the callback function in EventHandler #3507

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tetranoir
Copy link

Add _events and cb to the callback function in EventHandler to make reflection easier.

Add _events and cb to the callback function in EventHandler to make reflection easier.
Copy link

changeset-bot bot commented Nov 14, 2023

⚠️ No Changeset found

Latest commit: 7bcd3d7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@thdxr
Copy link
Contributor

thdxr commented Nov 20, 2023

can you achieve what you need to by wrapping the EventHandler function? this does feel like a bit of a hack to put in

@tetranoir
Copy link
Author

tetranoir commented Dec 14, 2023

Yea, I did just that. This is what I have now

/**
 * @file Drop in replacements for SST's EventHandler and
 * an event bus createEventBuilder instance.
 */
import * as SST from 'sst/node/event-bus';
// Extracted SST types
import { SstEvent, SstEventCallback, SstEventHandler } from './sst-types.js';
// My custom publishEvent function
import { publishEvent } from '@local-lib/local-event-bus.js';


/**
 * Proxies SST EventHandler. Adds a `events` to the return object.
 * @param _events uses _ because that's what SST.EventHandler calls it.
 */
export const EventHandler = <E extends SstEvent>(
  _events: E[],
  cb: SstEventCallback<E>,
): ReturnType<SstEventHandler<E>> & { events: E[], cb: SstEventCallback<E> } => {
  const events = Array.isArray(_events) ? _events : [_events];

  return Object.assign(SST.EventHandler<E>(events, cb), { events, cb });
};

/**
 * Use this to define events that mean to work with SST event bus.
 */
export const createEvent: ReturnType<typeof SST.createEventBuilder> = (name, shape) => {
  const event = SST.createEventBuilder({
    bus: 'Bus', // Can't use the dynamic resource name EventBus.Bus.eventBusName.
  })(name, shape);

  // If local, monkeypatch publish to instead resolve locally.
  // TODO: BAD
  if (process.env.EVENT_BUS === 'local') {
    event.publish = async (props) => {
      publishEvent(event, props);
      // Mocks AWS PutEventsCommandOutput
      return {
        $metadata: { httpStatusCode: 200 },
        FailedEntryCount: 0,
        Entries: [],
      };
    };
  }

  // Leaving as any because the generics in createEventBuilder don't transfer well.
  return event as any;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants