Skip to content

Commit

Permalink
refactor: Move sendSplit to a mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Nov 8, 2023
1 parent 45f8455 commit 0d516ff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
34 changes: 0 additions & 34 deletions src/common/controllers/OutputController.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { NodeMessageInFlow } from 'node-red';

import { RED } from '../../globals';
import { debugToClient } from '../../helpers/node';
import HomeAssistant from '../../homeAssistant/HomeAssistant';
import {
BaseNode,
NodeDone,
NodeMessage,
NodeSend,
OutputProperty,
} from '../../types/nodes';
import { NodeEvent } from '../events/Events';
Expand Down Expand Up @@ -56,36 +52,6 @@ export default class OutputController<T extends BaseNode = BaseNode> {

protected onClose?(removed: boolean): void;

protected sendSplit(
message: Partial<NodeMessageInFlow>,
data: any[],
send: NodeSend
) {
if (!send) {
send = this.node.send;
}

delete message._msgid;
message.parts = {
id: RED.util.generateId(),
count: data.length,
index: 0,
// TODO: check if this works
// type: 'array',
// len: 1,
};

let pos = 0;
for (let i = 0; i < data.length; i++) {
message.payload = data.slice(pos, pos + 1)[0];
if (message.parts) {
message.parts.index = i;
}
pos += 1;
send(RED.util.cloneMessage(message));
}
}

protected debugToClient(message: any | any[]) {
debugToClient(this.node, message);
}
Expand Down
42 changes: 42 additions & 0 deletions src/common/controllers/SendSplitMixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NodeMessageInFlow } from 'node-red';

import { RED } from '../../globals';
import { GConstructor } from '../../types/mixins';
import { NodeSend } from '../../types/nodes';
import OutputController from './OutputController';

export default function sendSplitMixin<
TBase extends GConstructor<OutputController>
>(Base: TBase) {
return class SendSplitController extends Base {
protected sendSplit(
message: Partial<NodeMessageInFlow>,
data: any[],
send?: NodeSend
) {
if (!send) {
send = this.node.send;
}

delete message._msgid;
message.parts = {
id: RED.util.generateId(),
count: data.length,
index: 0,
// TODO: check if this works
// type: 'array',
// len: 1,
};

let pos = 0;
for (let i = 0; i < data.length; i++) {
message.payload = data.slice(pos, pos + 1)[0];
if (message.parts) {
message.parts.index = i;
}
pos += 1;
send(RED.util.cloneMessage(message));
}
}
};
}
9 changes: 5 additions & 4 deletions src/nodes/get-entities/GetEntitiesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import InputOutputController, {
InputOutputControllerOptions,
InputProperties,
} from '../../common/controllers/InputOutputController';
import SendSplitMixin from '../../common/controllers/SendSplitMixin';
import ComparatorService from '../../common/services/ComparatorService';
import HomeAssistant from '../../homeAssistant/HomeAssistant';
import { HassEntity } from '../../types/home-assistant';
Expand All @@ -21,10 +22,10 @@ interface GetEntitiesControllerConstructor
homeAssistant: HomeAssistant;
}

export default class GetEntitiesController extends InputOutputController<
GetEntitiesNode,
GetEntitiesNodeProperties
> {
const SendSplitController = SendSplitMixin(
InputOutputController<GetEntitiesNode, GetEntitiesNodeProperties>
);
export default class GetEntitiesController extends SendSplitController {
#comparatorService: ComparatorService;
#homeAssistant: HomeAssistant;

Expand Down

0 comments on commit 0d516ff

Please sign in to comment.