Skip to content

Commit

Permalink
Refactor Bolt event handlers to not be HOC's
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Aug 8, 2023
1 parent 021c2a1 commit aa222fb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
7 changes: 4 additions & 3 deletions app-nest/src/bolt/bolt-register.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ export class BoltRegisterService {
const cref = this.moduleRef.get(discoveredMethod.parentClass.injectType, {
strict: false,
});
// N.B.: Take care where you call the discovered handler method. This is an easy
// place to bind it into a wrong context. Ask me how I know.
return cref[discoveredMethod.methodName]();

return cref[discoveredMethod.methodName].bind(
discoveredMethod.parentClass.instance,
);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions app-nest/src/dev-tools/dev-tools.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ export class DevToolsController {
constructor(private userSyncService: UserSyncService) {}

@BoltAction("sync_users")
syncUsers() {
return async ({ ack }) => {
await ack();
await this.userSyncService.syncUsers();
};
async syncUsers({ ack }) {
await ack();
await this.userSyncService.syncUsers();
}
}
68 changes: 33 additions & 35 deletions app-nest/src/gui/tabs/home-tab.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,42 @@ export class HomeTabController {
constructor(private userService: UserService) {}

@BoltEvent("app_home_opened")
getView() {
return async ({ event, client, logger }) => {
const users = await this.userService.findAll();
const { slackId } = users[0];
async getView({ event, client, logger }) {
const users = await this.userService.findAll();
const { slackId } = users[0];

try {
const result = await client.views.publish({
user_id: event.user,
view: {
type: "home",
blocks: [
...devTools,
{
type: "section",
text: {
type: "mrkdwn",
text:
"*Welcome controller, <@" +
event.user +
"> :house:* " +
slackId,
},
try {
const result = await client.views.publish({
user_id: event.user,
view: {
type: "home",
blocks: [
...devTools,
{
type: "section",
text: {
type: "mrkdwn",
text:
"*Welcome controller, <@" +
event.user +
"> :house:* " +
slackId,
},
{
type: "section",
text: {
type: "mrkdwn",
text: "Learn how home tabs can be more useful and interactive <https://api.slack.com/surfaces/tabs/using|*in the documentation*>.",
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: "Learn how home tabs can be more useful and interactive <https://api.slack.com/surfaces/tabs/using|*in the documentation*>.",
},
],
},
});
},
],
},
});

logger.debug(result);
} catch (error) {
logger.error(error);
}
};
logger.debug(result);
} catch (error) {
logger.error(error);
}
}
}
4 changes: 2 additions & 2 deletions app-nest/src/sync/sync.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class SyncController {
constructor(private userSyncService: UserSyncService) {}

@BoltEvent("user_profile_changed")
userProfileChanged() {
return ({ event }) => this.userSyncService.syncUsers(event.user);
async userProfileChanged({ event }) {
await this.userSyncService.syncUsers(event.user);
}
}

0 comments on commit aa222fb

Please sign in to comment.