<commit_before>bound_       = require './bound'
Promise         = require 'bluebird'
EventEmitter    = require 'microemitter'
Encoder         = require 'htmlencode'

module.exports = class Bongo extends EventEmitter

  [NOTCONNECTED, CONNECTING, CONNECTED, DISCONNECTED] = [0,1,2,3]

  BATCH_CHUNK_MS = 300

  # bongo     = new EventEmitter
  # contrib
  Traverse  = require 'traverse'
  createId  = @createId = require 'hat'

  JsPath    = @JsPath = require 'jspath'

  @dnodeProtocol          = require 'dnode-protocol'
  @dnodeProtocol.Scrubber = require './scrubber'
  @promibackify           = require './promibackify'

  {Store, Scrubber} = @dnodeProtocol

  @EventEmitter = EventEmitter
  Model = @Model = require './model'
  @ListenerTree = require './listenertree'
  EventBus  = @EventBus = require './eventbus'
  OpaqueType = require './opaquetype'
  Signature = require './signature'

  # mixin the event emitter for the AMQP broker
  Model::mixin require './eventemitter/broker'
  # need these aliases:
  Model::off = Model::removeListener
  Model::addGlobalListener = Model::on

  # TODO: temporary hack
  # @KDML = require 'kdml'


  @bound = bound_

  bound: bound_

  createBongoName =(resourceName)->
    "#{createId 128}.unknown.bongo-#{resourceName}"
  {slice}           = []

  constructor:(options)->
    EventEmitter this
    { @mq, @getSessionToken, @getUserArea, @fetchName, @resourceName,
      @apiEndpoint, @useWebsockets, @batchRequests, @apiDescriptor } = options
    @useWebsockets ?= no
    @batchRequests ?= yes
    @getUserArea   ?= -> # noop
    @localStore     = new Store
    @remoteStore    = new Store
    @readyState     = NOTCONNECTED
    @stack          = []
    @opaqueTypes  = {}
    @on 'newListener', (event, listener)=>
      if event is 'ready' and @readyState is CONNECTED
        process.nextTick =>
          @emit 'ready'
          @off 'ready'
    @setOutboundTimer()  if @batchRequests
    unless @useWebsockets
      @once 'ready', => process.nextTick @bound 'xhrHandshake'

    @api = @createRemoteApiShims @apiDescriptor

    if @mq?
      @eventBus = new EventBus @mq
      @mq.on 'disconnected', =>
        @disconnectedAt = Date.now()
        @emit 'disconnected'
        @readyState = DISCONNECTED

  isConnected: -> return @readyState is CONNECTED

  cacheable: require './cacheable'

  cacheableAsync: (rest...) ->
    new Promise (resolve, reject) =><commit_msg>connection: fixes safari's connectivity issue<commit_after>Promise         = require 'bluebird'
EventEmitter    = require 'microemitter'
Encoder         = require 'htmlencode'
Traverse        = require 'traverse'
createId        = require 'hat'
JsPath          = require 'jspath'
Model           = require './model'
ListenerTree    = require './listenertree'
EventBus        = require './eventbus'
OpaqueType      = require './opaquetype'
Signature       = require './signature'
bound           = require './bound'
createBongoName = (resourceName) -> "#{createId 128}.unknown.bongo-#{resourceName}"

do ->
  # mixin the event emitter for the AMQP broker
  Model::mixin require './eventemitter/broker'
  # need these aliases:
  Model::off               = Model::removeListener
  Model::addGlobalListener = Model::on

module.exports = class Bongo extends EventEmitter

  [NOTCONNECTED, CONNECTING, CONNECTED, DISCONNECTED] = [0,1,2,3]
  BATCH_CHUNK_MS = 300

  @dnodeProtocol          = require 'dnode-protocol'
  @dnodeProtocol.Scrubber = require './scrubber'
  @promibackify           = require './promibackify'

  {Store, Scrubber} = @dnodeProtocol
  {slice}           = []

  constructor:(options)->
    EventEmitter this
    { @mq, @getSessionToken, @getUserArea, @fetchName, @resourceName,
      @apiEndpoint, @useWebsockets, @batchRequests, @apiDescriptor } = options
    @useWebsockets ?= no
    @batchRequests ?= yes
    @getUserArea   ?= -> # noop
    @localStore     = new Store
    @remoteStore    = new Store
    @readyState     = NOTCONNECTED
    @stack          = []
    @opaqueTypes  = {}
    @on 'newListener', (event, listener)=>
      if event is 'ready' and @readyState is CONNECTED
        process.nextTick =>
          @emit 'ready'
          @off 'ready'
    @setOutboundTimer()  if @batchRequests
    unless @useWebsockets
      @once 'ready', => process.nextTick @bound 'xhrHandshake'

    @api = @createRemoteApiShims @apiDescriptor

    if @mq?
      @eventBus = new EventBus @mq
      @mq.on 'disconnected', =>
        @disconnectedAt = Date.now()
        @emit 'disconnected'
        @readyState = DISCONNECTED

  bound : bound

  isConnected: -> return @readyState is CONNECTED

  cacheable: require './cacheable'

  cacheableAsync: (rest...) ->
    new Promise (resolve, reject) =>
{'fraction_non_alphanumeric': 0.11763463569165787, 'fraction_numerical': 0.004223864836325237, 'mean_word_length': 2.866122448979592, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, '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': '2798910', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1453, 'n_words': 367}