Skip to content

Commit

Permalink
feature: isSupertypeOf now accept a NodeIdLike argument
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Feb 6, 2022
1 parent d8f2d2a commit 6e51af7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LocalizedText, NodeClass } from "node-opcua-data-model";
import { NodeId } from "node-opcua-nodeid";
import { NodeId, NodeIdLike } from "node-opcua-nodeid";
import { BaseNode } from "./base_node";
import { UAReference } from "./ua_reference";

Expand All @@ -10,7 +10,7 @@ export declare class UAReferenceType extends BaseNode {
public readonly isAbstract: boolean;
public readonly inverseName: LocalizedText;

public isSupertypeOf(baseType: UAReferenceType): boolean;
public isSupertypeOf(baseType: UAReferenceType | NodeIdLike): boolean;

public getAllSubtypes(): UAReferenceType[];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export declare class UAVariableType extends BaseNode implements VariableAttribut

public isAbstract: boolean;

public isSupertypeOf(type: UAVariableType): boolean;
public isSupertypeOf(type: UAVariableType | NodeIdLike): boolean;

public instantiate(options: InstantiateVariableOptions): UAVariable;
}
Expand Down
18 changes: 12 additions & 6 deletions packages/node-opcua-address-space/src/tool_isSupertypeOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import { assert } from "node-opcua-assert";
import { NodeId, resolveNodeId } from "node-opcua-nodeid";
import { NodeId, NodeIdLike, resolveNodeId } from "node-opcua-nodeid";
import { sameNodeId } from "node-opcua-nodeid";
import { BaseNode, UADataType, UAObjectType, UAReference, UAReferenceType, UAVariableType } from "node-opcua-address-space-base";

Expand All @@ -19,7 +19,14 @@ function _filterSubType(reference: UAReference) {

export type BaseNodeConstructor<T extends BaseNode> = new () => T;

function _slow_isSupertypeOf<T extends UAType>(this: T, Class: typeof BaseNodeImpl, baseType: T): boolean {
function _slow_isSupertypeOf<T extends UAType>(this: T, Class: typeof BaseNodeImpl, baseType: T | NodeIdLike): boolean {
if (!(baseType instanceof Class)) {
const node = this.addressSpace.findNode(baseType as NodeIdLike);
if (!node || !(node instanceof Class)) {
throw new Error("Invalid argument");
}
return _slow_isSupertypeOf.call(this, Class, node as unknown as T);
}
assert(this instanceof Class);
assert(baseType instanceof Class, " Object must have same type");
assert(this.addressSpace);
Expand Down Expand Up @@ -88,16 +95,15 @@ export type IsSupertypeOfFunc<T extends UAType> = (this: T, baseType: T) => bool
export type UAType = UAReferenceType | UADataType | UAObjectType | UAVariableType;

export function construct_isSupertypeOf<T extends UAType>(Class: typeof BaseNodeImpl): IsSupertypeOfFunc<T> {
assert(typeof Class === "function");
return wrap_memoize(function (this: T, baseType: T): boolean {
return wrap_memoize(function (this: T, baseType: T | NodeIdLike): boolean {
assert(baseType instanceof Class);
assert(this instanceof Class);
return _slow_isSupertypeOf.call(this, Class, baseType);
return _slow_isSupertypeOf.call(this, Class, baseType as T);
}, hashBaseNode);
}

export function construct_slow_isSupertypeOf<T extends UAType>(Class: typeof BaseNodeImpl) {
return function (this: T, baseType: T): boolean {
return function (this: T, baseType: T | NodeIdLike): boolean {
return _slow_isSupertypeOf.call(this, Class, baseType);
};
}
Expand Down

0 comments on commit 6e51af7

Please sign in to comment.