-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUserScriptHookComponent.tsx
38 lines (32 loc) · 1.2 KB
/
UserScriptHookComponent.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// MIT © 2017 azu
import * as React from "react";
import { UserScriptWindow } from "./UserScriptContainer";
import { UserScriptEvent } from "./UserScriptEvent";
export abstract class UserScriptHookComponent<P = {}, S = {}> extends React.PureComponent<P, S> {
abstract displayName: string;
private get userScriptEvent(): UserScriptEvent | undefined {
const userScript = (window as UserScriptWindow).userScript;
if (userScript && userScript.event) {
return userScript.event;
}
return;
}
private createComponentEventName(name: string): string {
return `${this.displayName}::${name}`;
}
componentDidMount() {
if (this.userScriptEvent) {
this.userScriptEvent.dispatch(this.createComponentEventName("componentDidMount"), this.props);
}
}
componentDidUpdate() {
if (this.userScriptEvent) {
this.userScriptEvent.dispatch(this.createComponentEventName("componentDidUpdate"), this.props);
}
}
componentWillUnmount() {
if (this.userScriptEvent) {
this.userScriptEvent.dispatch(this.createComponentEventName("componentWillUnmount"), this.props);
}
}
}