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

Rework API #1

Merged
merged 5 commits into from
Jan 12, 2024
Merged

Rework API #1

merged 5 commits into from
Jan 12, 2024

Conversation

whatyouhide
Copy link
Collaborator

WIP API discussed with @josevalim.

lib/nimble_ownership.ex Outdated Show resolved Hide resolved
@whatyouhide
Copy link
Collaborator Author

@josevalim you were right, shaved off a lot of code by using your proposed API. One last question from me: do we want to be able to remove ownerships? We could do that by having the callback in get_and_update/4 return nil, which would act as "delete this ownership" if the ownership exists (and no-op if it doesn't exist).

lib/nimble_ownership.ex Outdated Show resolved Hide resolved
lib/nimble_ownership.ex Outdated Show resolved Hide resolved
@whatyouhide
Copy link
Collaborator Author

@josevalim this is ready for review. This API is fantastic IMO, and definitely works on Mox 🙃

I have the Mox PR in dashbitco/mox#148.

Right now, we're basically keeping Mox.Server just for the sake of keeping track of whether we're in global mode or private mode. I’m not sure it's worth to extract that part into this lib just yet because I’m not sure we'll need it in other places.

lib/nimble_ownership.ex Outdated Show resolved Hide resolved
@whatyouhide whatyouhide merged commit 1d7fcad into main Jan 12, 2024
2 checks passed
@whatyouhide whatyouhide deleted the al/rework-api branch January 12, 2024 08:30
Gets the owner of `key` through one of the `callers`.

If one of the `callers` owns `key` or is allowed access to `key`,
then this function returns `{:ok, {owner_pid, metadata}}` where `metadata` is the
Copy link
Member

@wojtekmach wojtekmach Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{:ok, {owner_pid, metadata}}

I don't think it's returned anymore and the typespecs say so. Which I think is a shame because we could always find metadata from any caller with just one call instead of now two calls:

defmodule Req.Test do
  @ownership Req.Ownership

  def stub(name) do
    case NimbleOwnership.fetch_owner(@ownership, callers(), name) do
      {:ok, owner} when is_pid(owner) ->
        %{^name => value} = NimbleOwnership.get_owned(@ownership, owner)
        value

      :error ->
        raise "cannot find stub #{inspect(name)} in process #{inspect(self())}"
    end
  end

  def stub(name, value) do
    NimbleOwnership.get_and_update(@ownership, self(), name, fn _ -> {:ok, value} end)
  end

  defp callers do
    [self() | Process.get(:"$callers") || []]
  end
end

I think 2 calls are still OK but thought I'd mention this anyway.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wojtekmach I lost 90% of context on this already ahah. We could add fetch_owner_with_metadata/3, but do you have time to take a look at whether that's viable and if it would help in Mox too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup I’ll look into it soon!

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.

3 participants