Skip to content

tutorialStore onDocumentChanged unexpected behaviour #433

Open
@so0k

Description

@so0k

Describe the bug

using the tutorialStore.onDocumentChanged triggers callback immediately because the nanostores subscribe callback seems to work that way.

For example:

    const terminal = tutorialStore.terminalConfig.value!.panels[1];
    const filePath = '/foo';
    tutorialStore.onDocumentChanged(filePath, (document) => {
       terminal.write(`${document.filePath} changed\n`);
    });

Expected comparison between old and new document to confirm changes happened

    const terminal = tutorialStore.terminalConfig.value!.panels[1];
    const filePath = '/foo';
    const stopListening = tutorialStore.documents.listen((newDocuments, oldDocuments) => {
      if (!newDocuments[filePath] || !oldDocuments || !oldDocuments[filePath]) {
        return;
      }
      const oldDocument = oldDocuments[filePath];
      const newDocument = newDocuments[filePath];
      if (oldDocument.value !== newDocument.value) {
        queueMicrotask(() => {
          terminal.write(`${newDocument.filePath} changed\n`);
          stopListening();
        });
      }
    });

Link to a StackBlitz project which shows the error

https://stackblitz.com/~/github.com/so0k/tutorialkit-terminal-writer

Steps to reproduce

  1. click "Test" button
  2. Notice terminal shows "Document changed" when it did not

Expected behavior

  1. click "Test" Button
  2. Modify foo (i.e. use TutorialKit API to update foo)
  3. only then, notice callback is triggered

Screenshots

No response

Platform

  • TutorialKit version: 1.3.1
  • OS: local Linux, Stackblitz
  • Browser: Chrome
  • Version: 131.0.6778.204 (Official Build) (64-bit)

Additional context

  1. Consider using listen if we don't want immediate callback?
  2. Compare newValue(s) to oldValue(s) before triggering?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpr-welcomeAccepting pull requests for this change

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions