<commit_before>// @flow
// Handles sending requests to the daemon
import Session from './session'
import setupLocalLogs from '../util/local-log'
import type {CancelHandlerType} from './session'
import type {createClientType} from './index.platform'
import type {incomingCallMapType, logUiLogRpcParam} from '../constants/types/flow-types'
import {ConstantsStatusCode} from '../constants/types/flow-types'
import {isMobile} from '../constants/platform'
import {log} from '../native/log/logui'
import {resetClient, createClient, rpcLog} from './index.platform'
import {printOutstandingRPCs} from '../local-debug'

const {logLocal} = setupLocalLogs()

class Engine {
  // Tracking outstanding sessions
  _sessionsMap: {[key: SessionIDKey]: Session} = {}
  // Helper we delegate actual calls to
  _rpcClient: createClientType
  // All incoming call handlers
  _incomingHandler: {[key: MethodKey]: (param: Object, response: ?Object) => void} = {}
  // Keyed methods that care when we reconnect. Is null while we're handing _onConnect
  _onConnectHandlers: ?{[key: string]: () => void} = {}
  // Set to true to throw on errors. Used in testing
  _failOnError: boolean = false
  // We generate sessionIDs monotonically
  _nextSessionID: number = 123

  constructor () {
    this._setupClient()
    this._setupCoreHandlers()
    this._setupDebugging()
  }

  _setupClient () {
    this._rpcClient = createClient(
      payload => this._rpcIncoming(payload),
      () => this._onConnected()
    )
  }

  _setupDebugging () {
    if (!__DEV__) {
      return
    }

    if (typeof window !== 'undefined') {
      console.log('DEV MODE ENGINE AVAILABLE AS window.DEBUGengine')
      window.DEBUGengine = this
    }

    // Print out any alive sessions periodically
    if (printOutstandingRPCs) {
      setInterval(() => {
        if (Object.keys(this._sessionsMap).filter(k => !this._sessionsMap[k].dangling).length) {
          logLocal('outstandingSessionDebugger: ', this._sessionsMap)
        }
      }, 10 * 1000)
    }
  }

  // Default handlers for incoming messages
  _setupCoreHandlers () {
    this.setIncomingHandler('keybase.1.logUi.log', (param, response) => {
      const logParam: logUiLogRpcParam = param
      log(logParam)
      response && response.result && response.result()
    })
    this.setIncomingHandler('keybase.1.NotifyUsers.userChanged', () => {})
  }

  // Called when we reconnect to the server
  _onConnected () {
    // This should be impossible but makes flow happy
    if (!this._onConnectHandlers) {
      return
    }

    const handlers = this._onConnectHandlers<commit_msg>move registered unhandled to their own helper<commit_after>// @flow
// Handles sending requests to the daemon
import Session from './session'
import setupLocalLogs from '../util/local-log'
import type {CancelHandlerType} from './session'
import type {createClientType} from './index.platform'
import type {incomingCallMapType, logUiLogRpcParam} from '../constants/types/flow-types'
import {ConstantsStatusCode} from '../constants/types/flow-types'
import {isMobile} from '../constants/platform'
import {log} from '../native/log/logui'
import {resetClient, createClient, rpcLog} from './index.platform'
import {printOutstandingRPCs} from '../local-debug'

const {logLocal} = setupLocalLogs()

class Engine {
  // Tracking outstanding sessions
  _sessionsMap: {[key: SessionIDKey]: Session} = {}
  // Helper we delegate actual calls to
  _rpcClient: createClientType
  // All incoming call handlers
  _incomingHandler: {[key: MethodKey]: (param: Object, response: ?Object) => void} = {}
  // Keyed methods that care when we reconnect. Is null while we're handing _onConnect
  _onConnectHandlers: ?{[key: string]: () => void} = {}
  // Set to true to throw on errors. Used in testing
  _failOnError: boolean = false
  // We generate sessionIDs monotonically
  _nextSessionID: number = 123

  constructor () {
    this._setupClient()
    this._setupCoreHandlers()
    this._setupIgnoredHanlders()
    this._setupDebugging()
  }

  _setupClient () {
    this._rpcClient = createClient(
      payload => this._rpcIncoming(payload),
      () => this._onConnected()
    )
  }

  _setupDebugging () {
    if (!__DEV__) {
      return
    }

    if (typeof window !== 'undefined') {
      console.log('DEV MODE ENGINE AVAILABLE AS window.DEBUGengine')
      window.DEBUGengine = this
    }

    // Print out any alive sessions periodically
    if (printOutstandingRPCs) {
      setInterval(() => {
        if (Object.keys(this._sessionsMap).filter(k => !this._sessionsMap[k].dangling).length) {
          logLocal('outstandingSessionDebugger: ', this._sessionsMap)
        }
      }, 10 * 1000)
    }
  }

  // Default handlers for incoming messages
  _setupCoreHandlers () {
    this.setIncomingHandler('keybase.1.logUi.log', (param, response) => {
      const logParam: logUiLogRpcParam = param
      log(logParam)
      response && response.result && response.result()
    })
  }

  _setupIgnoredHanlders () {
    // The ui doesn't do anything with these calls currently. We handle it so we don't
    // get an unhandled rpc warning.
    this.setIncomingHandler('keybase.1.NotifyUsers.userChanged', () => {})
  }

  // Called when we reconnect to the server
  _onConnected () {
    // This should be impossible but makes flow happy
    if (!this._onConnectHandlers) {
      return
    }

    const handlers = this._onConnectHandlers
{'fraction_non_alphanumeric': 0.12142988759904183, 'fraction_numerical': 0.004053805048829924, 'mean_word_length': 3.3633440514469455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '1643526', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1584, 'n_words': 437}