From 37f733dfa0c02ff891808ed1fe122d09c07062c4 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Fri, 10 Aug 2018 16:12:08 +0200 Subject: [PATCH] Add post message listeners --- src/core/client/stream/index.tsx | 14 +++++++-- src/core/client/stream/listeners/index.ts | 5 ++++ .../listeners/onPostMessageAuthError.ts | 11 +++++++ .../onPostMessageSetAuthToken.spec.ts | 30 +++++++++++++++++++ .../listeners/onPostMessageSetAuthToken.ts | 16 ++++++++++ .../onPymSetCommentID.spec.ts} | 6 ++-- .../onPymSetCommentID.ts} | 2 +- src/core/client/stream/pym/index.ts | 1 - 8 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 src/core/client/stream/listeners/index.ts create mode 100644 src/core/client/stream/listeners/onPostMessageAuthError.ts create mode 100644 src/core/client/stream/listeners/onPostMessageSetAuthToken.spec.ts create mode 100644 src/core/client/stream/listeners/onPostMessageSetAuthToken.ts rename src/core/client/stream/{pym/withSetCommentID.spec.ts => listeners/onPymSetCommentID.spec.ts} (88%) rename src/core/client/stream/{pym/withSetCommentID.ts => listeners/onPymSetCommentID.ts} (91%) delete mode 100644 src/core/client/stream/pym/index.ts diff --git a/src/core/client/stream/index.tsx b/src/core/client/stream/index.tsx index 4ac024d6d..dcd2af7e1 100644 --- a/src/core/client/stream/index.tsx +++ b/src/core/client/stream/index.tsx @@ -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() { diff --git a/src/core/client/stream/listeners/index.ts b/src/core/client/stream/listeners/index.ts new file mode 100644 index 000000000..7901544e0 --- /dev/null +++ b/src/core/client/stream/listeners/index.ts @@ -0,0 +1,5 @@ +export { default as onPymSetCommentID } from "./onPymSetCommentID"; +export { + default as onPostMessageSetAuthToken, +} from "./onPostMessageSetAuthToken"; +export { default as onPostMessageAuthError } from "./onPostMessageAuthError"; diff --git a/src/core/client/stream/listeners/onPostMessageAuthError.ts b/src/core/client/stream/listeners/onPostMessageAuthError.ts new file mode 100644 index 000000000..668a4ba1f --- /dev/null +++ b/src/core/client/stream/listeners/onPostMessageAuthError.ts @@ -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); + }); +} diff --git a/src/core/client/stream/listeners/onPostMessageSetAuthToken.spec.ts b/src/core/client/stream/listeners/onPostMessageSetAuthToken.spec.ts new file mode 100644 index 000000000..37ed034d6 --- /dev/null +++ b/src/core/client/stream/listeners/onPostMessageSetAuthToken.spec.ts @@ -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); +}); diff --git a/src/core/client/stream/listeners/onPostMessageSetAuthToken.ts b/src/core/client/stream/listeners/onPostMessageSetAuthToken.ts new file mode 100644 index 000000000..9321e0395 --- /dev/null +++ b/src/core/client/stream/listeners/onPostMessageSetAuthToken.ts @@ -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"); + }); + }); +} diff --git a/src/core/client/stream/pym/withSetCommentID.spec.ts b/src/core/client/stream/listeners/onPymSetCommentID.spec.ts similarity index 88% rename from src/core/client/stream/pym/withSetCommentID.spec.ts rename to src/core/client/stream/listeners/onPymSetCommentID.spec.ts index 3403a0092..6b5e67ea2 100644 --- a/src/core/client/stream/pym/withSetCommentID.spec.ts +++ b/src/core/client/stream/listeners/onPymSetCommentID.spec.ts @@ -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); }); diff --git a/src/core/client/stream/pym/withSetCommentID.ts b/src/core/client/stream/listeners/onPymSetCommentID.ts similarity index 91% rename from src/core/client/stream/pym/withSetCommentID.ts rename to src/core/client/stream/listeners/onPymSetCommentID.ts index 903921d9b..4b5547173 100644 --- a/src/core/client/stream/pym/withSetCommentID.ts +++ b/src/core/client/stream/listeners/onPymSetCommentID.ts @@ -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) { diff --git a/src/core/client/stream/pym/index.ts b/src/core/client/stream/pym/index.ts deleted file mode 100644 index 34ba3fc8f..000000000 --- a/src/core/client/stream/pym/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { default as withSetCommentID } from "./withSetCommentID";