run_once 1.0.0 copy "run_once: ^1.0.0" to clipboard
run_once: ^1.0.0 copied to clipboard

An extremely simple library for running 'things' exactly once. No matter how many times that 'thing' is called.

run_once #

An extremely simple library for running 'things' exactly once. No matter how many times that 'thing' is called.

How does it work ? #

Well it's very simple. We basically use the StackTrace.current.toString() and use hashCode to keep record on when a function has been called. This should work fine in most cases. It means that the call stack is basically used a unique identifier for when a function has been called.

Usage #

import 'package:run_once/run_once.dart';

void main() async {
  /// This will print 'called 1' exactly 1 time although
  /// we call this function 10 times.
  for (var i = 0; i < 10; i++) {
    runOnce(() {
      print('called 1');
    });
  }

  /// This will print 'called 2' exactly 2 times as the [runOnce] simply
  /// isn't called in the same spot.
  runOnce(() {
    print('called 2');
  });
  runOnce(() {
    print('called 2');
  });

  /// This will print 'called 3' exactly 3 times although
  /// we call this function 15 times.
  /// Note the `forDuration` is exactly a [Duration] of 500 milliseconds.
  /// Using `forDuration` means for a duration of 500 milliseconds [runOnce]
  /// can only run once.
  /// So we loop 15 times in this loop and wait 100 milliseconds in each
  /// loop cycle; thus 'called 3' is printed exactly 3 times.
  for (var i = 0; i < 15; i++) {
    await Future.delayed(Duration(milliseconds: 100));

    runOnce(() {
      print('called 3');
    }, forDuration: Duration(milliseconds: 500));
  }
}

Disclaimer #

This library isn't really tested very comprehensively at all. So it might not work in every use case.

1
likes
0
pub points
53%
popularity

Publisher

unverified uploader

An extremely simple library for running 'things' exactly once. No matter how many times that 'thing' is called.

Repository (GitLab)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on run_once