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

feat: refactor with typescript #117

Merged
merged 2 commits into from
Oct 11, 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
11 changes: 9 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"extends": "eslint-config-egg"
}
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
],
"rules": {
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-unused-vars": "off"
}
}
18 changes: 0 additions & 18 deletions .github/workflows/ci.yml

This file was deleted.

16 changes: 16 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI

on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

jobs:
Job:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest'
version: '16, 18, 20'
6 changes: 2 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ on:
push:
branches: [ master ]

workflow_dispatch: {}

jobs:
release:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
with:
checkTest: false
checkTest: false
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
coverage/
.tshy*
dist/
61 changes: 46 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@
"ready",
"async"
],
"main": "lib/ready.js",
"files": [
"index.js",
"lib"
"dist",
"src"
],
"dependencies": {
"debug": "^4.3.4",
"get-ready": "^2.0.1",
"get-ready": "^3.1.0",
"once": "^1.4.0"
},
"devDependencies": {
"egg-bin": "^5.9.0",
"eslint": "^8.31.0",
"eslint-config-egg": "^12.1.0",
"koa": "^1.2.4",
"spy": "^1.0.0"
"@eggjs/koa": "^2.15.1",
"@eggjs/tsconfig": "^1.3.3",
"@types/mocha": "^10.0.2",
"@types/node": "^20.8.3",
"@types/once": "^1.4.1",
"egg-bin": "^6.5.2",
"eslint": "^8.51.0",
"eslint-config-egg": "^12.3.1",
"git-contributor": "^2.1.5",
"mocha": "^10.2.0",
"tinyspy": "^2.2.0",
"ts-node": "^10.9.1",
"tshy": "^1.2.2",
"tshy-after": "^1.0.0",
"typescript": "^5.2.2"
},
"repository": {
"type": "git",
Expand All @@ -32,12 +40,35 @@
"author": "popomore <[email protected]>",
"license": "MIT",
"scripts": {
"lint": "eslint .",
"test": "npm run lint -- --fix && egg-bin test",
"cov": "egg-bin cov",
"ci": "npm run lint && egg-bin cov"
"lint": "eslint src test --ext ts",
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly",
"contributor": "git-contributor",
"prepublishOnly": "tshy && tshy-after"
},
"engines": {
"node": ">=14.0.0"
}
"node": ">=16.0.0"
},
"ci": {
"version": "16, 18, 20"
},
"tshy": {
"exports": {
".": "./src/index.ts"
}
},
"exports": {
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
},
"type": "module",
"types": "./dist/commonjs/index.d.ts"
}
93 changes: 47 additions & 46 deletions lib/ready.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
const debug = require('util').debuglog('ready-callback');
const EventEmitter = require('events');
const once = require('once');
const ready = require('get-ready');
const { randomUUID } = require('crypto');
import EventEmitter from 'node:events';
import { debuglog } from 'node:util';
import { randomUUID } from 'node:crypto';
import once from 'once';
import { Ready as ReadyObject, type ReadyFunctionArg } from 'get-ready';

const debug = debuglog('ready-callback');

interface ReadyOption {
timeout?: number;
isWeakDep?: boolean;
lazyStart?: boolean;
}
interface ReadyCallbackOption {
name?: string;
timeout?: number;
isWeakDep?: boolean;
}
interface ReadyCallbackFn {
(err?: any): void;
id: string;
}
type ReadyCallbackCache = Map<string, ReadyCallbackFn>;

const defaults = {
const defaults: ReadyCallbackOption = {
timeout: 10000,
isWeakDep: false,
};
Expand All @@ -13,21 +31,19 @@ const defaults = {
* @class Ready
*/
class Ready extends EventEmitter {
isError = false;
cache: ReadyCallbackCache = new Map();

/**
* @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
* - {Boolean} [lazyStart=false] - will not check cache size automatically, if lazyStart is true
*/
constructor(opt) {
opt: ReadyOption;
obj: any;

ready: (flagOrFunction?: ReadyFunctionArg) => void;

constructor(opt: ReadyOption = {}) {
super();
ready.mixin(this);
ReadyObject.mixin(this);

this.opt = opt || {};
this.isError = false;
this.cache = new Map();
this.opt = opt;

if (!this.opt.lazyStart) {
this.start();
Expand All @@ -50,7 +66,7 @@ class Ready extends EventEmitter {
* @param {Object} obj - The mixed object
* @return {Ready} this
*/
mixin(obj) {
mixin(obj?: any) {
// only mixin once
if (!obj || this.obj) return null;

Expand All @@ -72,19 +88,12 @@ class Ready extends EventEmitter {
return this;
}

/**
* Create a callback, ready won't be fired until all the callbacks are triggered.
* @function Ready#readyCallback
* @param {String} name -
* @param {Object} opt - the options that will override global
* @return {Function} - a callback
*/
readyCallback(name, opt) {
readyCallback(name: string, opt: ReadyCallbackOption = {}) {
opt = Object.assign({}, defaults, this.opt, opt);
const cacheKey = randomUUID();
opt.name = name || cacheKey;
const timer = setTimeout(() => this.emit('ready_timeout', opt.name), opt.timeout);
const cb = once(err => {
const cb = once((err?: any) => {
if (err != null && !(err instanceof Error)) {
err = new Error(err);
}
Expand All @@ -93,23 +102,14 @@ class Ready extends EventEmitter {
if (this.isError === true) return;
// fire callback after all register
setImmediate(() => this.readyDone(cacheKey, opt, err));
});
}) as unknown as ReadyCallbackFn;
debug('[%s] Register task id `%s` with %j', cacheKey, opt.name, opt);
cb.id = opt.name;
this.cache.set(cacheKey, cb);
return cb;
}

/**
* resolve ths callback when readyCallback be called
* @function Ready#readyDone
* @private
* @param {String} id - unique id generated by readyCallback
* @param {Object} opt - the options that will override global
* @param {Error} err - err passed by ready callback
* @return {Ready} this
*/
readyDone(id, opt, err) {
readyDone(id: string, opt: ReadyCallbackOption, err?: Error) {
if (err != null && !opt.isWeakDep) {
this.isError = true;
debug('[%s] Throw error task id `%s`, error %s', id, opt.name, err);
Expand All @@ -130,17 +130,18 @@ class Ready extends EventEmitter {
}
return this;
}

}

// Use ready-callback with options
module.exports = opt => new Ready(opt);
module.exports.Ready = Ready;

function getRemain(map) {
const names = [];
function getRemain(map: ReadyCallbackCache) {
const names: string[] = [];
for (const cb of map.values()) {
names.push(cb.id);
}
return names;
}

export { Ready };

export default function(opt: ReadyOption = {}) {
return new Ready(opt);
}
12 changes: 6 additions & 6 deletions test/index.test.js → test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const assert = require('assert');
const koa = require('koa');
const spy = require('spy');
const ready = require('..');
import { strict as assert } from 'node:assert';
import { spy } from 'tinyspy';
import Koa = require('@eggjs/koa');
import ready from '../src/index.js';

describe('koa', function() {

let app;
let app: any;
beforeEach(function() {
app = koa();
app = new Koa.default();
ready().mixin(app);
});

Expand Down
Loading