Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
0.20.0
Browse files Browse the repository at this point in the history
- one class
- overloading for repeat-method
- biome and bun
  • Loading branch information
oscarpalmer committed Jan 1, 2024
1 parent 5ee0167 commit 952bd2b
Show file tree
Hide file tree
Showing 14 changed files with 381 additions and 6,733 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm i
- run: npm test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules
www

*.tsbuildinfo

.DS_Store
61 changes: 28 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ A better solution for timeout- and interval-based timers.

Timer is available on _npm_ as [`@oscarpalmer/timer`](https://www.npmjs.com/package/@oscarpalmer/timer) to be bundled with your awesome projects.

If you don't need to bundle things, you can use the CDN-version and include a script in your proejcts right away, thanks to [jsDelivr](https://www.jsdelivr.com/package/npm/@oscarpalmer/timer) and [UNPKG](https://unpkg.com/@oscarpalmer/timer).

## Getting started

This is fairly lightweight package, so hopefully you'll be up and running in seconds :blush:
Expand All @@ -21,65 +19,62 @@ The timers can be called with nice helper methods, which also auto-starts the ti
```typescript
import {repeat, wait} from '@oscarpalmer/timer';

let waited = wait(callback, time);
let repeated = repeat(callback, time, count);
const waited = wait(waitedCallback);
const repeated = repeat(repeatedCallback, 10);
```

Or they can be created using class syntax, but without being auto-started:
Or they can be created using the `new`-keyword, but without being auto-started:

```typescript
import {Repeated, Waited} from '@oscarpalmer/timer';
import {Timer} from '@oscarpalmer/timer';

waited = new Waited(callback, time);
repeated = new Repeated(callback, time, count);
const waited = new Timer(waitedCallback);
const repeated = new Timer(repeatedCallback, 10);
```

### CDN & IIFE

If you're using Timer by including the file suffixed with `.iife.js` – or one of the CDN-versions mentioned above – you won't have to import any of the classes or methods.
## Parameters

Instead, just include a `script`-tag in your HTML linking to Timer and you can access Timer in other scripts, as below:
When creating a _Timer_, either with the new `new`-keyword or using the functions, you can configure the timer with a few parameters:

```javascript
// With auto-start
var waited = Timer.wait(callback, time);
var repeated = Timer.repeat(callback, time, count);

// With manual start
waited = new Timer.Waited(callback, time);
repeated = new Timer.Repeated(callback, time, count);
```
|Parameter|Description|
|--------:|:----------|
|`callback`|Callback function to be invoked for each run that are __required__ for all timers.<br>For more information on callbacks, please read [the callbacks section](#callbacks).|
|`count`|How many times the timer should run.<br>If no value is provided, it will default to `1` when using the `new`-keyword and the `wait`-method, but throws an error for the `repeat`-method.|
|`time`|How many milliseconds between each invokations of the provided callback.<br>Defaults to `0`, which is not really _0_ milliseconds, but close enough :wink:|
|`after`|A callback to run after the timer finishes, both when cancelled and completed.<br>If _count_ is greater than `1` and _after_ __is not__ `undefined`, a function is expected.|

## Methods
## Methods and properties

Both the nice helper methods and the class syntax create similar objects – `Waited` and `Repeated` – which share methods:
An instance of _Timer_ also has a few helpful methods and properties:

|Method|Description|
|-----:|:----------|
|`start()`|Starts the timer: necessary when creating a timer using the class syntax _(e.g. `new Waited...`)_, but helpful when the timer needs to be started at other times, as well|
|`stop()`|Stops the timer|
|`restart()`|Restarts the timer|
|Name|Type|Description|
|---:|----|:----------|
|`active`|_Property_|A `boolean` value to check if the timer is running|
|`finished`|_Property_|A `boolean` value to check if the timer was able to finish|
|`start()`|_Method_|Starts the timer.<br>Necessary when creating a timer using the class syntax _(e.g. `new Waited...`)_, but helpful when the timer needs to be started at other times, as well.|
|`stop()`|_Method_|Stops the timer|
|`restart()`|_Method_|Restarts the timer|

## Callbacks

Callbacks for waited timers do not receive any arguments, but callbacks for repeated ones do:
Callbacks for both waited and repeated timers receive one parameter:

```typescript
repeat(index => {
function callback(index) {
// 'index' is the current step
// starts at 0, goes up to a maximum of count - 1
// for this example: 0 → 9
}, 0, 10);
};
```

When you create a repeated timer, you can also provide a fourth parameter to act as a callback to run when the timer stops, as below:
When you create a repeated timer, you can also provide a callback to run when the timer stops, as below:

```typescript
function after(finished: boolean) {
// Let's do something fun!
}

repeat(() => {}, 0, 10, after);
repeat(() => {}, 10, after);
```

The `finished`-parameter for the `after`-function can be used to determine if the timer was stopped manually, or if it was able to finish its work.
Expand Down
22 changes: 22 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "https://biomejs.dev/schemas/1.4.1/schema.json",
"files": {
"ignore": ["./dist/**/*"]
},
"javascript": {
"formatter": {
"arrowParentheses": "asNeeded",
"bracketSpacing": false,
"quoteStyle": "single"
}
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"organizeImports": {
"enabled": true
}
}
Binary file added bun.lockb
Binary file not shown.
162 changes: 0 additions & 162 deletions dist/timer.iife.js

This file was deleted.

0 comments on commit 952bd2b

Please sign in to comment.