-
Notifications
You must be signed in to change notification settings - Fork 355
/
StreamEmbed.ts
177 lines (157 loc) · 4.63 KB
/
StreamEmbed.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { EventEmitter2 } from "eventemitter2";
import { stringifyQuery } from "coral-common/utils";
import ensureNoEndSlash from "coral-common/utils/ensureNoEndSlash";
import urls from "coral-framework/helpers/urls";
import { ExternalConfig } from "coral-framework/lib/externalConfig";
import {
Decorator,
withAutoHeight,
withClickEvent,
withConfig,
withEventEmitter,
withIOSSafariWidthWorkaround,
withKeypressEvent,
withLiveCommentCount,
withPymStorage,
withSetCommentID,
} from "./decorators";
import injectCountScriptIfNeeded from "./injectCountScriptIfNeeded";
import onIntersect, { OnIntersectCancellation } from "./onIntersect";
import PymControl, {
defaultPymControlFactory,
PymControlFactory,
} from "./PymControl";
export interface StreamEmbedConfig {
storyID?: string;
storyURL?: string;
commentID?: string;
autoRender?: boolean;
title: string;
eventEmitter: EventEmitter2;
id: string;
rootURL: string;
accessToken?: string;
bodyClassName?: string;
enableDeprecatedEvents?: boolean;
customCSSURL?: string;
}
export class StreamEmbed {
private config: StreamEmbedConfig;
private pymControl?: PymControl;
private pymControlFactory: PymControlFactory;
private ready = false;
private cancelAutoRender: OnIntersectCancellation | null = null;
constructor(
config: StreamEmbedConfig,
pymControlFactory = defaultPymControlFactory
) {
this.config = config;
this.pymControlFactory = pymControlFactory;
// Detect if comment count injection is needed and add the count script.
injectCountScriptIfNeeded(config.rootURL);
if (config.commentID) {
// Delay emit of `showPermalink` event to allow
// user enough time to setup event listeners.
setTimeout(() => config.eventEmitter.emit("showPermalink"), 0);
}
if (config.autoRender) {
if (config.commentID) {
this.render();
} else {
this.cancelAutoRender = onIntersect(
document.getElementById(config.id)!,
() => {
this.cancelAutoRender = null;
if (!this.rendered) {
this.render();
}
}
);
}
}
config.eventEmitter.once("ready", () => {
this.ready = true;
});
}
private assertRendered() {
if (!this.pymControl) {
throw new Error("Stream Embed must be rendered first");
}
}
public on(eventName: string, callback: (data: any) => void) {
return this.config.eventEmitter.on(eventName, callback);
}
public off(eventName: string, callback: (data: any) => void) {
return this.config.eventEmitter.off(eventName, callback);
}
public login(token: string) {
if (!this.ready) {
this.config.eventEmitter.once("ready", () => this.login(token));
return;
}
this.pymControl!.sendMessage("login", token);
}
public logout() {
if (!this.ready) {
this.config.eventEmitter.once("ready", () => this.logout());
return;
}
this.pymControl!.sendMessage("logout");
}
public remove() {
// If lazy render was enabled, just cancel it.
if (this.cancelAutoRender) {
this.cancelAutoRender();
this.cancelAutoRender = null;
return;
}
this.assertRendered();
this.pymControl!.remove();
this.pymControl = undefined;
}
public get rendered() {
return !!this.pymControl;
}
public render() {
if (this.pymControl) {
throw new Error("Stream Embed already rendered");
}
const externalConfig: ExternalConfig = {
accessToken: this.config.accessToken,
bodyClassName: this.config.bodyClassName,
};
const streamDecorators: ReadonlyArray<Decorator> = [
withIOSSafariWidthWorkaround,
withAutoHeight,
withClickEvent,
withSetCommentID,
withEventEmitter(
this.config.eventEmitter,
this.config.enableDeprecatedEvents
),
withLiveCommentCount(this.config.eventEmitter),
withPymStorage(localStorage, "localStorage"),
withPymStorage(sessionStorage, "sessionStorage"),
withConfig(externalConfig),
withKeypressEvent,
];
const query = stringifyQuery({
storyID: this.config.storyID,
storyURL: this.config.storyURL,
commentID: this.config.commentID,
customCSSURL: this.config.customCSSURL,
});
const url = `${ensureNoEndSlash(this.config.rootURL)}${urls.embed.stream}${
query ? `?${query}` : ""
}`;
this.pymControl = this.pymControlFactory({
id: this.config.id,
title: this.config.title,
decorators: streamDecorators,
url,
});
}
}
export default function createStreamEmbed(config: StreamEmbedConfig) {
return new StreamEmbed(config);
}