pendulum 0.1.3

Pendulum #

A library for task-scheduling in Dart. Exports a Task class that returns Streams, with a number of versatile options to customize how Tasks are run.

Tasks take a Function (and a corresponding List argument) and run them asynchronously on a number of triggers. All methods return Streams, because of their versatility: Streams can be listened, canceled, or converted to other types.

Methods take inspiration from Quartz Scheduler.

Usage #

A (very) simple usage example:

import 'package:pendulum/pendulum.dart';

void main() async {
  var task = Task<String>((List args) {
    return 'Simple process!';
  }, []);
  var task2 = Task<String>((List args) {
    return 'Delayed process!';
  }, []);
  var task3 = Task<String>((List args) {
    return 'Repeated process!';
  }, []);
  var watch = Stopwatch();

  // Simple process and listen.
  task.process().listen((var data) {
    print(data);
  });

  // Process after a delay of 5 seconds.
  watch.start();
  task2.processAfter(Duration(seconds: 5)).listen((var data) {
    print(data);
    watch.stop();
    print(watch.elapsedMilliseconds);
  });

  await Future.delayed(Duration(seconds: 10));

  // Repeatedly process every 5 seconds for 20 seconds.
  watch.reset();
  watch.start();
  task3
      .repeatIntervalFor(Duration(seconds: 20), Duration(seconds: 5))
      .listen((var data) {
        print(data);
        print(watch.elapsedMilliseconds);
      });
}

This is just the start of the features of Pendulum. To see all the methods and triggers, check out the API reference.

Simulated Tasks #

Testing Pendulum can be a pain if you're using Tasks with DateTime arguments, because you have to wait for the DateTime to be reached to test. My solution to this problem is SimulatedTask:

  • Simulated Task uses a provided DateTime instead of DateTime.now()
  • You can 'fast-forward' to any point in time that you want using setNow(DateTime arg)

This is obviously an imperfect solution, and I'm welcome to suggestions as to alternatives.

Partners in Crime #

While Pendulum works fine all on its own, there are some packages that can enhance its functionality. A few of them are listed below:

  • RxDart
    • Library for Stream extensions.
    • Can be used to enhance processor methods
  • Instant
    • Library for DateTime manipulation.
    • Many Pendulum triggers involve DateTimes
    • Can be used to adjust for timezones
  • Isolate
    • Library for isolate management.
    • While Pendulum runs async, one isolate might not be able to handle all the tasks.
    • Using the isolate load_balancer class to run tasks could thus save CPU.

Features and bugs #

Please file feature requests and bugs at the issue tracker.


This package and its contents are subject to the terms of the Mozilla Public License, v. 2.0. #
© 2019 Aditya Kishore #

0.1.3 (beta) #

  • Added Type arguments so that returned Streams can be type-identified
  • Changed example accordingly

0.1.2 (beta) #

  • Added SimulatedTask class for easier testing

0.1.1 (beta) #

  • Initial Version
  • Created Task Class, with processor methods
  • Added Stream-based API and dartdocs
  • Created example and minimal README

example/pendulum_example.dart

import 'package:pendulum/pendulum.dart';

void main() async {
  var task = Task<String>((List args) {
    return 'Simple process!';
  }, []);
  var task2 = Task<String>((List args) {
    return 'Delayed process!';
  }, []);
  var task3 = Task<String>((List args) {
    return 'Repeated process!';
  }, []);
  var watch = Stopwatch();

  // Simple process and listen.
  task.process().listen((var data) {
    print(data);
  });

  // Process after a delay of 5 seconds.
  watch.start();
  task2.processAfter(Duration(seconds: 5)).listen((var data) {
    print(data);
    watch.stop();
    print(watch.elapsedMilliseconds);
  });

  await Future.delayed(Duration(seconds: 10));

  // Repeatedly process every 5 seconds for 20 seconds.
  watch.reset();
  watch.start();
  task3
      .repeatIntervalFor(Duration(seconds: 20), Duration(seconds: 5))
      .listen((var data) {
        print(data);
        print(watch.elapsedMilliseconds);
      });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  pendulum: ^0.1.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:pendulum/pendulum.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
11
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
55
Learn more about scoring.

We analyzed this package on Feb 22, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Format lib/pendulum.dart.

Run dartfmt to format lib/pendulum.dart.

Format lib/src/Stacks/tasks.dart.

Run dartfmt to format lib/src/Stacks/tasks.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
path ^1.6.4 1.6.4
Dev dependencies
pedantic ^1.8.0
test ^1.6.0