@gjsify/events 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # @gjsify/events
2
+
3
+ Node.js events module for Gjs
4
+
5
+ ## Inspirations and credits
6
+ - https://github.com/EventEmitter2/EventEmitter2
7
+ - https://github.com/browserify/events
8
+ - https://github.com/geut/brode/blob/main/packages/browser-node-core/src/events.js
9
+ - https://github.com/denoland/deno_std/blob/main/node/_events.mjs
@@ -0,0 +1,36 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var src_exports = {};
30
+ __export(src_exports, {
31
+ default: () => src_default
32
+ });
33
+ module.exports = __toCommonJS(src_exports);
34
+ __reExport(src_exports, require("@gjsify/deno_std/node/events"), module.exports);
35
+ var import_events = __toESM(require("@gjsify/deno_std/node/events"), 1);
36
+ var src_default = import_events.default;
@@ -0,0 +1,6 @@
1
+ export * from "@gjsify/deno_std/node/events";
2
+ import events from "@gjsify/deno_std/node/events";
3
+ var src_default = events;
4
+ export {
5
+ src_default as default
6
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@gjsify/events",
3
+ "version": "0.0.2",
4
+ "description": "Node.js events module for Gjs",
5
+ "type": "module",
6
+ "main": "lib/cjs/index.js",
7
+ "module": "lib/esm/index.js",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./lib/types/index.d.ts",
12
+ "default": "./lib/esm/index.js"
13
+ },
14
+ "require": {
15
+ "types": "./lib/types/index.d.ts",
16
+ "default": "./lib/cjs/index.js"
17
+ }
18
+ }
19
+ },
20
+ "scripts": {
21
+ "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo || exit 0",
22
+ "print:name": "echo '@gjsify/events'",
23
+ "build": "yarn print:name && yarn build:gjsify",
24
+ "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
25
+ "build:types": "tsc --project tsconfig.types.json || exit 0",
26
+ "build:test": "yarn build:test:gjs && yarn build:test:node",
27
+ "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
28
+ "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
29
+ "test": "yarn print:name && yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
30
+ "test:gjs": "gjs -m test.gjs.mjs",
31
+ "test:node": "node test.node.mjs"
32
+ },
33
+ "keywords": [
34
+ "gjs",
35
+ "node",
36
+ "events"
37
+ ],
38
+ "devDependencies": {
39
+ "@gjsify/cli": "^0.0.2",
40
+ "@gjsify/unit": "^0.0.2",
41
+ "@types/node": "^20.3.1",
42
+ "typescript": "^5.1.3"
43
+ },
44
+ "dependencies": {
45
+ "@gjsify/deno_std": "^0.0.2"
46
+ }
47
+ }
@@ -0,0 +1,158 @@
1
+ import { describe, it, expect } from '@gjsify/unit';
2
+
3
+ import { EventEmitter } from 'events';
4
+
5
+ export default async () => {
6
+
7
+ // Credits https://github.com/EventEmitter2/EventEmitter2/blob/master/test/simple/emit.js
8
+ // TODO port more tests
9
+
10
+ await describe('events.EventEmitter: emit', async () => {
11
+ await it('1. Add two listeners on a single event and emit the event', async () => {
12
+ var emitter = new EventEmitter({ verbose: true } as any);
13
+ let count = 0;
14
+
15
+ function functionA() { count++; }
16
+ function functionB() { count++ }
17
+
18
+ emitter.on('test2', functionA);
19
+ emitter.on('test2', functionB);
20
+
21
+ emitter.emit('test2');
22
+
23
+ expect(count).toBe(2);
24
+ });
25
+
26
+ await it('2. Add two listeners on a single event and emit the event twice', async () => {
27
+ var emitter = new EventEmitter({ verbose: true } as any);
28
+ let count = 0;
29
+
30
+ function functionA() { count++ }
31
+ function functionB() { count++ }
32
+
33
+ emitter.on('test2', functionA);
34
+ emitter.on('test2', functionB);
35
+
36
+ emitter.emit('test2');
37
+ emitter.emit('test2');
38
+
39
+ expect(count).toBe(4);
40
+ });
41
+
42
+ await it('3. Add two listeners on a single event and emit the event with a parameter', async () => {
43
+ var emitter = new EventEmitter({ verbose: true } as any);
44
+ let count = 0;
45
+
46
+ function functionA(value1: string) {
47
+ count++;
48
+ expect(typeof value1).toBe('string');
49
+ }
50
+
51
+ function functionB(value1: string) {
52
+ count++;
53
+ expect(typeof value1).toBe('string');
54
+ }
55
+
56
+ emitter.on('test2', functionA);
57
+ emitter.on('test2', functionB);
58
+
59
+ emitter.emit('test2', 'Hello, Node');
60
+
61
+ expect(count).toBe(2);
62
+ });
63
+
64
+ await it(`4. Add two listeners on an single event and emit the event twice with a parameter.`, async () => {
65
+ var emitter = new EventEmitter({ verbose: true } as any);
66
+ let count = 0;
67
+
68
+ function functionA(value1: string) {
69
+ count++;
70
+ expect(typeof value1).toBe('string');
71
+ }
72
+
73
+ function functionB(value1: string) {
74
+ count++;
75
+ expect(typeof value1).toBe('string');
76
+ }
77
+
78
+ emitter.on('test2', functionA);
79
+ emitter.on('test2', functionB);
80
+
81
+ emitter.emit('test2', 'Hello, Node1');
82
+ emitter.emit('test2', 'Hello, Node2');
83
+
84
+ expect(count).toBe(4);
85
+ });
86
+
87
+ await it(`5. Add two listeners on an single event and emit the event twice with multiple parameters.`, async () => {
88
+
89
+ var emitter = new EventEmitter({ verbose: true } as any);
90
+ let count = 0;
91
+
92
+ function functionA(value1: string, value2: string, value3: string) {
93
+ count++;
94
+ expect(true).toBeTruthy(); // 'The event was raised';
95
+ expect(typeof value1).toBe('string');
96
+ expect(typeof value2).toBe('string');
97
+ expect(typeof value3).toBe('string');
98
+ }
99
+
100
+ function functionB(value1: string, value2: string, value3: string) {
101
+ count++;
102
+ expect(true).toBeTruthy(); // 'The event was raised';
103
+ expect(typeof value1).toBe('string');
104
+ expect(typeof value2).toBe('string');
105
+ expect(typeof value3).toBe('string');
106
+ }
107
+
108
+ emitter.on('test2', functionA);
109
+ emitter.on('test2', functionB);
110
+
111
+ emitter.emit('test2', 'Hello, Node1', 'Hello, Node2', 'Hello, Node3');
112
+ emitter.emit('test2', 'Hello, Node1', 'Hello, Node2', 'Hello, Node3');
113
+
114
+ expect(count).toBe(4);
115
+ });
116
+
117
+
118
+ await it('6. Check return values of emit.', async () => {
119
+ let count = 0;
120
+ var emitter = new EventEmitter({ verbose: true } as any);
121
+
122
+ function functionA() {
123
+ count++;
124
+ expect(true).toBeTruthy(); // 'The event was raised'
125
+ }
126
+
127
+ emitter.on('test6', functionA);
128
+
129
+ expect(emitter.emit('test6')).toBeTruthy(); // 'emit should return true after calling a listener'
130
+ expect(emitter.emit('other')).toBeFalsy(); // 'emit should return false when no listener was called'
131
+
132
+ // The original implementation has no onAny method
133
+ expect(() => {
134
+ (emitter as any).onAny(functionA);
135
+ }).toThrow();
136
+
137
+ expect(emitter.emit('other')).toBeFalsy(); // 'emit should return false without the onAny() listener'
138
+
139
+ expect(count).toBe(1);
140
+ });
141
+
142
+ await it('7. Emit event with more than 2 arguments', async () => {
143
+ let count = 0;
144
+ var emitter = new EventEmitter({ verbose: true } as any);
145
+
146
+ emitter.on('test', function (x: number, y: number, z: number) {
147
+ count++;
148
+ expect(x).toBe(1);
149
+ expect(y).toBe(2);
150
+ expect(z).toBe(3);
151
+ });
152
+
153
+ emitter.emit('test', 1, 2, 3);
154
+ expect(count).toBe(1);
155
+ });
156
+
157
+ });
158
+ }
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ // import "@gjsify/deno-runtime/ext/web/00_infra";
2
+ // import "@gjsify/deno-runtime/ext/web/01_dom_exception";
3
+ // import "@gjsify/deno-runtime/ext/web/01_mimesniff";
4
+ // import "@gjsify/deno-runtime/ext/web/02_event";
5
+ // import "@gjsify/deno-runtime/ext/web/02_structured_clone";
6
+ // import "@gjsify/deno-runtime/ext/web/02_timers";
7
+ // import "@gjsify/deno-runtime/ext/web/03_abort_signal";
8
+
9
+ export * from '@gjsify/deno_std/node/events';
10
+ import events from '@gjsify/deno_std/node/events';
11
+ export default events;
package/src/test.mts ADDED
@@ -0,0 +1,6 @@
1
+
2
+ import { run } from '@gjsify/unit';
3
+
4
+ import eventEmitterTestSuite from './event-emitter.spec.js';
5
+
6
+ run({ eventEmitterTestSuite });