@@ -9,13 +9,11 @@ defmodule ChromeRemoteInterface.PageSession do
99
1010 use GenServer
1111
12- defstruct [
13- url: "" ,
14- socket: nil ,
15- callbacks: [ ] ,
16- event_subscribers: % { } ,
17- ref_id: 1
18- ]
12+ defstruct url: "" ,
13+ socket: nil ,
14+ callbacks: [ ] ,
15+ event_subscribers: % { } ,
16+ ref_id: 1
1917
2018 # ---
2119 # Public API
@@ -25,6 +23,7 @@ defmodule ChromeRemoteInterface.PageSession do
2523 Connect to a Page's 'webSocketDebuggerUrl'.
2624 """
2725 def start_link ( % { "webSocketDebuggerUrl" => url } ) , do: start_link ( url )
26+
2827 def start_link ( url ) do
2928 GenServer . start_link ( __MODULE__ , url )
3029 end
@@ -58,15 +57,15 @@ defmodule ChromeRemoteInterface.PageSession do
5857 "params" => %{"timestamp" => 1012329.888558}}}
5958 ```
6059 """
61- @ spec subscribe ( pid ( ) , String . t , pid ( ) ) :: any ( )
60+ @ spec subscribe ( pid ( ) , String . t ( ) , pid ( ) ) :: any ( )
6261 def subscribe ( pid , event , subscriber_pid \\ self ( ) ) do
6362 GenServer . call ( pid , { :subscribe , event , subscriber_pid } )
6463 end
6564
6665 @ doc """
6766 Unsubscribes from an event.
6867 """
69- @ spec unsubscribe ( pid ( ) , String . t , pid ( ) ) :: any ( )
68+ @ spec unsubscribe ( pid ( ) , String . t ( ) , pid ( ) ) :: any ( )
7069 def unsubscribe ( pid , event , subscriber_pid \\ self ( ) ) do
7170 GenServer . call ( pid , { :unsubscribe , event , subscriber_pid } )
7271 end
@@ -121,6 +120,7 @@ defmodule ChromeRemoteInterface.PageSession do
121120
122121 def init ( url ) do
123122 { :ok , socket } = ChromeRemoteInterface.Websocket . start_link ( url )
123+
124124 state = % __MODULE__ {
125125 url: url ,
126126 socket: socket
@@ -156,7 +156,7 @@ defmodule ChromeRemoteInterface.PageSession do
156156 new_event_subscribers =
157157 state
158158 |> Map . get ( :event_subscribers , % { } )
159- |> Map . update ( event , [ subscriber_pid ] , fn ( subscriber_pids ) ->
159+ |> Map . update ( event , [ subscriber_pid ] , fn subscriber_pids ->
160160 [ subscriber_pid | subscriber_pids ]
161161 end )
162162
@@ -169,7 +169,7 @@ defmodule ChromeRemoteInterface.PageSession do
169169 new_event_subscribers =
170170 state
171171 |> Map . get ( :event_subscribers , % { } )
172- |> Map . update ( event , [ ] , fn ( subscriber_pids ) ->
172+ |> Map . update ( event , [ ] , fn subscriber_pids ->
173173 List . delete ( subscriber_pids , subscriber_pid )
174174 end )
175175
@@ -182,7 +182,7 @@ defmodule ChromeRemoteInterface.PageSession do
182182 new_event_subscribers =
183183 state
184184 |> Map . get ( :event_subscribers , % { } )
185- |> Enum . map ( fn ( { key , subscriber_pids } ) ->
185+ |> Enum . map ( fn { key , subscriber_pids } ->
186186 { key , List . delete ( subscriber_pids , subscriber_pid ) }
187187 end )
188188 |> Enum . into ( % { } )
@@ -200,7 +200,7 @@ defmodule ChromeRemoteInterface.PageSession do
200200 # If the frame is an event:
201201 # - Forward the event to any subscribers.
202202 def handle_info ( { :message , frame_data } , state ) do
203- json = Poison . decode! ( frame_data )
203+ json = Jason . decode! ( frame_data )
204204 id = json [ "id" ]
205205 method = json [ "method" ]
206206
@@ -227,14 +227,14 @@ defmodule ChromeRemoteInterface.PageSession do
227227 "params" => params
228228 }
229229
230- json = Poison . encode! ( message )
230+ json = Jason . encode! ( message )
231231 WebSockex . send_frame ( socket , { :text , json } )
232232 { :noreply , state }
233233 end
234234
235235 defp add_callback ( state , from ) do
236236 state
237- |> Map . update ( :callbacks , [ { state . ref_id , from } ] , fn ( callbacks ) ->
237+ |> Map . update ( :callbacks , [ { state . ref_id , from } ] , fn callbacks ->
238238 [ { state . ref_id , from } | callbacks ]
239239 end )
240240 end
@@ -252,18 +252,20 @@ defmodule ChromeRemoteInterface.PageSession do
252252 defp send_rpc_response ( callbacks , id , json ) do
253253 error = json [ "error" ]
254254
255- Enum . find ( callbacks , fn ( { ref_id , _from } ) ->
255+ Enum . find ( callbacks , fn { ref_id , _from } ->
256256 ref_id == id
257257 end )
258258 |> case do
259259 { _ref_id , { :cast , method , from } } = callback ->
260260 event = { :chrome_remote_interface , method , json }
261261 send ( from , event )
262262 remove_callback ( callbacks , callback )
263+
263264 { _ref_id , { :call , from } } = callback ->
264265 status = if error , do: :error , else: :ok
265266 GenServer . reply ( from , { status , json } )
266267 remove_callback ( callbacks , callback )
268+
267269 _ ->
268270 callbacks
269271 end
@@ -277,7 +279,7 @@ defmodule ChromeRemoteInterface.PageSession do
277279 |> Map . get ( event_name , [ ] )
278280
279281 pids_subscribed_to_event
280- |> Enum . each ( & ( send ( & 1 , event ) ) )
282+ |> Enum . each ( & send ( & 1 , event ) )
281283 end
282284
283285 def terminate ( _reason , state ) do
0 commit comments