Skip to content

Commit

Permalink
Require Node.js 18 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 12, 2024
1 parent 5261f66 commit b77de7e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 57 deletions.
2 changes: 0 additions & 2 deletions .github/funding.yml

This file was deleted.

10 changes: 4 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = typeof setImmediate === 'function' ? setImmediate : (...args) => {
args.splice(1, 0, 0);
setTimeout(...args);
const setImmediate = typeof globalThis.setImmediate === 'function' ? globalThis.setImmediate : (...arguments_) => {
arguments_.splice(1, 0, 0);
setTimeout(...arguments_);
};

export default setImmediate;
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.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:

Expand Down
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
"description": "Simple setImmediate shim",
"license": "MIT",
"repository": "sindresorhus/set-immediate-shim",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=8"
"node": ">=18"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -28,8 +35,7 @@
"ponyfill"
],
"devDependencies": {
"ava": "^2.3.0",
"import-fresh": "^3.1.0",
"xo": "^0.24.0"
"ava": "^6.1.2",
"xo": "^0.58.0"
}
}
20 changes: 4 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

> Simple [`setImmediate`](https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate) [ponyfill](https://ponyfill.com)
Note: This shim is quite inefficient, see [`#4`](https://github.com/sindresorhus/set-immediate-shim/issues/4).
Note: This shim is [quite inefficient](https://github.com/sindresorhus/set-immediate-shim/issues/4).

## Install

```
$ npm install set-immediate-shim
```sh
npm install set-immediate-shim
```

## Usage

```js
const setImmediateShim = require('set-immediate-shim');
import setImmediateShim from 'set-immediate-shim';

setImmediateShim(() => {
console.log('2');
Expand All @@ -28,15 +28,3 @@ console.log('1');
## Related

- [p-immediate](https://github.com/sindresorhus/p-immediate) - Returns a promise resolved in the next event loop - think `setImmediate()`

---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-set-immediate-shim?utm_source=npm-set-immediate-shim&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
48 changes: 26 additions & 22 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
/* eslint-disable no-global-assign */
import {randomInt} from 'node:crypto';
import test from 'ava';
import importFresh from 'import-fresh';

test.cb('shim', t => {
let called = false;
const importFresh = async moduleName => import(`${moduleName}?${randomInt(100_000_000)}`);

test.serial('shim', async t => {
// Force the shim
const _ = setImmediate;
setImmediate = null;
const setImmediateShim = importFresh('.');
setImmediate = _;
const _ = globalThis.setImmediate;
globalThis.setImmediate = undefined;
const {default: setImmediateShim} = await importFresh('./index.js');
globalThis.setImmediate = _;

setImmediateShim(() => {
called = true;
t.end();
await new Promise(resolve => {
setImmediateShim(() => {
resolve();
});
});

t.false(called);
t.pass();
});

test.cb('pass rest arguments', t => {
test.serial('pass rest arguments', async t => {
// Force the shim
const _ = setImmediate;
setImmediate = null;
const setImmediateShim = importFresh('.');
setImmediate = _;
const _ = globalThis.setImmediate;
globalThis.setImmediate = undefined;
const {default: setImmediateShim} = await importFresh('./index.js');
globalThis.setImmediate = _;

setImmediateShim((a, b) => {
t.is(a, 3);
t.is(b, 5);
t.end();
}, 3, 5);
await new Promise(resolve => {
setImmediateShim((a, b) => {
t.is(a, 3);
t.is(b, 5);
resolve();
}, 3, 5);
});

t.pass();
});

0 comments on commit b77de7e

Please sign in to comment.