#NEWLINE# @ defer . inlineCallbacks #NEWLINE# def user_syncing( self , user_id , affect_presence = True ) : #NEWLINE# #INDENT# "Returns a context manager that should surround any stream requests\n        from the user.\n\n        This allows us to keep track of who is currently streaming and who isn't\n        without having to have timers outside of this module to avoid flickering\n        when users disconnect/reconnect.\n\n        Args:\n            user_id (str)\n            affect_presence (bool): If false this function will be a no-op.\n                Useful for streams that are not associated with an actual\n                client that is being used by a user.\n        " #NEWLINE# if affect_presence : #NEWLINE# #INDENT# curr_sync = self . user_to_num_current_syncs . get ( curr_sync , 0 ) #NEWLINE# self . user_to_num_current_syncs [ user_id ] = ( curr_sync + 1 ) #NEWLINE# prev_state = ( yield self . current_state_for_user ( user_id ) ) #NEWLINE# if ( prev_state . state == PresenceState . OFFLINE ) : #NEWLINE# #INDENT# ( yield self . _update_states ( [ prev_state . copy_and_replace ( state = PresenceState . ONLINE , last_active_ts = self . clock . time_msec ( ) , last_user_sync_ts = self . clock . time_msec ( ) ) ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( yield self . _update_states ( [ prev_state . copy_and_replace ( last_user_sync_ts = self . clock . time_msec ( ) ) ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# @ defer . inlineCallbacks #NEWLINE# def _end( ) : #NEWLINE# #INDENT# if affect_presence : #NEWLINE# #INDENT# self . user_to_num_current_syncs [ user_id ] -= 1 #NEWLINE# prev_state = ( yield self . current_state_for_user ( user_id ) ) #NEWLINE# ( yield self . _update_states ( [ prev_state . copy_and_replace ( last_user_sync_ts = self . clock . time_msec ( ) ) ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# @ contextmanager #NEWLINE# def _user_syncing( ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ( yield ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# preserve_fn ( _end ) ( ) #NEWLINE# #UNINDENT# #UNINDENT# defer . returnValue ( _user_syncing ( ) )