Skip to content

Commit

Permalink
API thightening, small cleanups and documentation improvements (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfccp authored Oct 25, 2023
2 parents 2071bd1 + 84a7d98 commit 0c44383
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 93 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/check-dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,3 @@ jobs:
"version for https://hub.docker.com/r/_/dart" >&2
exit 1
fi
# vim: set et sw=2 sts=2 :
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,3 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
directory: coverage/lcov.info
flags: unit,dart${{ matrix.dart_version }}

# vim: set et sw=2 sts=2 :
2 changes: 0 additions & 2 deletions .github/workflows/pub-score.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,3 @@ jobs:
echo "Al least $min_score is expected, but we got $score :("
exit 1
fi
# vim: set et sw=2 sts=2 :
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ doc/api/

# Coverage reports
coverage/
/doc/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.0.0

- **BREAKING:** `PausableTimer` is now `final`
- Minor cleanups and fixes in documentations and tests

## 2.0.0+1

- Update and test against Dart 3.1.
Expand Down
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ BSD 3-Clause License

Copyright (c) 2020, Google LLC.
Copyright (c) 2020, Leandro Lucarella.
Copyright (c) 2023, Mateus Felipe Cordeiro Caetano Pinto.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
16 changes: 3 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@
[![Latest Dart version](https://github.com/llucax/pausable_timer/actions/workflows/check-dart.yml/badge.svg)](https://github.com/llucax/pausable_timer/actions/workflows/check-dart.yml)
[![Coverage](https://codecov.io/gh/llucax/pausable_timer/branch/main/graph/badge.svg)](https://codecov.io/gh/llucax/pausable_timer)
[![pub package](https://img.shields.io/pub/v/pausable_timer.svg)](https://pub.dev/packages/pausable_timer)
[![pub points](https://badges.bar/pausable_timer/pub%20points)](https://pub.dev/packages/pausable_timer/score)
[![popularity](https://badges.bar/pausable_timer/popularity)](https://pub.dev/packages/pausable_timer/score)
[![likes](https://badges.bar/pausable_timer/likes)](https://pub.dev/packages/pausable_timer/score)
[![pub points](https://img.shields.io/pub/points/pausable_timer)](https://pub.dev/packages/pausable_timer/score)
[![popularity](https://img.shields.io/pub/popularity/pausable_timer)](https://pub.dev/packages/pausable_timer/score)
[![likes](https://img.shields.io/pub/likes/pausable_timer)](https://pub.dev/packages/pausable_timer/score)

A [Dart](https://dart.dev/)
[timer](https://api.dart.dev/stable/dart-async/Timer/Timer.html) that can be
paused, resumed and reset.

## New maintainer wanted

It's been almost 2 years since the last time I used Dart or Flutter, and while
I enjoy maintaining open source projects, it is becoming more and more
difficult to keep up to date with changes in Dart and maintaining this project,
so **I'm looking for a maintainer willing to take over this project** to ensure
its health in the future. Please [get in
touch](https://github.com/llucax/pausable_timer/discussions/55) if you are
interested.

## Example using `start()`, `pause()` and `reset()`

```dart
Expand Down
4 changes: 4 additions & 0 deletions dartdoc_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dartdoc:
linkToSource:
root: .
uriTemplate: https://github.com/llucax/pausable_timer/tree/main/%f%#L%l%
3 changes: 1 addition & 2 deletions example/countdown.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Example on how to implement countdown making a PausableTimer periodic.
// Example on how to implement countdown making a PausableTimer periodic.
import 'package:pausable_timer/pausable_timer.dart';

void main() async {
Expand Down
25 changes: 14 additions & 11 deletions lib/pausable_timer.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Copyright 2020, Google LLC.
// Copyright 2020, Leandro Lucarella.
// Copyright 2023, Mateus Felipe Cordeiro Caetano Pinto.
// SPDX-License-Identifier: BSD-3-Clause
import 'dart:async' show Timer, Zone;

import 'package:clock/clock.dart' show clock;

/// A [Timer] that can be paused, resumed and reset.
///
/// This implementation is roughly based on:
/// https://github.com/dart-lang/sdk/issues/43329#issuecomment-687024252
class PausableTimer implements Timer {
/// This implementation is roughly based on
/// [this comment](https://github.com/dart-lang/sdk/issues/43329#issuecomment-687024252).
final class PausableTimer implements Timer {
/// The [Zone] where the [_callback] will be run.
///
/// Dart generally calls asynchronous callbacks in the zone where they were
Expand All @@ -20,7 +21,7 @@ class PausableTimer implements Timer {
/// callback is created (say, remember the stack trace, which is what the
/// stack_trace package does).
///
/// That is, we call [Zone.registeCallback] to enable zones to know about the
/// That is, we call [Zone.registerCallback] to enable zones to know about the
/// callback creation as well as the later [Zone.run] for running it.
///
/// If you just store the callback, but don't register it at the time it's
Expand Down Expand Up @@ -60,12 +61,15 @@ class PausableTimer implements Timer {
/// should make sure the timer wasn't cancelled before calling this function.
void _startTimer() {
assert(_stopwatch != null);
_timer = _zone.createTimer(_originalDuration - _stopwatch!.elapsed, () {
_tick++;
_timer = null;
_stopwatch = null;
_zone.run(_callback!);
});
_timer = _zone.createTimer(
_originalDuration - _stopwatch!.elapsed,
() {
_tick++;
_timer = null;
_stopwatch = null;
_zone.run(_callback!);
},
);
_stopwatch!.start();
}

Expand All @@ -85,7 +89,6 @@ class PausableTimer implements Timer {
_originalDuration = duration,
_zone = Zone.current {
_callback = _zone.bindCallback(callback);
assert(_callback != null);
}

/// The original duration this [Timer] was created with.
Expand Down
9 changes: 5 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ description: A timer implementation that can be paused, resumed and reset.
homepage: https://github.com/llucax/pausable_timer
repository: https://github.com/llucax/pausable_timer
funding:
- https://github.com/sponsors/mateusfccp
- https://github.com/llucax/llucax/blob/main/sponsoring-platforms.md

version: 2.0.0+1
version: 3.0.0

environment:
sdk: ">=3.0.0 <4.0.0"
Expand All @@ -14,7 +15,7 @@ dependencies:
clock: ^1.1.0

dev_dependencies:
coverage: ^1.6.3
test: ^1.24.3
coverage: ^1.7.1
fake_async: ^1.3.1
lints: ^2.1.0
lints: ^3.0.0
test: ^1.24.8
Loading

0 comments on commit 0c44383

Please sign in to comment.