mirror of
https://github.com/wassname/talk.git
synced 2026-08-05 13:30:26 +08:00
[CORL-491] Support event alias loginPrompt (#2474)
* feat: support event alias loginPrompt * lint: import ordering
This commit is contained in:
@@ -23,6 +23,17 @@
|
||||
<script>
|
||||
const CoralStreamEmbed = Coral.createStreamEmbed({
|
||||
id: "coralStreamEmbed",
|
||||
/**
|
||||
* You can listen to events using the example below.
|
||||
* The argument passed is the event emitter from
|
||||
* https://github.com/asyncly/EventEmitter2
|
||||
*
|
||||
* events: function(events) {
|
||||
* events.onAny(function(eventName, data) {
|
||||
* console.log(eventName, data);
|
||||
* });
|
||||
* },
|
||||
*/
|
||||
});
|
||||
window.CoralStreamEmbed = CoralStreamEmbed;
|
||||
CoralStreamEmbed.render();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { noop } from "lodash";
|
||||
import React from "react";
|
||||
import { createRenderer } from "react-test-renderer/shallow";
|
||||
|
||||
@@ -27,3 +28,24 @@ it("Broadcasts events to pym", () => {
|
||||
);
|
||||
expect(pym.sendMessage.calledOnce).toBe(true);
|
||||
});
|
||||
|
||||
it("emits event aliases", () => {
|
||||
const eventEmitter: any = {
|
||||
emit: createSinonStub(
|
||||
s => s.throws(),
|
||||
s => s.withArgs("loginPrompt").returns(null)
|
||||
),
|
||||
onAny: (cb: (eventName: string, value: any) => void) => {
|
||||
cb("mutation.showAuthPopup", { view: "SIGN_IN" });
|
||||
},
|
||||
};
|
||||
|
||||
const pym = {
|
||||
sendMessage: noop,
|
||||
};
|
||||
|
||||
createRenderer().render(
|
||||
<OnEvents pym={pym as any} eventEmitter={eventEmitter} />
|
||||
);
|
||||
expect(eventEmitter.emit.calledOnce).toBe(true);
|
||||
});
|
||||
|
||||
@@ -2,6 +2,8 @@ import { Component } from "react";
|
||||
|
||||
import { CoralContext, withContext } from "coral-framework/lib/bootstrap";
|
||||
|
||||
import emitEventAliases from "./emitEventAliases";
|
||||
|
||||
interface Props {
|
||||
pym: CoralContext["pym"];
|
||||
eventEmitter: CoralContext["eventEmitter"];
|
||||
@@ -10,8 +12,9 @@ interface Props {
|
||||
export class OnEvents extends Component<Props> {
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
// Auth popup will use this to handle a successful login.
|
||||
props.eventEmitter.onAny((eventName: string, value: any) => {
|
||||
// Emit event aliases.
|
||||
emitEventAliases(props.eventEmitter, eventName, value);
|
||||
props.pym!.sendMessage(
|
||||
"event",
|
||||
JSON.stringify({
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { EventEmitter2 } from "eventemitter2";
|
||||
|
||||
export default function emitEventAliases(
|
||||
eventEmitter: EventEmitter2,
|
||||
eventName: string,
|
||||
value: any
|
||||
) {
|
||||
switch (eventName) {
|
||||
case "mutation.showAuthPopup":
|
||||
switch (value.view) {
|
||||
case "SIGN_IN":
|
||||
eventEmitter.emit("loginPrompt");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user