Open
Description
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
- click "Test" button
- Notice terminal shows "Document changed" when it did not
Expected behavior
- click "Test" Button
- Modify foo (i.e. use TutorialKit API to update foo)
- 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
- Consider using
listen
if we don't want immediate callback? - Compare newValue(s) to oldValue(s) before triggering?