Current section

Files

Jump to
i3_client src i3_client_publish.erl
Raw

src/i3_client_publish.erl

% This module abstracts publishing of events. In order to have events
% published as soon as they're received from the socket, the protocol
% needs to have a way to publish them. The protocol doesn't know about
% the `i3_client_connection` callback module in use but needs to
% somehow call its `publish` callback.
%
% This module acts as a bridge by wrapping an `i3_client_connection`
% callback module and its state, and when `call`ed invokes the
% `publish` callback and returns an updated version of itself. The
% protocol receives an opaque "instance" of this module and uses
% it. The only caveat is that the protocol needs to return an
% "instance" to the `it_client_connection`.
-module(i3_client_publish).
-moduledoc false.
-export([wrap/2, call/3, unwrap/1, null/2]).
wrap(Mod, ModState) ->
{ModState, fun Mod:publish/3}.
null(_Mod, ModState) ->
{ModState, fun(_, _, S) -> S end}.
call(Type, Payload, {ModState, Publish}) ->
{Publish(Type, Payload, ModState), Publish}.
unwrap({ModState, _}) ->
ModState.