File

projects/wms-framework/src/lib/utils/RefOrOut.ts

Description

Wraps ref and out parameter types. Allows to modify the value of arguments and parameters.

Index

Properties
Accessors

Constructor

constructor(value: T, setter: (newValue: T) => void)

Creates an instance of RefOrOut.

Parameters :
Name Type Optional Description
value T No

Value to be set or modified.

setter function No

Function to call to set the value.

Properties

Private innerValue
Type : T

Internal value to be set or modified.

Private setter
Type : function

Function to set the value of the external variable.

Accessors

value
getvalue()

Value to be set or modified.

Returns : T
setvalue(value: T)
Parameters :
Name Type Optional
value T No
Returns : void
export class RefOrOut<T> {
  /**
   * Function to set the value of the external variable.
   *
   * @private
   * @memberof RefOrOut
   */
  private setter: (x_ref: T) => void;

  /**
   * Internal value to be set or modified.
   *
   * @private
   * @type {T}
   * @memberof RefOrOut
   */
  private innerValue: T;

  /**
   * Value to be set or modified.
   *
   * @type {T}
   * @memberof RefOrOut
   */
  get value(): T {
    return this.innerValue;
  }

  set value(value: T) {
    this.innerValue = value;
    this.setter(value);
  }

  /**
   * Creates an instance of RefOrOut.
   *
   * @param {T} value Value to be set or modified.
   * @param {(newValue: T) => void} setter Function to call to set the value.
   * @memberof RefOrOut
   */
  constructor(value: T, setter: (newValue: T) => void) {
    this.setter = setter;
    this.innerValue = value;
  }
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export type ref<T> = RefOrOut<T>;

/**
 * Creates a `RefOrOut` instance for `ref` arguments.
 *
 * @export
 * @template T
 * @param {T} value The value to be modified.
 * @param {(newValue: T) => void} setter The function to call to set the value.
 * @return {*}  {ref<T>} A new instance of `RefOrOut` for `ref` arguments.
 */
// eslint-disable-next-line no-redeclare
export function ref<T>(value: T, setter: (newValue: T) => void): ref<T> {
  return new RefOrOut(value, setter);
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export type out<T> = RefOrOut<T>;

/**
 * Creates a `RefOrOut` instance for `out` arguments.
 *
 * @export
 * @template T
 * @param {(newValue: T) => void} setter The function to call to set the value.
 * @return {*}  {out<T>} A new instance of `RefOrOut` for `out` arguments.
 */
// eslint-disable-next-line no-redeclare
export function out<T>(setter: (newValue: T) => void): out<T> {
  return new RefOrOut(undefined, setter);
}

result-matching ""

    No results matching ""