HEX
Server: nginx/1.24.0
System: Linux nowruzgan 6.8.0-57-generic #59-Ubuntu SMP PREEMPT_DYNAMIC Sat Mar 15 17:40:59 UTC 2025 x86_64
User: babak (1000)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: //usr/share/opensearch-dashboards/node_modules/async-value/index.js
'use strict'

const assert = require('assert')

module.exports = class AsyncValue {
  constructor(value) {
    this.value = null
    this.callbacks = []
    if (value !== null && value !== undefined) {
      this.set(value)
    }
  }

  get(callback) {
    assert(typeof callback === 'function', 'callback must be a function')
    if (this.value !== null) {
      callback(this.value)
    } else {
      this.callbacks.push(callback)
    }
  }

  set(value) {
    assert(this.value === null, 'value can only be set once')
    if (value instanceof AsyncValue) {
      value.send(this)
      return
    }
    for (let callback of this.callbacks) {
      callback(value)
    }
    this.callbacks = null
    this.value = value
  }

  send(target) {
    assert(target instanceof AsyncValue, 'send target must be an async value')
    this.get(target.set.bind(target))
  }
}