<commit_before>%% @author <NAME> <<EMAIL>>
%% @copyright 2009 <NAME>
%%
%% @doc Simple implementation of an observer/notifier. Relays events to observers of that event.
%% Also implements map and fold operations over the observers.

%% Copyright 2009 <NAME>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%% 
%%     http://www.apache.org/licenses/LICENSE-2.0
%% 
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(z_notifier).

-author("<NAME> <<EMAIL>>").

-behaviour(gen_server).

%% gen_server exports
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start_link/1]).

%% interface functions
-export([
    observe/3,
    observe/4,
    detach/3,
    detach_all/2,
    get_observers/2,
    notify/2, 
    notify1/2, 
    first/2, 
    map/2, 
    foldl/3, 
    foldr/3
]).

%% internal
-export([notify_observer/4, test/0, test_observer/2]).

-include_lib("zotonic.hrl").

-define(DEFAULT_PRIORITY, 500).
-define(TIMEOUT, 60000).

-record(state, {observers}).

%%====================================================================
%% API
%%====================================================================
%% @spec start_link(SiteProps) -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the notification server
start_link(SiteProps) when is_list(SiteProps) ->
    {host, Host} = proplists:lookup(host, SiteProps),
    Name = z_utils:name_for_host(?MODULE, Host),
    gen_server:start_link({local, Name}, ?MODULE, SiteProps, []).


%%====================================================================
%% API for subscription
%%====================================================================

%% @doc Subscribe to an event. Observer is a {M,F} or pid()
observe(Event, Observer, Context) ->
    observe(Event, Observer, ?DEFAULT_PRIORITY, Context).

%% @doc Subscribe to an event. Observer is a {M,F} or pid()
observe(Event, Observer, Priority, #context{notifier=Notifier}) ->
    gen_server:cast(Notifier, {'observe', Event, Observer, Priority}).

%% @doc Detach all observers and delete the event
detach_all(Event, #context{notifier=Notifier}) ->
    gen_server:cast(Notifier, {'detach_all', Event}).

%% @doc Unsubscribe from an event. Observer is a {M,F} or pid()<commit_msg>When using z_notifier:observe/3 with {Module, Fun} syntax, take into account the module priority.<commit_after>%% @author <NAME> <<EMAIL>>
%% @copyright 2009 <NAME>
%%
%% @doc Simple implementation of an observer/notifier. Relays events to observers of that event.
%% Also implements map and fold operations over the observers.

%% Copyright 2009 <NAME>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%% 
%%     http://www.apache.org/licenses/LICENSE-2.0
%% 
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(z_notifier).

-author("<NAME> <<EMAIL>>").

-behaviour(gen_server).

%% gen_server exports
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start_link/1]).

%% interface functions
-export([
    observe/3,
    observe/4,
    detach/3,
    detach_all/2,
    get_observers/2,
    notify/2, 
    notify1/2, 
    first/2, 
    map/2, 
    foldl/3, 
    foldr/3
]).

%% internal
-export([notify_observer/4, test/0, test_observer/2]).

-include_lib("zotonic.hrl").

-define(DEFAULT_PRIORITY, 500).
-define(TIMEOUT, 60000).

-record(state, {observers}).

%%====================================================================
%% API
%%====================================================================
%% @spec start_link(SiteProps) -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the notification server
start_link(SiteProps) when is_list(SiteProps) ->
    {host, Host} = proplists:lookup(host, SiteProps),
    Name = z_utils:name_for_host(?MODULE, Host),
    gen_server:start_link({local, Name}, ?MODULE, SiteProps, []).


%%====================================================================
%% API for subscription
%%====================================================================

%% @doc Subscribe to an event. Observer is a {M,F} or pid()
observe(Event, {Module, Function}, Context) ->
    observe(Event, {Module, Function}, z_module_sup:prio(Module), Context);
observe(Event, Observer, Context) ->
    observe(Event, Observer, ?DEFAULT_PRIORITY, Context).

%% @doc Subscribe to an event. Observer is a {M,F} or pid()
observe(Event, Observer, Priority, #context{notifier=Notifier}) ->
    gen_server:cast(Notifier, {'observe', Event, Observer, Priority}).

%% @doc Detach all observers and delete the event
detach_all(Event, #context{notifier=Notifier}) ->
    gen_server:cast(Notifier, {'detach_all', Event}).

%% @doc Unsubscribe from an event. Observer is a {M,F} or pid()
{'fraction_non_alphanumeric': 0.22774252039891205, 'fraction_numerical': 0.015412511332728921, 'mean_word_length': 4.956803455723542, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3071603', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1609, 'n_words': 496}