From bcc7fb47ef4d5bc13c4951622ef97707e6993009 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 8 Apr 2018 15:08:32 -0700 Subject: [PATCH] start using an event bus --- package-lock.json | 13 ++++-- package.json | 1 + routes/_actions/compose.js | 8 ++-- routes/_components/compose/ComposeBox.html | 8 ---- routes/_components/compose/ComposeInput.html | 3 -- routes/_components/dialog/ComposeDialog.html | 11 ++++- .../_components/status/StatusComposeBox.html | 43 ++++++++++--------- routes/_utils/eventBus.js | 17 ++++++++ 8 files changed, 63 insertions(+), 41 deletions(-) create mode 100644 routes/_utils/eventBus.js diff --git a/package-lock.json b/package-lock.json index a7551009..e9856c4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3186,9 +3186,9 @@ } }, "events": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/events/-/events-1.1.1.tgz", - "integrity": "sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=" + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/events/-/events-2.0.0.tgz", + "integrity": "sha512-r/M5YkNg9zwI8QbSf7tsDWWJvO3PGwZXyG7GpFAxtMASnHL2eblFd7iHiGPtyGKKFPZ59S63NeX10Ws6WqGDcg==" }, "evp_bytestokey": { "version": "1.0.3", @@ -6369,6 +6369,13 @@ "url": "0.11.0", "util": "0.10.3", "vm-browserify": "0.0.4" + }, + "dependencies": { + "events": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/events/-/events-1.1.1.tgz", + "integrity": "sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=" + } } }, "node-sass": { diff --git a/package.json b/package.json index 3eeb5c1a..547ec2b4 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "cross-env": "^5.1.3", "css-loader": "^0.28.7", "esm": "^3.0.12", + "events": "^2.0.0", "express": "^4.16.2", "fg-loadcss": "^2.0.1", "file-api": "^0.10.4", diff --git a/routes/_actions/compose.js b/routes/_actions/compose.js index 1ab6bea4..d0c0a1b1 100644 --- a/routes/_actions/compose.js +++ b/routes/_actions/compose.js @@ -3,6 +3,7 @@ import { toast } from '../_utils/toast' import { postStatus as postStatusToServer } from '../_api/statuses' import { addStatusOrNotification } from './addStatusOrNotification' import { database } from '../_database/database' +import { emit } from '../_utils/eventBus' export async function insertHandleForReply (statusId) { let instanceName = store.get('currentInstance') @@ -30,17 +31,14 @@ export async function postStatus (realm, text, inReplyToId, mediaIds, } store.set({ - postingStatus: true, - postedStatusForRealm: null + postingStatus: true }) try { let status = await postStatusToServer(instanceName, accessToken, text, inReplyToId, mediaIds, sensitive, spoilerText, visibility) addStatusOrNotification(instanceName, 'home', status) store.clearComposeData(realm) - store.set({ - postedStatusForRealm: realm - }) + emit('postedStatus', realm) } catch (e) { console.error(e) toast.say('Unable to post status: ' + (e.message || '')) diff --git a/routes/_components/compose/ComposeBox.html b/routes/_components/compose/ComposeBox.html index 6d179de0..19a41132 100644 --- a/routes/_components/compose/ComposeBox.html +++ b/routes/_components/compose/ComposeBox.html @@ -126,13 +126,6 @@ // make sure the visibility is consistent with the replied-to status setReplyVisibility(realm, replyVisibility) } - - this.observe('postedStatusForRealm', postedStatusForRealm => { - if (postedStatusForRealm !== realm) { - return - } - this.fire('postedStatus') - }, {init: false}) }, ondestroy() { this.teardownStickyObserver() @@ -177,7 +170,6 @@ overLimit: (length) => length > CHAR_LIMIT, contentWarningShown: (composeData) => composeData.contentWarningShown, contentWarning: (composeData) => composeData.contentWarning || '', - postedStatusForRealm: ($postedStatusForRealm) => $postedStatusForRealm, timelineInitialized: ($timelineInitialized) => $timelineInitialized }, transitions: { diff --git a/routes/_components/compose/ComposeInput.html b/routes/_components/compose/ComposeInput.html index bd6b73d2..5a17b1f5 100644 --- a/routes/_components/compose/ComposeInput.html +++ b/routes/_components/compose/ComposeInput.html @@ -166,9 +166,6 @@ data: () => ({ rawText: '' }), - computed: { - postedStatusForRealm: ($postedStatusForRealm) => $postedStatusForRealm - }, events: { selectionChange } diff --git a/routes/_components/dialog/ComposeDialog.html b/routes/_components/dialog/ComposeDialog.html index c5114770..eb947cc2 100644 --- a/routes/_components/dialog/ComposeDialog.html +++ b/routes/_components/dialog/ComposeDialog.html @@ -6,13 +6,17 @@ background="var(--main-bg)" on:destroyDialog="destroy()" > - + \ No newline at end of file diff --git a/routes/_utils/eventBus.js b/routes/_utils/eventBus.js new file mode 100644 index 00000000..a542a318 --- /dev/null +++ b/routes/_utils/eventBus.js @@ -0,0 +1,17 @@ +import EventEmitter from 'events' + +const eventBus = new EventEmitter() + +if (process.browser && process.env.NODE_ENV !== 'production') { + window.eventBus = eventBus +} + +export function on (eventName, component, method) { + let callback = method.bind(component) + eventBus.on(eventName, callback) + component.on('destroy', () => { + eventBus.removeListener(eventName, callback) + }) +} + +export const emit = eventBus.emit.bind(eventBus)