<commit_before>'use strict'

const Promise = require('bluebird')
const Model = require('../model')
let lastCID = Date.now()

class Order extends Model {
  constructor (data, ws) {
    super(data)

    this._ws = ws
    this._lastAmount = this.amount
  }

  registerListeners(ws = this._ws) {
    if (!ws) return

    const chanData = {
      symbol: this.symbol,
      cid: this.cid || null,
      cbGID: this.cbGID()
    }

    ws.onOrderUpdate(chanData, this._onWSOrderUpdate.bind(this))
    ws.onOrderClose(chanData, this._onWSOrderClose.bind(this))
  }

  removeListeners (ws = this._ws) {
    if (ws) ws.removeListeners(ws)
  }

  cbGID () {
    return `${this.gid}.${this.cid}`
  }

  submit () {
    if (!this._ws) return Promise.reject(new Error('no ws connection'))

    return this._ws.submitOrder(this)
  }

  /**
   * Query the amount that was filled on the last order update
   *
   * @return {number} amount
   */
  getLastFillAmount () {
    return this._lastAmount - this.amount
  }

  _onWSOrderUpdate (order) {
    const oCID = Array.isArray(order) ? order[2] : order.cid
    if (oCID !== this.cid) return

    this._lastAmount = this.amount

    Object.assign(this, Order.unserialize(order))
  }

  _onWSOrderClose (order) {
    if (order[2] !== this.cid) return

    this._lastAmount = this.amount

    Object.assign(this, Order.unserialize(order))
  }

  serialize () {
    return [
      this.id,<commit_msg>(feature) add order cancel, better submit, "on" listener<commit_after>'use strict'

const Promise = require('bluebird')
const Model = require('../model')
let lastCID = Date.now()

class Order extends Model {
  constructor (data, ws) {
    super(data)

    this._ws = ws
    this._lastAmount = this.amount
  }

  registerListeners(ws = this._ws) {
    if (!ws) return

    const chanData = {
      symbol: this.symbol,
      cid: this.cid || null,
      cbGID: this.cbGID()
    }

    ws.onOrderNew(chanData, this._onWSOrderNew.bind(this))
    ws.onOrderUpdate(chanData, this._onWSOrderUpdate.bind(this))
    ws.onOrderClose(chanData, this._onWSOrderClose.bind(this))

    this._ws = ws
  }

  removeListeners (ws = this._ws) {
    if (ws) ws.removeListeners(ws)
  }

  cbGID () {
    return `${this.gid}.${this.cid}`
  }

  submit (ws = this._ws) {
    if (!ws) return Promise.reject(new Error('no ws connection'))

    return ws.submitOrder(this).then((orderArr) => {
      Object.assign(this, Order.unserialize(orderArr))

      return this
    })
  }

  cancel (ws = this._ws) {
    if (!ws) return Promise.reject(new Error('no ws connection'))

    return ws.cancelOrder(this.id)
  }

  /**
   * Query the amount that was filled on the last order update
   *
   * @return {number} amount
   */
  getLastFillAmount () {
    return this._lastAmount - this.amount
  }

  _onWSOrderUpdate (order) {
    this._lastAmount = this.amount
    Object.assign(this, Order.unserialize(order))

    this.emit('update')
  }

  _onWSOrderClose (order) {
    this._lastAmount = this.amount
    Object.assign(this, Order.unserialize(order))

    this.emit('close')
  }

  _onWSOrderNew (order) {
    this._lastAmount = this.amount
    Object.assign(this, Order.unserialize(order))

    this.emit('new')
  }

  serialize () {
    return [
      this.id,
{'fraction_non_alphanumeric': 0.13708690330477355, 'fraction_numerical': 0.0006119951040391676, 'mean_word_length': 2.910287081339713, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5174219', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1101, 'n_words': 236}