Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
47 changes: 47 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = {
root: true,
env: {
browser: true,
es2021: true
},
extends: [
'airbnb-base',
'eslint:recommended'
],
plugins: [
'import'
],
overrides: [
{
env: {
node: true
},
files: [
'.eslintrc.{js,cjs}'
],
parserOptions: {
sourceType: 'script'
}
}
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
rules: {
indent: ['error', 2],
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single'],
semi: ['error', 'always'],
'comma-dangle': ['error', {
arrays: 'never',
objects: 'never',
imports: 'never',
exports: 'never',
functions: 'never'
}],
'no-restricted-globals': ['off'],
'import/prefer-default-export': ['off']
},
ignorePatterns: ['examples/', 'dist/', 'node_modules/', '**/*.d.ts']
};
32 changes: 32 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Deploy package on NPM
on:
workflow_dispatch:
inputs:
isConfirmed:
description: "Which version do you want to deploy? (ex. 1.0.0)"
required: true
type: boolean
jobs:
deploy-package-on-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
cache: "yarn"
registry-url: "https://registry.npmjs.org"
- name: Validate Tag
if: ${{ (github.ref_type != 'tag' || inputs.IS_CONFIRMED == 'false') }}
run: |
exit 1
- name: Install dependencies
run: yarn
- name: Lint
run: yarn lint
- name: Build
run: yarn build
- name: Deploy
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
144 changes: 144 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

### Node Patch ###
# Serverless Webpack directories
.webpack/

# Optional stylelint cache

# SvelteKit build / generate output
.svelte-kit

# End of https://www.toptal.com/developers/gitignore/api/node
3 changes: 3 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

yarn lint
14 changes: 14 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

PROHIBITED_REF="refs/heads/master"

if read local_ref local_sha remote_ref remote_sha
then
if [ "$remote_ref" = "$PROHIBITED_REF" ]
then
echo "prevent to push master"
exit 1
fi
fi

exit 0
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.gitignore
.git/
node_modules/
npm-debug.log
21 changes: 21 additions & 0 deletions LISENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 univdev<pcydeveloper@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 57 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
![WW Timer](./banner.jpg)
# Introduce
The 'WW Timer' is a timer for browsers that operate using web workers.
# Why need
Implementing a timer in a browser is challenging for a variety of reasons.
- Accurate time calculation may be difficult depending on the congestion level of the event loop.
- Browser operating in the background does not have 'setInterval' or 'setTimeout' working properly.
# WW Timer
WW Timer is a highly accurate timer library for web browsers.

## Why It's Needed
Typically, when implementing timer functionality in web browsers, the `setTimeout` or `setInterval` methods are used. However, due to the nature of JavaScript's execution model, achieving precise time measurement can be challenging.

JavaScript's asynchronous functions are subject to delays depending on the number of tasks the web browser is executing. This can result in the `setTimeout` and `setInterval` functions being deprioritized in the event loop, which, while generally inconsequential for standard functionality, can introduce critical errors in timing accuracy for timer implementations.

## Solution
The solution lies in utilizing the [Web Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) supported by browsers. Since Web Workers operate independently of the browser's main thread, they are not affected by the complexity of the tasks being executed in the web app. This independence allows for more accurate time measurements, irrespective of the main thread's load.

## Features
- **Accuracy**: Provides highly accurate timing by circumventing the event loop's congestion.
- **Independence**: Operates separately from the main thread, ensuring timer precision is maintained.
- **Ease of Use**: Designed to be straightforward to implement within any web application.

## Installation
```sh
# npm
npm install ww-timer
# yarn
yarn add ww-timer
```

## Quick Start
```javascript
// Import WW Timer
import WWTimer from 'ww-timer';

// Create a new timer
const timer = new WWTimer();

// Start the timer with a callback function
timer.start(() => {
console.log('Timer tick');
});

// Pause the timer
timer.pause();

// Destroy the timer instance
timer.destroy();
```

## API Reference
- `start(callback, interval)`: Starts the timer with the specified callback function and interval.
- `pause()`: Stops the timer.
- `destroy()`: Destroy the timer instance.

## Compatibility
Compatible with most modern web browsers that support the Web Workers API.

## Contributing
Contributions to the WW Timer project are welcome. Please refer to the CONTRIBUTING.md file for more details.

## License
WW Timer is released under the MIT License. See the LICENSE file for more information.
Binary file added examples/.DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions examples/react/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}
24 changes: 24 additions & 0 deletions examples/react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
1 change: 1 addition & 0 deletions examples/react/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v18.18.0
Loading