@etsoo/react 1.5.99 → 1.6.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/__tests__/EventWatcher.tsx +27 -0
- package/lib/app/EventWatcher.d.ts +35 -0
- package/lib/app/EventWatcher.js +43 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +1 -1
- package/src/app/EventWatcher.ts +66 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { EventWatcher } from '../src/app/EventWatcher';
|
|
4
|
+
|
|
5
|
+
function App(props: { callback: () => void }) {
|
|
6
|
+
const { callback } = props;
|
|
7
|
+
const watcher = new EventWatcher();
|
|
8
|
+
|
|
9
|
+
React.useEffect(() => {
|
|
10
|
+
watcher.add({
|
|
11
|
+
type: 'click',
|
|
12
|
+
action: (_event) => callback(),
|
|
13
|
+
once: true
|
|
14
|
+
});
|
|
15
|
+
}, []);
|
|
16
|
+
return <button onClick={(event) => watcher.do(event)}></button>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test('Tests for EventWatcher', () => {
|
|
20
|
+
const callback = jest.fn();
|
|
21
|
+
render(<App callback={callback} />);
|
|
22
|
+
const button = screen.getByRole<HTMLButtonElement>('button');
|
|
23
|
+
button.click();
|
|
24
|
+
expect(callback).toBeCalled();
|
|
25
|
+
button.click();
|
|
26
|
+
expect(callback).toBeCalledTimes(1);
|
|
27
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Event watcher action
|
|
4
|
+
*/
|
|
5
|
+
export interface EventWatcherAction {
|
|
6
|
+
/**
|
|
7
|
+
* Event type
|
|
8
|
+
*/
|
|
9
|
+
type?: string | string[];
|
|
10
|
+
/**
|
|
11
|
+
* Once action or not
|
|
12
|
+
*/
|
|
13
|
+
once?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Action
|
|
16
|
+
*/
|
|
17
|
+
action: (event: React.BaseSyntheticEvent | string) => void | PromiseLike<void>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Event watcher
|
|
21
|
+
*/
|
|
22
|
+
export declare class EventWatcher {
|
|
23
|
+
private actions;
|
|
24
|
+
/**
|
|
25
|
+
* Add action
|
|
26
|
+
* @param action Action
|
|
27
|
+
*/
|
|
28
|
+
add(action: EventWatcherAction): void;
|
|
29
|
+
/**
|
|
30
|
+
* Do the event
|
|
31
|
+
* @param event Event
|
|
32
|
+
*/
|
|
33
|
+
do(event: React.BaseSyntheticEvent | string): void;
|
|
34
|
+
private isMatch;
|
|
35
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event watcher
|
|
3
|
+
*/
|
|
4
|
+
export class EventWatcher {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.actions = [];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Add action
|
|
10
|
+
* @param action Action
|
|
11
|
+
*/
|
|
12
|
+
add(action) {
|
|
13
|
+
this.actions.push(action);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Do the event
|
|
17
|
+
* @param event Event
|
|
18
|
+
*/
|
|
19
|
+
do(event) {
|
|
20
|
+
// Type
|
|
21
|
+
const type = typeof event === 'string' ? event : event.type;
|
|
22
|
+
// Execute
|
|
23
|
+
const removeIndices = [];
|
|
24
|
+
this.actions.forEach((item, index) => {
|
|
25
|
+
if (!this.isMatch(item, type))
|
|
26
|
+
return;
|
|
27
|
+
item.action(event);
|
|
28
|
+
if (item.once)
|
|
29
|
+
removeIndices.push(index);
|
|
30
|
+
});
|
|
31
|
+
// Remove all once actions
|
|
32
|
+
removeIndices
|
|
33
|
+
.reverse()
|
|
34
|
+
.forEach((index) => this.actions.splice(index, 1));
|
|
35
|
+
}
|
|
36
|
+
isMatch(action, type) {
|
|
37
|
+
if (action.type == null)
|
|
38
|
+
return true;
|
|
39
|
+
if (typeof action.type === 'string')
|
|
40
|
+
return action.type === type;
|
|
41
|
+
return action.type.includes(type);
|
|
42
|
+
}
|
|
43
|
+
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Event watcher action
|
|
5
|
+
*/
|
|
6
|
+
export interface EventWatcherAction {
|
|
7
|
+
/**
|
|
8
|
+
* Event type
|
|
9
|
+
*/
|
|
10
|
+
type?: string | string[];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Once action or not
|
|
14
|
+
*/
|
|
15
|
+
once?: boolean;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Action
|
|
19
|
+
*/
|
|
20
|
+
action: (
|
|
21
|
+
event: React.BaseSyntheticEvent | string
|
|
22
|
+
) => void | PromiseLike<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Event watcher
|
|
27
|
+
*/
|
|
28
|
+
export class EventWatcher {
|
|
29
|
+
private actions: EventWatcherAction[] = [];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Add action
|
|
33
|
+
* @param action Action
|
|
34
|
+
*/
|
|
35
|
+
add(action: EventWatcherAction) {
|
|
36
|
+
this.actions.push(action);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Do the event
|
|
41
|
+
* @param event Event
|
|
42
|
+
*/
|
|
43
|
+
do(event: React.BaseSyntheticEvent | string) {
|
|
44
|
+
// Type
|
|
45
|
+
const type = typeof event === 'string' ? event : event.type;
|
|
46
|
+
|
|
47
|
+
// Execute
|
|
48
|
+
const removeIndices: number[] = [];
|
|
49
|
+
this.actions.forEach((item, index) => {
|
|
50
|
+
if (!this.isMatch(item, type)) return;
|
|
51
|
+
item.action(event);
|
|
52
|
+
if (item.once) removeIndices.push(index);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Remove all once actions
|
|
56
|
+
removeIndices
|
|
57
|
+
.reverse()
|
|
58
|
+
.forEach((index) => this.actions.splice(index, 1));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private isMatch(action: EventWatcherAction, type: string) {
|
|
62
|
+
if (action.type == null) return true;
|
|
63
|
+
if (typeof action.type === 'string') return action.type === type;
|
|
64
|
+
return action.type.includes(type);
|
|
65
|
+
}
|
|
66
|
+
}
|