@ixfx/events 0.38.0 → 0.39.0
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 +28 -0
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +0 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/dist/src/race.d.ts +0 -23
- package/dist/src/race.d.ts.map +0 -1
- package/dist/src/race.js +0 -70
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
A simple eventing implementation.
|
|
2
|
+
|
|
3
|
+
For a class, extend `SimpleEventEmitter`.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
export type MyClassEvents = {
|
|
7
|
+
// An event named `blah` that has one field, 'id'.
|
|
8
|
+
readonly blah: (id:string);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export class MyClass extends SimpleEventEmitter<MyClassEvents> {
|
|
12
|
+
|
|
13
|
+
test() {
|
|
14
|
+
// Eg: firing an event
|
|
15
|
+
this.fireEvent(`blah`, {
|
|
16
|
+
id: Math.random()
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
You can then un/subscribe as usual:
|
|
23
|
+
```js
|
|
24
|
+
const c = new MyClass();
|
|
25
|
+
c.addEventListener(`blah`, (id) => {
|
|
26
|
+
// Event!
|
|
27
|
+
});
|
|
28
|
+
```
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,cAAc,YAAY,CAAC"}
|
package/dist/src/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/index.ts","../src/map-of.ts","../src/
|
|
1
|
+
{"root":["../src/index.ts","../src/map-of.ts","../src/simple-event-emitter.ts","../src/types.ts","../__tests__/test.ts"],"version":"5.8.3"}
|
package/package.json
CHANGED
package/dist/src/race.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Subscribes to events on `target`, returning the event data
|
|
3
|
-
* from the first event that fires.
|
|
4
|
-
*
|
|
5
|
-
* By default waits a maximum of 1 minute.
|
|
6
|
-
*
|
|
7
|
-
* Automatically unsubscribes on success or failure (ie. timeout)
|
|
8
|
-
*
|
|
9
|
-
* ```js
|
|
10
|
-
* // Event will be data from either event, whichever fires first
|
|
11
|
-
* // Exception is thrown if neither fires within 1 second
|
|
12
|
-
* const event = await eventRace(document.body, [`pointermove`, `pointerdown`], { timeout: 1000 });
|
|
13
|
-
* ```
|
|
14
|
-
* @param target Event source
|
|
15
|
-
* @param eventNames Event name(s)
|
|
16
|
-
* @param options Options
|
|
17
|
-
* @returns
|
|
18
|
-
*/
|
|
19
|
-
export declare const eventRace: (target: EventTarget, eventNames: Array<string>, options?: Partial<{
|
|
20
|
-
timeoutMs: number;
|
|
21
|
-
signal: AbortSignal;
|
|
22
|
-
}>) => Promise<Event>;
|
|
23
|
-
//# sourceMappingURL=race.d.ts.map
|
package/dist/src/race.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"race.d.ts","sourceRoot":"","sources":["../../src/race.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,SAAS,GAAI,QAAQ,WAAW,EAAE,YAAY,KAAK,CAAC,MAAM,CAAC,EAAE,UAAS,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,WAAW,CAAA;CAAE,CAAM,mBAmD1I,CAAA"}
|
package/dist/src/race.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Subscribes to events on `target`, returning the event data
|
|
3
|
-
* from the first event that fires.
|
|
4
|
-
*
|
|
5
|
-
* By default waits a maximum of 1 minute.
|
|
6
|
-
*
|
|
7
|
-
* Automatically unsubscribes on success or failure (ie. timeout)
|
|
8
|
-
*
|
|
9
|
-
* ```js
|
|
10
|
-
* // Event will be data from either event, whichever fires first
|
|
11
|
-
* // Exception is thrown if neither fires within 1 second
|
|
12
|
-
* const event = await eventRace(document.body, [`pointermove`, `pointerdown`], { timeout: 1000 });
|
|
13
|
-
* ```
|
|
14
|
-
* @param target Event source
|
|
15
|
-
* @param eventNames Event name(s)
|
|
16
|
-
* @param options Options
|
|
17
|
-
* @returns
|
|
18
|
-
*/
|
|
19
|
-
export const eventRace = (target, eventNames, options = {}) => {
|
|
20
|
-
const intervalMs = options.timeoutMs ?? 60_1000; //intervalToMs(options.timeout, 60 * 1000);
|
|
21
|
-
const signal = options.signal;
|
|
22
|
-
let triggered = false;
|
|
23
|
-
let disposed = false;
|
|
24
|
-
let timeout;
|
|
25
|
-
const promise = new Promise((resolve, reject) => {
|
|
26
|
-
const onEvent = (event) => {
|
|
27
|
-
if (`type` in event) {
|
|
28
|
-
if (eventNames.includes(event.type)) {
|
|
29
|
-
triggered = true;
|
|
30
|
-
resolve(event);
|
|
31
|
-
dispose();
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
console.warn(`eventRace: Got event '${event.type}' that is not in race list`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
console.warn(`eventRace: Event data does not have expected 'type' field`);
|
|
39
|
-
console.log(event);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
for (const name of eventNames) {
|
|
43
|
-
target.addEventListener(name, onEvent);
|
|
44
|
-
}
|
|
45
|
-
const dispose = () => {
|
|
46
|
-
if (disposed)
|
|
47
|
-
return;
|
|
48
|
-
if (timeout !== undefined)
|
|
49
|
-
clearTimeout(timeout);
|
|
50
|
-
timeout = undefined;
|
|
51
|
-
disposed = true;
|
|
52
|
-
for (const name of eventNames) {
|
|
53
|
-
target.removeEventListener(name, onEvent);
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
timeout = setTimeout(() => {
|
|
57
|
-
if (triggered || disposed)
|
|
58
|
-
return;
|
|
59
|
-
dispose();
|
|
60
|
-
reject(new Error(`eventRace: Events not fired within interval. Events: ${JSON.stringify(eventNames)} Interval: ${intervalMs}`));
|
|
61
|
-
}, intervalMs);
|
|
62
|
-
signal?.addEventListener(`abort`, () => {
|
|
63
|
-
if (triggered || disposed)
|
|
64
|
-
return;
|
|
65
|
-
dispose();
|
|
66
|
-
reject(new Error(`Abort signal received ${signal.reason}`));
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
return promise;
|
|
70
|
-
};
|