Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Drop Node.js < 14 support #115

Merged
merged 2 commits into from
Jan 3, 2023
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
21 changes: 0 additions & 21 deletions .autod.conf.js

This file was deleted.

16 changes: 0 additions & 16 deletions .editorconfig

This file was deleted.

2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "egg"
"extends": "eslint-config-egg"
}
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: CI

on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

workflow_dispatch: {}

jobs:
Job:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
with:
os: 'ubuntu-latest'
version: '14, 16, 18'
17 changes: 17 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Release

on:
push:
branches: [ master ]

workflow_dispatch: {}

jobs:
release:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
with:
checkTest: false
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

23 changes: 9 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
# ready-callback

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![CI](https://github.com/node-modules/ready-callback/actions/workflows/ci.yml/badge.svg)](https://github.com/node-modules/ready-callback/actions/workflows/ci.yml)
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/ready-callback.svg?style=flat-square
[npm-url]: https://npmjs.org/package/ready-callback
[travis-image]: https://img.shields.io/travis/node-modules/ready-callback.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/ready-callback
[codecov-image]: https://codecov.io/github/node-modules/ready-callback/coverage.svg?branch=master
[codecov-url]: https://codecov.io/github/node-modules/ready-callback?branch=master
[david-image]: https://img.shields.io/david/node-modules/ready-callback.svg?style=flat-square
[david-url]: https://david-dm.org/node-modules/ready-callback
[download-image]: https://img.shields.io/npm/dm/ready-callback.svg?style=flat-square
[download-url]: https://npmjs.org/package/ready-callback

Expand All @@ -23,15 +18,15 @@ Launch server after all async task ready

## Install

```
```bash
$ npm install ready-callback
```

## Usage

**Note: ready-callback is using `class`, so you should use node>=2**

```
```js
var koa = require('koa');
var ready = require('ready-callback')();
var app = koa();
Expand All @@ -51,7 +46,7 @@ app.ready(function() {

If task is called with error, `error` event will be emit, `ready` will never be called.

```
```js
// register a service that will emit error
var done = app.readyCallback('service');
serviceLaunch(function(err) {
Expand All @@ -68,7 +63,7 @@ app.on('error', function(err) {

If you set a task weak dependency, task will be done without emit `error`.

```
```js
var done = app.readyCallback('service', {isWeakDep: true});
serviceLaunch(function(err) {
done(err);
Expand All @@ -86,15 +81,15 @@ app.on('error', function(err) {

You can also set for all ready-callback

```
```js
var ready = require('ready-callback')({isWeakDep: true});
```

### Ready Status

You can get status every callback end.

```
```js
app.on('ready_stat', function(data) {
console.log(data.id); // id of the ended task
console.log(data.remain); // tasks waiting to be ended
Expand All @@ -105,7 +100,7 @@ app.on('ready_stat', function(data) {

You can set timeout when a task run a long time.

```
```js
var ready = require('ready-callback')({timeout: 1000});
ready.mixin(app);
app.on('ready_timeout', function(id) {
Expand All @@ -121,7 +116,7 @@ serviceLaunch(function() {

You can also set timeout for every task

```
```js
ready.mixin(app);
app.on('ready_timeout', function(id) {
// this will be called after 1s that `service` task don't complete
Expand Down
16 changes: 0 additions & 16 deletions appveyor.yml

This file was deleted.

16 changes: 7 additions & 9 deletions lib/ready.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const debug = require('util').debuglog('ready-callback');
const EventEmitter = require('events');
const once = require('once');
const ready = require('get-ready');
const uuid = require('uuid');
const debug = require('debug')('ready-callback');
const { randomUUID } = require('crypto');

const defaults = {
timeout: 10000,
Expand All @@ -17,7 +15,7 @@ const defaults = {
class Ready extends EventEmitter {

/**
* @constructor
* @class
* @param {Object} opt
* - {Number} [timeout=10000] - emit `ready_timeout` when it doesn't finish but reach the timeout
* - {Boolean} [isWeakDep=false] - whether it's a weak dependency
Expand Down Expand Up @@ -48,7 +46,7 @@ class Ready extends EventEmitter {

/**
* Mix `ready` and `readyCallback` to `obj`
* @method Ready#mixin
* @function Ready#mixin
* @param {Object} obj - The mixed object
* @return {Ready} this
*/
Expand Down Expand Up @@ -76,14 +74,14 @@ class Ready extends EventEmitter {

/**
* Create a callback, ready won't be fired until all the callbacks are triggered.
* @method Ready#readyCallback
* @function Ready#readyCallback
* @param {String} name -
* @param {Object} opt - the options that will override global
* @return {Function} - a callback
*/
readyCallback(name, opt) {
opt = Object.assign({}, defaults, this.opt, opt);
const cacheKey = uuid.v1();
const cacheKey = randomUUID();
opt.name = name || cacheKey;
const timer = setTimeout(() => this.emit('ready_timeout', opt.name), opt.timeout);
const cb = once(err => {
Expand All @@ -104,7 +102,7 @@ class Ready extends EventEmitter {

/**
* resolve ths callback when readyCallback be called
* @method Ready#readyDone
* @function Ready#readyDone
* @private
* @param {String} id - unique id generated by readyCallback
* @param {Object} opt - the options that will override global
Expand Down
22 changes: 7 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@
"lib"
],
"dependencies": {
"debug": "^2.6.0",
"get-ready": "^2.0.0",
"once": "^1.4.0",
"uuid": "^3.0.1"
"debug": "^4.3.4",
"get-ready": "^2.0.1",
"once": "^1.4.0"
},
"devDependencies": {
"autod": "^2.7.1",
"egg-bin": "^1.11.1",
"egg-ci": "^1.1.0",
"eslint": "^3.15.0",
"eslint-config-egg": "^3.2.0",
"egg-bin": "^5.9.0",
"eslint": "^8.31.0",
"eslint-config-egg": "^12.1.0",
"koa": "^1.2.4",
"mz-modules": "^2.1.0",
"spy": "^1.0.0"
},
"repository": {
Expand All @@ -36,16 +32,12 @@
"author": "popomore <[email protected]>",
"license": "MIT",
"scripts": {
"autod": "autod",
"lint": "eslint .",
"test": "npm run lint -- --fix && egg-bin test",
"cov": "egg-bin cov",
"ci": "npm run lint && egg-bin cov"
},
"engines": {
"node": ">=4.0.0"
},
"ci": {
"version": "4, 6, 7"
"node": ">=14.0.0"
}
}
2 changes: 0 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const assert = require('assert');
const koa = require('koa');
const spy = require('spy');
Expand Down
9 changes: 6 additions & 3 deletions test/ready.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict';

const assert = require('assert');
const EventEmitter = require('events');
const spy = require('spy');
const Ready = require('..').Ready;
const sleep = require('mz-modules/sleep');

function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}

describe('Ready', function() {

Expand Down