Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(seqence): waiting timeout seconds after last message #20

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions src/content/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { status, Status, raceWithStatus, waitForStatus } from './status';
import { tabDataStore } from '../storage';
import { StatusError, TimeoutError, StoppedError } from './errors';
import { transforms } from '../transforms';
import { defaultConfig } from '../consts';

const clickStart = () => {
const start = elements.startButton()!;
Expand Down Expand Up @@ -73,15 +74,39 @@ const checkStopTime = async () => {
if (stopAfterEnabled && mustStop) throw new StoppedError();
};

const messagePipeline = raceWithStatus(
pipeline<string, void>(transforms.transformAllBlocks, sendMessage, sleep('messageTimeoutSecs', 1_000)),
({ detail: status }) => status !== Status.Connected
);
const ifElse =
(
predicate: () => boolean | Promise<boolean>,
ifHandler: (...args: any[]) => void | Promise<any>,
elseHandler?: (...args: any[]) => void | Promise<any>
) =>
async (...args: any[]) => {
if (await predicate()) return ifHandler(...args);
return elseHandler?.(...args);
};

const sendMessages = async () => {
for (const message of messages.latest!) {
// todo: Don't wait messageTimeoutSecs after the last message
await messagePipeline(message.content);
const messageList = messages.latest!;
let i = 0;

const messagePipeline = raceWithStatus(
pipeline<string, void>(
transforms.transformAllBlocks,
sendMessage,
ifElse(
() => i < messageList.length - 1,
sleep('messageTimeoutSecs', 1_000),
// Don't wait messageTimeoutSecs after the last message
// Wait one second instead
() => wait(defaultConfig.messageTimeoutSecs)
)
),
({ detail: status }) => status !== Status.Connected
);

while (i < messageList.length) {
await messagePipeline(messageList[i].content);
i++;
}
};

Expand Down Expand Up @@ -113,17 +138,6 @@ const bypassShowFaceMessage = async () => {
clearInterval(interval);
};

const ifElse =
(
predicate: () => boolean | Promise<boolean>,
ifHandler: (...args: any[]) => void | Promise<any>,
elseHandler: (...args: any[]) => void | Promise<any>
) =>
async (...args: any[]) => {
if (await predicate()) return ifHandler(...args);
return elseHandler(...args);
};

/**
* Runs the message sequence once.
*
Expand Down