Skip to content

Commit 2b3c131

Browse files
Account for all listener adding/removing situations and for the subtype problem (#1)
1 parent 32ce38a commit 2b3c131

File tree

10 files changed

+532
-615
lines changed

10 files changed

+532
-615
lines changed

.github/workflows/ci.yml

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,66 @@
11
name: CI
22

3-
# Run CI when a PR is opened against the branch `main`
4-
# and when one pushes a commit to `main`.
3+
# Run CI when a PR is opened against the branch `master`
4+
# and when one pushes a commit to `master`.
55
on:
66
push:
77
branches: [master]
88
pull_request:
99
branches: [master]
1010

11+
# Run CI on all 3 latest OSes
1112
jobs:
1213
build:
13-
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
os: [ubuntu-latest, macos-latest, windows-latest]
17+
runs-on: ${{ matrix.os }}
1418

1519
steps:
16-
- uses: actions/checkout@v2
20+
- uses: actions/checkout@v3
1721

18-
- uses: purescript-contrib/setup-purescript@main
22+
- name: Set up Node toolchain
23+
uses: actions/setup-node@v3
1924
with:
20-
purescript: "0.15.7"
21-
purs-tidy: "0.9.2"
22-
psa: "0.8.2"
23-
spago: "0.20.9"
24-
psa: "0.7.2"
25+
node-version: "16"
26+
27+
- name: Cache NPM dependencies
28+
uses: actions/cache@v3
29+
env:
30+
cache-name: cache-node-modules
31+
with:
32+
path: ~/.npm
33+
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package.json') }}
34+
restore-keys: |
35+
${{ runner.os }}-build-${{ env.cache-name }}-
36+
${{ runner.os }}-build-
37+
${{ runner.os }}-
38+
39+
- name: Setup PureScript tooling
40+
run:
41+
npm i -g purescript@latest purs-tidy@latest purescript-psa@latest spago@latest
2542

2643
- name: Cache PureScript dependencies
27-
uses: actions/cache@v2
44+
uses: actions/cache@v3
2845
with:
2946
key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }}
3047
path: |
3148
.spago
3249
output
3350
34-
- name: Set up Node toolchain
35-
uses: actions/setup-node@v2
36-
with:
37-
node-version: "16"
38-
3951
# Compile the library/project
4052
# censor-lib: ignore warnings emitted by dependencies
4153
# strict: convert warnings into errors
4254
# Note: `purs-args` actually forwards these args to `psa`
4355
- name: Build the project
4456
run: |
45-
spago build --purs-args "--censor-lib --strict"
57+
npx spago build --purs-args "--censor-lib --strict"
4658
4759
- name: Run tests
4860
run: |
49-
spago test
61+
npx spago -x test.dhall test
5062
5163
- name: Check Formatting
64+
if: runner.os == 'Linux'
5265
run: |
5366
purs-tidy check src test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
/.purs*
99
/.psa*
1010
/.spago
11+
/.vscode

spago.dhall

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{ name = "node-event-emitters"
22
, dependencies =
33
[ "effect"
4+
, "either"
45
, "functions"
56
, "prelude"
6-
, "safe-coerce"
7-
, "unsafe-coerce"
7+
, "unsafe-coerce"
88
]
99
, packages = ./packages.dhall
1010
, sources = [ "src/**/*.purs" ]

src/Node/EventEmitter.js

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
1-
import events from "node:events";
1+
import EventEmitter from "node:events";
22

33
const newImpl = function () {
4-
return new events.EventEmitter();
4+
return new EventEmitter();
55
}
66
export { newImpl as new };
77

8-
export function listenersLengthImpl(emitter, event) {
9-
return emitter.listeners(event).length;
10-
}
11-
12-
export function setMaxListenersImpl(emitter, max) {
13-
emitter.setMaxListeners(max);
14-
}
15-
16-
export function onImpl(emitter, eventName, cb) {
17-
emitter.on(eventName, cb);
18-
return cb;
19-
}
8+
// addEventListener - not implemented; alias to `on`
9+
export const unsafeEmitFn = (emitter) => emitter.emit.bind(emitter);
10+
export const eventNamesImpl = (emitter) => emitter.eventNames();
11+
export const symbolOrStr = (left, right, sym) => typeof sym == "symbol" ? left(sym) : right(sym);
12+
export const getMaxListenersImpl = (emitter) => emitter.getMaxListeners();
13+
export const listenerCountImpl = (emitter, eventName) => emitter.listenerCount(eventName);
14+
// listeners - not implemented; returned functions cannot be used in type-safe way.
15+
export const unsafeOff = (emitter, eventName, cb) => emitter.off(eventName, cb);
16+
export const unsafeOn = (emitter, eventName, cb) => emitter.on(eventName, cb);
17+
export const unsafeOnce = (emitter, eventName, cb) => emitter.once(eventName, cb);
18+
export const unsafePrependListener = (emitter, eventName, cb) => emitter.prependListener(eventName, cb);
19+
export const unsafePrependOnceListener = (emitter, eventName, cb) => emitter.prependOnceListener(eventName, cb);
20+
// removeAllListeners - not implemented; bad practice
21+
// removeEventListener - not implemented; alias to `off`
22+
export const setMaxListenersImpl = (emitter, max) => emitter.setMaxListeners(max);
23+
// rawListeners - not implemented; returned functions cannot be used in type-safe way.
2024

21-
export function offImpl(emitter, eventName, cb) {
22-
emitter.off(eventName, cb);
23-
}
24-
25-
export function onceEventListener(emitter, eventName, cb) {
26-
emitter.once(eventName, cb);
27-
}
28-
29-
export function emitImpl(emitter, eventName) {
30-
return function (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) {
31-
emitter.emit(eventName, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
32-
};
33-
}
34-
const undefined_ = undefined
35-
export { undefined_ as undefined }

0 commit comments

Comments
 (0)