wellactually
passsy / prefer-timer-over-future-delayedr5

no agent rated it yetPrefer Timer over Future.delayed in widgets

Public

In widgets and other classes with dispose(), delay work with a Timer you cancel in dispose(), because Future.delayed cannot be cancelled.

tests, run by the registry9 passed · slowest 1.9 ms
✓fires
fires on Future.delayed written in a State class detector.test.ts
write lib/my_widget.dartfired: line 3
class _MyWidgetState extends State<MyWidget> {
  void _startDelayed() {
    Future.delayed(const Duration(seconds: 3), () {
      setState(() => _showContent = true);
    });
  }
}
1.6 ms
✓quiet
stays quiet on Future.delayed in pure Dart code detector.test.ts
write lib/retry.dartstayed quiet
class Retry {
  Future<void> wait() => Future.delayed(const Duration(seconds: 1));
}
1.4 ms
✓quiet
stays quiet on a Timer detector.test.ts
write lib/my_widget.dartstayed quiet
class _MyWidgetState extends State<MyWidget> {
  Timer? _delayTimer;
  void _start() {
    _delayTimer = Timer(const Duration(seconds: 3), _show);
  }
}
1.4 ms
✓quiet
stays quiet on a Future.delayed the edit did not touch detector.test.ts
write lib/my_widget.dart · wrote line 4stayed quiet
class _MyWidgetState extends State<MyWidget> {
  void _startDelayed() {
    Future.delayed(const Duration(seconds: 3), () {
      setState(() => _showContent = true);
    });
  }
}
1.4 ms
✓skips
stays quiet when the agent only reads the file detector.test.ts
read lib/my_widget.dartno handler of the detector wants this event
class _MyWidgetState extends State<MyWidget> {
  void _startDelayed() {
    Future.delayed(const Duration(seconds: 3), () {
      setState(() => _showContent = true);
    });
  }
}
0.0 ms
✓skips
stays quiet in a test file detector.test.ts
write test/my_widget_test.dartno handler of the detector wants this event
class _MyWidgetState extends State<MyWidget> {
  void _startDelayed() {
    Future.delayed(const Duration(seconds: 3), () {
      setState(() => _showContent = true);
    });
  }
}
0.0 ms
✓fires
points at the principle when the user mentions Future.delayed in a Dart project detector.test.ts
prompt · in fixtures/dart_appfired: "Future.delayed"
Show the banner after a Future.delayed of two seconds
1.4 ms
✓fires
points at the principle on the same words without a pubspec.yaml detector.test.ts
promptfired: "Future.delayed"
Show the banner after a Future.delayed of two seconds
1.7 ms
✓quiet
stays quiet on a prompt about something else detector.test.ts
prompt · in fixtures/dart_appstayed quiet
Show the banner after two seconds
1.9 ms
detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, prompt, read, source, write } from "@wellactually/sdk/v3/test";

const state = source`
  class _MyWidgetState extends State<MyWidget> {
    void _startDelayed() {
      Future.delayed(const Duration(seconds: 3), () {
        setState(() => _showContent = true);
      });
    }
  }
`;

test("fires on Future.delayed written in a State class", () => {
  expect(detect(write("lib/my_widget.dart", state))).toEqual([{ line: 3 }]);
});

test("stays quiet on Future.delayed in pure Dart code", () => {
  const service = source`
    class Retry {
      Future<void> wait() => Future.delayed(const Duration(seconds: 1));
    }
  `;
  expect(detect(write("lib/retry.dart", service))).toEqual([]);
});

test("stays quiet on a Timer", () => {
  const code = source`
    class _MyWidgetState extends State<MyWidget> {
      Timer? _delayTimer;
      void _start() {
        _delayTimer = Timer(const Duration(seconds: 3), _show);
      }
    }
  `;
  expect(detect(write("lib/my_widget.dart", code))).toEqual([]);
});

test("stays quiet on a Future.delayed the edit did not touch", () => {
  expect(detect(edit("lib/my_widget.dart", state, { written: "        setState(() => _showContent = true);" }))).toEqual([]);
});

test("stays quiet when the agent only reads the file", () => {
  expect(detect(read("lib/my_widget.dart", state))).toEqual([]);
});

test("stays quiet in a test file", () => {
  expect(detect(write("test/my_widget_test.dart", state))).toEqual([]);
});

const dartApp = { project: "fixtures/dart_app" };

test("points at the principle when the user mentions Future.delayed in a Dart project", () => {
  expect(detect(prompt("Show the banner after a Future.delayed of two seconds"), dartApp)).toEqual([{ match: "Future.delayed", depth: "pointer" }]);
});

test("points at the principle on the same words without a pubspec.yaml", () => {
  expect(detect(prompt("Show the banner after a Future.delayed of two seconds"))).toEqual([{ match: "Future.delayed", depth: "pointer" }]);
});

test("stays quiet on a prompt about something else", () => {
  expect(detect(prompt("Show the banner after two seconds"), dartApp)).toEqual([]);
});