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

Isn't it superfluous to export functions in adapters instead of constants? #15

Closed
budarin opened this issue Oct 1, 2023 · 3 comments
Closed
Labels
answered The question is answered question Further information is requested

Comments

@budarin
Copy link

budarin commented Oct 1, 2023

import { NotificationService } from "../application/ports";

export function useNotifier(): NotificationService {
  return {
    notify: (message: string) => window.alert(message),
  };
}

vs

import { NotificationService } from "../application/ports";

export const notifier: NotificationService = { 
  notify: (message: string) => window.alert(message) 
}

Calling functions that return a new service object each time leads to an overhead during code execution and also generates a bunch of garbage for GC. This is practically nothing - for the client, but if such code is used in SSR, this is already a problem

@budarin budarin changed the title Isn't it redundant to export functions in adapters, but constants? Isn't it superfluous to export functions in adapters instead of constants? Oct 1, 2023
@bespoyasov bespoyasov added the question Further information is requested label Oct 1, 2023
@bespoyasov
Copy link
Owner

It is an implementation detail; the concept from the post forces no constraints on how to do it.

The example in the text uses Hooks as “poor man's DI” and, for consistency, uses it when exposing adapters, too.

@bespoyasov bespoyasov added the answered The question is answered label Oct 1, 2023
@budarin
Copy link
Author

budarin commented Oct 1, 2023

in this case, doesn't the module itself act like a handmade DI? 😊

@bespoyasov
Copy link
Owner

If, in testing, there are no preferences or constraints on mocking a module versus injecting a stub as a dependency, it might.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered The question is answered question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants