123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151(*********************************************************************************)(* Ojs-base *)(* *)(* Copyright (C) 2014-2021 INRIA. All rights reserved. *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Library General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public *)(* License along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)openJs_of_ocamlopenOjs_jslet(>>=)=Lwt.(>>=)letlog=Ojs_js.logmoduletypeP=sigincludeOjs_list.Types.Pvalinsert:Ojs_js.id->elt->Ojs_js.idendmoduleMake(P:P)=structclass['a]elistcallsend~msg_idid=object(self)valmutablelist=([]:(string*'a)list)methodid:Ojs_js.id=idmethodmsg_id:Ojs_js.id=msg_idmethoddisplay_errormsg=Ojs_js.display_text_errormsg_idmsgmethodsimple_call:'aP.client_msg->unitLwt.t=funmsg->callmsg(funmsg->Lwt.return(matchmsgwith|P.SErrormsg->self#display_errormsg|_->()))methodhandle_add(e:'a)=letnew_id=P.insertideinlist<-(new_id,e)::listmethodhandle_delete(e:'a)=trylet(elt_id,_)=List.find(fun(id,elt)->e=elt)listinletparent_node=Ojs_js.node_by_ididin(matchOjs_js.node_by_idelt_idwith|exception_->()|node->ignore(parent_node##removeChild((node:>Dom.nodeJs.t))));list<-List.filter(fun(_,elt)->elt<>e)listwithNot_found->()methodset_list(l:'alist)=(*log (Printf.sprintf "setting list len=%d, id=%s" (List.length l) id);*)letparent_node=Ojs_js.node_by_ididinlog"clearing children ";Ojs_js.clear_childrenparent_node;list<-[];List.iterself#handle_addlmethodhandle_message(msg:'aP.server_msg)=try(matchmsgwith|P.SListl->self#set_listl|P.SAdde->self#handle_adde|P.SDeletee->self#handle_deletee|P.SOk->()|P.SErrormsg->self#display_errormsg|_->failwith"Unhandled message received from server");Js._falsewithe->log(Printexc.to_stringe);Js._falsemethodupdate_list:unitLwt.t=callP.Get(function|P.SListl->Lwt.return(self#set_listl)|P.SErrormsg->Lwt.return(self#display_errormsg)|_->Lwt.return_unit)initializerignore(self#update_list)endclass['a]elists(call:P.app_client_msg->(P.app_server_msg->unitLwt.t)->unitLwt.t)(send:P.app_client_msg->unitLwt.t)spawn(* (spawn : ('clt -> ('srv -> unit Lwt.t) -> unit Lwt.t) ->
('clt -> unit) ->
msg_id: string -> string -> ('clt, 'srv) tree) *)=object(self)valmutablelists=(SMap.empty:'aelistSMap.t)methodget_listid=trySMap.findidlistswithNot_found->failwith("No list "^id)methodget_msg_idid=(self#get_listid)#msg_idmethodsetup_list~(msg_id:string)(id:string)=letsendmsg=send(P.pack_client_msgidmsg)inletcallmsgcb=letcbmsg=matchP.unpack_server_msgmsgwith|Some(_,msg)->cbmsg|None->Lwt.return_unitincall(P.pack_client_msgidmsg)cbinletl=spawncallsend~msg_ididinlists<-SMap.addidllists;lmethodhandle_messagemsg=matchP.unpack_server_msgmsgwith|Some(id,msg)->letl=self#get_listidinl#handle_messagemsg|None->Js._falseendend