projects/wms-framework/src/lib/utils/RefOrOut.ts
Wraps ref
and out
parameter types.
Allows to modify the value of arguments and parameters.
Properties |
|
Accessors |
constructor(value: T, setter: (newValue: T) => void)
|
||||||||||||
Creates an instance of RefOrOut.
Parameters :
|
Private innerValue |
Type : T
|
Internal value to be set or modified. |
Private setter |
Type : function
|
Function to set the value of the external variable. |
value | ||||||
getvalue()
|
||||||
Value to be set or modified.
Returns :
T
|
||||||
setvalue(value: T)
|
||||||
Parameters :
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);
}