mirror of
https://github.com/wassname/talk.git
synced 2026-07-18 12:40:13 +08:00
Add post message listeners
This commit is contained in:
@@ -10,16 +10,24 @@ import {
|
||||
} from "talk-framework/lib/bootstrap";
|
||||
|
||||
import AppContainer from "./containers/AppContainer";
|
||||
import {
|
||||
onPostMessageAuthError,
|
||||
onPostMessageSetAuthToken,
|
||||
onPymSetCommentID,
|
||||
} from "./listeners";
|
||||
import { initLocalState } from "./local";
|
||||
import localesData from "./locales";
|
||||
import { withSetCommentID } from "./pym";
|
||||
|
||||
const pymFeatures = [withSetCommentID];
|
||||
const listeners = [
|
||||
onPymSetCommentID,
|
||||
onPostMessageSetAuthToken,
|
||||
onPostMessageAuthError,
|
||||
];
|
||||
|
||||
// This is called when the context is first initialized.
|
||||
async function init(context: TalkContext) {
|
||||
await initLocalState(context.relayEnvironment);
|
||||
pymFeatures.forEach(f => f(context));
|
||||
listeners.forEach(f => f(context));
|
||||
}
|
||||
|
||||
async function main() {
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
export { default as onPymSetCommentID } from "./onPymSetCommentID";
|
||||
export {
|
||||
default as onPostMessageSetAuthToken,
|
||||
} from "./onPostMessageSetAuthToken";
|
||||
export { default as onPostMessageAuthError } from "./onPostMessageAuthError";
|
||||
@@ -0,0 +1,11 @@
|
||||
import { TalkContext } from "talk-framework/lib/bootstrap";
|
||||
|
||||
export default function onPostMessageSetAuthToken({
|
||||
postMessage,
|
||||
}: TalkContext) {
|
||||
// Auth popup will use this to send back errors during login.
|
||||
postMessage!.on("authError", error => {
|
||||
// tslint:disable-next-line:no-console
|
||||
console.error(error);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Environment, RecordSource } from "relay-runtime";
|
||||
|
||||
import { LOCAL_ID } from "talk-framework/lib/relay";
|
||||
import { createRelayEnvironment } from "talk-framework/testHelpers";
|
||||
|
||||
import onPostMessageSetAuthToken from "./onPostMessageSetAuthToken";
|
||||
|
||||
let relayEnvironment: Environment;
|
||||
const source: RecordSource = new RecordSource();
|
||||
|
||||
beforeAll(() => {
|
||||
relayEnvironment = createRelayEnvironment({
|
||||
source,
|
||||
});
|
||||
});
|
||||
|
||||
it("Sets auth token", () => {
|
||||
const token = "auth-token";
|
||||
const context = {
|
||||
postMessage: {
|
||||
on: (name: string, cb: (token: string) => void) => {
|
||||
expect(name).toBe("setAuthToken");
|
||||
cb(token);
|
||||
},
|
||||
},
|
||||
relayEnvironment,
|
||||
};
|
||||
onPostMessageSetAuthToken(context as any);
|
||||
expect(source.get(LOCAL_ID)!.authToken).toEqual(token);
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { commitLocalUpdate } from "react-relay";
|
||||
|
||||
import { TalkContext } from "talk-framework/lib/bootstrap";
|
||||
import { LOCAL_ID } from "talk-framework/lib/relay";
|
||||
|
||||
export default function onPostMessageSetAuthToken({
|
||||
relayEnvironment,
|
||||
postMessage,
|
||||
}: TalkContext) {
|
||||
// Auth popup will use this to handle a successful login.
|
||||
postMessage!.on("setAuthToken", token => {
|
||||
commitLocalUpdate(relayEnvironment, s => {
|
||||
s.get(LOCAL_ID)!.setValue(token, "authToken");
|
||||
});
|
||||
});
|
||||
}
|
||||
+3
-3
@@ -3,7 +3,7 @@ import { Environment, RecordSource } from "relay-runtime";
|
||||
import { LOCAL_ID } from "talk-framework/lib/relay";
|
||||
import { createRelayEnvironment } from "talk-framework/testHelpers";
|
||||
|
||||
import withSetCommentID from "./withSetCommentID";
|
||||
import onPymSetCommentID from "./onPymSetCommentID";
|
||||
|
||||
let relayEnvironment: Environment;
|
||||
const source: RecordSource = new RecordSource();
|
||||
@@ -25,7 +25,7 @@ it("Sets comment id", () => {
|
||||
},
|
||||
relayEnvironment,
|
||||
};
|
||||
withSetCommentID(context as any);
|
||||
onPymSetCommentID(context as any);
|
||||
expect(source.get(LOCAL_ID)!.commentID).toEqual(id);
|
||||
});
|
||||
|
||||
@@ -40,6 +40,6 @@ it("Sets comment id to null when empty", () => {
|
||||
},
|
||||
relayEnvironment,
|
||||
};
|
||||
withSetCommentID(context as any);
|
||||
onPymSetCommentID(context as any);
|
||||
expect(source.get(LOCAL_ID)!.commentID).toEqual(null);
|
||||
});
|
||||
+1
-1
@@ -3,7 +3,7 @@ import { commitLocalUpdate } from "react-relay";
|
||||
import { TalkContext } from "talk-framework/lib/bootstrap";
|
||||
import { LOCAL_ID } from "talk-framework/lib/relay";
|
||||
|
||||
export default function withSetCommentID({
|
||||
export default function onPymSetCommentID({
|
||||
relayEnvironment,
|
||||
pym,
|
||||
}: TalkContext) {
|
||||
@@ -1 +0,0 @@
|
||||
export { default as withSetCommentID } from "./withSetCommentID";
|
||||
Reference in New Issue
Block a user