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

Local counter in loop as side effect #67

Closed
Morb0 opened this issue Dec 19, 2022 · 1 comment · Fixed by #122
Closed

Local counter in loop as side effect #67

Morb0 opened this issue Dec 19, 2022 · 1 comment · Fixed by #122

Comments

@Morb0
Copy link

Morb0 commented Dec 19, 2022

The code below doesn't work correctly, most likely it breaks the side effect system, but based on the documentation, this behavior is not obvious. I managed to fix it by wrapping counter in external:counter = await conv.external(counter - 1).

async function counter(conv: Conversation, ctx: Context) {
    await ctx.reply('Please send me a message');
    let counter = 0;
    while (true) {
      conv.log(`Loop begin: Counter: ${counter}`);
      const text = await conv.form.text();
      conv.log(`After wait: Counter: ${counter}`);

      const idx = conv.session.tags.indexOf(text);
      if (idx !== -1) {
        conv.log(`Remove "${text}" at ${idx} index`);
        conv.session.tags.splice(idx, 1);
        counter--;
      } else {
        conv.log(`Add "${text}"`);
        conv.session.tags.push(text);
        counter++;
      }
      await ctx.reply(`Counter: ${counter}`);

      if (counter === 3) {
        await ctx.reply('Done');
        return;
      }

      conv.log(`Loop end: Counter: ${counter}`);
    }
  }

Logs:

Loop begin: Counter: 0
After wait: Counter: 0
Add "1"
Loop end: Counter: 1


Loop begin: Counter: 1
After wait: Counter: 1
Remove "1" at 0 index
Loop end: Counter: 0


Loop begin: Counter: 0
After wait: Counter: -2 <--- Unexpected after wait
Add "1"
Loop end: Counter: -1


Loop begin: Counter: -1
[Stuck, no more interactions work]
@KnorpelSenf
Copy link
Member

This seems to be a bug, most likely with conversation.session. This needs to be investigated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: To do
Development

Successfully merging a pull request may close this issue.

2 participants