Skip to content

Commit

Permalink
refactor: replace INodeQuery/isNodeQuery with hasNodeId
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Oct 24, 2024
1 parent 0d9e742 commit 943a65c
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 39 deletions.
1 change: 1 addition & 0 deletions docs/getting-started/migrating/v14.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,4 @@ Most of these should not affect any application:
- The `Message.options` property was removed
- The `ICommandClassContainer` interface was replaced with the new `ContainsCC` interface, which indicates that something contains a deserialized CC instance.
- The `Driver.computeNetCCPayloadSize` method now requires that the passed message instance contains a deserialized CC instance.
- The `INodeQuery` interface and the `isNodeQuery` function were removed. To test whether a message references a node, use `hasNodeId(msg)` instead, which communicates the intent more clearly.
1 change: 0 additions & 1 deletion packages/serial/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
export { SerialLogger } from "./log/Logger";
export type { SerialLogContext } from "./log/Logger_safe";
export * from "./message/Constants";
export * from "./message/INodeQuery";
export * from "./message/Message";
export * from "./message/MessageHeaders";
export * from "./message/SuccessIndicator";
Expand Down
10 changes: 0 additions & 10 deletions packages/serial/src/message/INodeQuery.ts

This file was deleted.

5 changes: 2 additions & 3 deletions packages/serial/src/message/Message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ZWaveErrorCodes, assertZWaveError } from "@zwave-js/core";
import { createTestingHost } from "@zwave-js/host";
import test from "ava";
import { FunctionType, MessageType } from "./Constants";
import type { INodeQuery } from "./INodeQuery";
import { Message, messageTypes } from "./Message";

test("should deserialize and serialize correctly", (t) => {
Expand Down Expand Up @@ -228,10 +227,10 @@ test("tryGetNode() returns the associated node otherwise", (t) => {
});

// This node exists
(msg as any as INodeQuery).nodeId = 1;
(msg as any).nodeId = 1;
t.is(msg.tryGetNode(host), host.getNode(1));

// This one does
(msg as any as INodeQuery).nodeId = 2;
(msg as any).nodeId = 2;
t.is(msg.tryGetNode(host), undefined);
});
12 changes: 10 additions & 2 deletions packages/serial/src/message/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type {
import type { JSONObject, TypedClassDecorator } from "@zwave-js/shared/safe";
import { num2hex, staticExtends } from "@zwave-js/shared/safe";
import { FunctionType, MessageType } from "./Constants";
import { isNodeQuery } from "./INodeQuery";
import { MessageHeaders } from "./MessageHeaders";

export type MessageConstructor<T extends Message> = typeof Message & {
Expand Down Expand Up @@ -78,6 +77,15 @@ export interface MessageEncodingContext
): void;
}

export interface HasNodeId {
nodeId: number;
}

/** Tests if the given message is for a node or references a node */
export function hasNodeId<T extends Message>(msg: T): msg is T & HasNodeId {
return typeof (msg as any).nodeId === "number";
}

