MishkaPay.PubSub (Mishka Pay v0.0.1)

Copy Markdown View Source

Broadcasts Mishka Pay's changes on the host's own Phoenix.PubSub, so a LiveView, a channel or any process hears a payment, subscription, customer or dispute change at once. One line:

defmodule MyApp.Pay.Broadcast do
  use MishkaPay.PubSub, pubsub: MyApp.PubSub
end

# application.ex children, after the PubSub
MyApp.Pay.Broadcast

It is a set of mishka_installer plugins, one on each mishka_pay:*:changed event (MishkaPay.Events); supervising the module starts them all. Every change goes to two topics: the kind's ("mishka_pay:payments") and the kind's for its tenant ("mishka_pay:site-42:payments"), so a multi-site project subscribes each site's screens to its own changes only. topic/3 builds them:

Phoenix.PubSub.subscribe(MyApp.PubSub, MishkaPay.PubSub.topic(:payment, site.id))

def handle_info({:mishka_pay, :payment, %{payment: payment, record: row}}, socket), do: ...

The message is {:mishka_pay, kind, changed}: kind is :payment, :subscription, :customer, :dispute, :invoice, :product, :price, :coupon, :promotion_code or :payment_link, changed is what the event carried (the provider's struct under the kind's name, and the stored :record).

Options:

  • :pubsub (required): the host's PubSub name.
  • :kinds: which changes to broadcast, every kind by default.
  • :prefix: the topics' first part, "mishka_pay" by default.
  • :priority: the plugins' priority on their events, 90 by default (after the host's own).

Summary

Functions

The kinds of change it can broadcast.

The topic of kind changes: for every tenant, or for tenant only. prefix is the plugin's :prefix option.

Functions

kinds()

@spec kinds() :: [atom()]

The kinds of change it can broadcast.

topic(kind, tenant \\ nil, prefix \\ "mishka_pay")

@spec topic(atom(), term(), String.t()) :: String.t()

The topic of kind changes: for every tenant, or for tenant only. prefix is the plugin's :prefix option.

iex> MishkaPay.PubSub.topic(:payment)
"mishka_pay:payments"
iex> MishkaPay.PubSub.topic(:dispute, "site-42")
"mishka_pay:site-42:disputes"