<commit_before>-module(fingwb_msg_handler).
-behaviour(cowboy_websocket_handler).

-export([init/3]).
-export([websocket_init/3]).
-export([websocket_handle/3]).
-export([websocket_info/3]).
-export([websocket_terminate/3]).

-record(ws_state, {
	id
}).

init({tcp, http}, _Req, _Opts) ->
	{upgrade, protocol, cowboy_websocket}.

websocket_init(_TransportName, Req, _Opts) ->
	{WhiteBoardId, Req2} = cowboy_req:binding(whiteboard_id, Req),
	[ self() ! {join, Pid}|| Pid <- fingwb_whiteboard:watchers(WhiteBoardId)],
	ok = fingwb_whiteboard:watch(WhiteBoardId),
	ok = fingwb_whiteboard:notify(WhiteBoardId, {join, self()}),
	[ self() ! Message || Message <- fingwb_whiteboard:readArchive(WhiteBoardId)],
	{ok, Req2, #ws_state{id=WhiteBoardId}}.

websocket_handle(Data, Req, State = #ws_state{id=WbId}) ->
	ok = fingwb_whiteboard:publish(WbId, Data),
	{ok, Req, State}.

websocket_info({join, Pid}, Req, State) ->
	{reply, {text, jiffy:encode({[{<<"join">>, list_to_binary(pid_to_list(Pid))}]})}, Req, State};
websocket_info({leave, Pid}, Req, State) ->
	{reply, {text, jiffy:encode({[{<<"leave">>, list_to_binary(pid_to_list(Pid))}]})}, Req, State};
websocket_info(Message, Req, State) ->
	{reply, Message, Req, State}.

websocket_terminate(_Reason, _Req, #ws_state{id=WbId}) ->
	fingwb_whiteboard:notify(WbId, {leave, self()}),
	fingwb_whiteboard:unWatch(WbId).<commit_msg>proper handling for case where page reloads on purged board<commit_after>-module(fingwb_msg_handler).
-behaviour(cowboy_websocket_handler).

-export([init/3]).
-export([websocket_init/3]).
-export([websocket_handle/3]).
-export([websocket_info/3]).
-export([websocket_terminate/3]).

-record(ws_state, {
	id
}).

init({tcp, http}, _Req, _Opts) ->
	{upgrade, protocol, cowboy_websocket}.

websocket_init(_TransportName, Req, _Opts) ->
	{WhiteBoardId, Req2} = cowboy_req:binding(whiteboard_id, Req),
	case fingwb_whiteboard:exists(WhiteBoardId) of
		false -> {shutdown, Req2};
		true  ->
			[ self() ! {join, Pid}|| Pid <- fingwb_whiteboard:watchers(WhiteBoardId)],
			ok = fingwb_whiteboard:watch(WhiteBoardId),
			ok = fingwb_whiteboard:notify(WhiteBoardId, {join, self()}),
			[ self() ! Message || Message <- fingwb_whiteboard:readArchive(WhiteBoardId)],
			{ok, Req2, #ws_state{id=WhiteBoardId}}
	end.

websocket_handle(Data, Req, State = #ws_state{id=WbId}) ->
	ok = fingwb_whiteboard:publish(WbId, Data),
	{ok, Req, State}.

websocket_info({join, Pid}, Req, State) ->
	{reply, {text, jiffy:encode({[{<<"join">>, list_to_binary(pid_to_list(Pid))}]})}, Req, State};
websocket_info({leave, Pid}, Req, State) ->
	{reply, {text, jiffy:encode({[{<<"leave">>, list_to_binary(pid_to_list(Pid))}]})}, Req, State};
websocket_info(Message, Req, State) ->
	{reply, Message, Req, State}.

websocket_terminate(_Reason, _Req, #ws_state{id=WbId}) ->
	fingwb_whiteboard:notify(WbId, {leave, self()}),
	fingwb_whiteboard:unWatch(WbId).
{'fraction_non_alphanumeric': 0.19032815198618308, 'fraction_numerical': 0.0051813471502590676, 'mean_word_length': 6.977961432506887, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5186728', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1138, 'n_words': 186}