/** Returns the number of bytes the first message in the buffer occupies */
function getMessageLength(data: Buffer): number {
const remainingLength = data[1];
Expand Down Expand Up @@ -393,7 +401,7 @@ export class Message {

/** Finds the ID of the target or source node in a message, if it contains that information */
public getNodeId(): number | undefined {
if (isNodeQuery(this)) return this.nodeId;
if (hasNodeId(this)) return this.nodeId;
// Override this in subclasses if a different behavior is desired
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
encodeNodeID,
} from "@zwave-js/core";
import type {
INodeQuery,
MessageEncodingContext,
MessageParsingContext,
MessageRaw,
Expand Down Expand Up @@ -48,9 +47,7 @@ export interface AssignReturnRouteRequestOptions {

@expectedResponse(FunctionType.AssignReturnRoute)
@expectedCallback(FunctionType.AssignReturnRoute)
export class AssignReturnRouteRequest extends AssignReturnRouteRequestBase
implements INodeQuery
{
export class AssignReturnRouteRequest extends AssignReturnRouteRequestBase {
public constructor(
options: AssignReturnRouteRequestOptions & MessageBaseOptions,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from "@zwave-js/core";
import {
FunctionType,
type INodeQuery,
Message,
type MessageBaseOptions,
type MessageEncodingContext,
Expand Down Expand Up @@ -55,8 +54,8 @@ function testAssignSUCReturnRouteCallback(

@expectedResponse(FunctionType.AssignSUCReturnRoute)
@expectedCallback(testAssignSUCReturnRouteCallback)
export class AssignSUCReturnRouteRequest extends AssignSUCReturnRouteRequestBase
implements INodeQuery
export class AssignSUCReturnRouteRequest
extends AssignSUCReturnRouteRequestBase
{
public constructor(
options: AssignSUCReturnRouteRequestOptions & MessageBaseOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
encodeNodeID,
} from "@zwave-js/core";
import type {
INodeQuery,
MessageEncodingContext,
MessageParsingContext,
MessageRaw,
Expand Down Expand Up @@ -47,9 +46,7 @@ export interface DeleteReturnRouteRequestOptions {

@expectedResponse(FunctionType.DeleteReturnRoute)
@expectedCallback(FunctionType.DeleteReturnRoute)
export class DeleteReturnRouteRequest extends DeleteReturnRouteRequestBase
implements INodeQuery
{
export class DeleteReturnRouteRequest extends DeleteReturnRouteRequestBase {
public constructor(
options: DeleteReturnRouteRequestOptions & MessageBaseOptions,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
encodeNodeID,
} from "@zwave-js/core";
import type {
INodeQuery,
MessageEncodingContext,
MessageParsingContext,
MessageRaw,
Expand Down Expand Up @@ -57,8 +56,8 @@ function testDeleteSUCReturnRouteCallback(

@expectedResponse(FunctionType.DeleteSUCReturnRoute)
@expectedCallback(testDeleteSUCReturnRouteCallback)
export class DeleteSUCReturnRouteRequest extends DeleteSUCReturnRouteRequestBase
implements INodeQuery
export class DeleteSUCReturnRouteRequest
extends DeleteSUCReturnRouteRequestBase
{
public constructor(
options: DeleteSUCReturnRouteRequestOptions & MessageBaseOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from "@zwave-js/core";
import {
FunctionType,
type INodeQuery,
Message,
type MessageBaseOptions,
type MessageEncodingContext,
Expand Down Expand Up @@ -88,7 +87,7 @@ function testCallbackForRequestNodeInfoRequest(
@expectedResponse(RequestNodeInfoResponse)
@expectedCallback(testCallbackForRequestNodeInfoRequest)
@priority(MessagePriority.NodeQuery)
export class RequestNodeInfoRequest extends Message implements INodeQuery {
export class RequestNodeInfoRequest extends Message {
public constructor(
options: RequestNodeInfoRequestOptions & MessageBaseOptions,
) {
Expand Down
14 changes: 7 additions & 7 deletions packages/zwave-js/src/lib/driver/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ import {
type BootloaderChunk,
BootloaderChunkType,
FunctionType,
type INodeQuery,
type HasNodeId,
Message,
type MessageEncodingContext,
MessageHeaders,
Expand All @@ -135,7 +135,7 @@ import {
type ZWaveSerialPortImplementation,
ZWaveSocket,
getDefaultPriority,
isNodeQuery,
hasNodeId,
isSuccessIndicator,
isZWaveSerialPortImplementation,
} from "@zwave-js/serial";
Expand Down Expand Up @@ -4046,7 +4046,7 @@ export class Driver extends TypedEventEmitter<DriverEventCallbacks>
*/
public handleMissingNodeACK(
transaction: Transaction & {
message: INodeQuery;
message: HasNodeId;
},
error: ZWaveError,
): boolean {
Expand Down Expand Up @@ -5023,7 +5023,7 @@ ${handlers.length} left`,
private async handleRequest(msg: Message): Promise<void> {
let handlers: RequestHandlerEntry[] | undefined;

if (isNodeQuery(msg) || containsCC(msg)) {
if (hasNodeId(msg) || containsCC(msg)) {
const node = this.tryGetNode(msg);
if (node) {
// We have received an unsolicited message from a dead node, bring it back to life
Expand Down Expand Up @@ -5465,7 +5465,7 @@ ${handlers.length} left`,
// Update statistics
const node = this.tryGetNode(msg);
let success = true;
if (isSendData(msg) || isNodeQuery(msg)) {
if (isSendData(msg) || hasNodeId(msg)) {
// This shouldn't happen, but just in case
if (!node) return;

Expand Down Expand Up @@ -5984,7 +5984,7 @@ ${handlers.length} left`,
this.ensureReady();

let node: ZWaveNode | undefined;
if (isNodeQuery(msg) || containsCC(msg)) {
if (hasNodeId(msg) || containsCC(msg)) {
node = this.tryGetNode(msg);
}

Expand Down Expand Up @@ -6111,7 +6111,7 @@ ${handlers.length} left`,
} else {
// For other messages to the node, just check for successful completion. If the callback is not OK,
// we might not be able to communicate with the node. Sending another message is not a good idea.
maybeSendToSleep = isNodeQuery(msg)
maybeSendToSleep = hasNodeId(msg)
&& result
&& isSuccessIndicator(result)
&& result.isOK();
Expand Down

0 comments on commit 943a65c

Please sign in to comment.