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 initialize/1 handler callback #526

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

Conversation

dvic
Copy link
Contributor

@dvic dvic commented Feb 15, 2023

This PR replaces the init/0 callback with initialize/1 (using the old callback still works). In this way, you can for example use ecto dynamic repos with Commanded.Projections.Ecto, e.g.

defmodule Myhandler do
  use Commanded.Projections.Ecto,
    name: "MyHandler",
    repo: MyRepo

  @impl true
  def initialize(state) do
    Ecto.Repo.put_dynamic_repo(state.tenant)
  end
end

@yordis
Copy link
Contributor

yordis commented Feb 15, 2023

Is there any particular reason why it can not be init?

@dvic
Copy link
Contributor Author

dvic commented Feb 15, 2023

Because there is already an init/1 😅😅

@dvic
Copy link
Contributor Author

dvic commented Feb 15, 2023

It's rather confusing, init/1 is called to get the config (outside the handler process) and init/0 is called inside the handlers process

@yordis
Copy link
Contributor

yordis commented Feb 15, 2023

So you can not use the existing init function, then?

Also, confusing, I agree!

@dvic
Copy link
Contributor Author

dvic commented Feb 15, 2023

Yes but then that would be a breaking change, because the meaning (and where the function is called) would have to change. So I thought it would be better to have a new callback, where we get rid of init/0 in the future.

@jvantuyl
Copy link

jvantuyl commented Jun 2, 2023

Maybe use a completely different name, like setup/1? At least that doesn't complicate init/* more. Probably should document it, too.

@slashdotdash
Copy link
Member

Having three similarly named functions is too confusing (init/0, init/1 and initialize/1).

The init/1 function is called before the event handler process is started to optionally configure the handler with runtime config. This is similar to Ecto Repo's init/2 callback function.

The init/0 function is called within the event handler process after it has started.

We could deprecate the init/0 function and then define a new callback function, such as after_start, which could optionally receive the event handler state?

@callback after_start() :: :ok | {:stop, reason :: any()}
@callback after_start(state :: any()) :: :ok | {:stop, reason :: any()}

For backwards compatibility we could call the init/0 function if it exists.

@dvic
Copy link
Contributor Author

dvic commented Jan 10, 2024

Having three similarly named functions is too confusing (init/0, init/1 and initialize/1).

The init/1 function is called before the event handler process is started to optionally configure the handler with runtime config. This is similar to Ecto Repo's init/2 callback function.

The init/0 function is called within the event handler process after it has started.

We could deprecate the init/0 function and then define a new callback function, such as after_start, which could optionally receive the event handler state?

@callback after_start() :: :ok | {:stop, reason :: any()}
@callback after_start(state :: any()) :: :ok | {:stop, reason :: any()}

For backwards compatibility we could call the init/0 function if it exists.

That sounds like a good idea! But do we need after_start/0? Because we're introducing a new function anyways, we could just have after_start/1, with a default implementation that does nothing or calls init/0 if it exists. So we would always call after_start/1.

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

Successfully merging this pull request may close these issues.

None yet

4 participants