From e0bed8c1144f5c96977c0c190638aea2f1af1eac Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 10 Nov 2017 12:29:18 +0100 Subject: [PATCH] Force env editor to refresh on sync change (Closes #563) --- app/common/database.js | 20 ++++++++--- .../workspace-environments-edit-modal.js | 34 +++++++++++++++++-- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/app/common/database.js b/app/common/database.js index ab0caa4f2..2203626d6 100644 --- a/app/common/database.js +++ b/app/common/database.js @@ -40,7 +40,11 @@ function getDBFilePath (modelType) { * @param forceReset * @returns {null} */ -export async function init (types: Array, config: Object = {}, forceReset: boolean = false) { +export async function init ( + types: Array, + config: Object = {}, + forceReset: boolean = false +) { if (forceReset) { changeListeners = []; db = {}; @@ -240,8 +244,10 @@ export function insert (doc: T, fromSync: boolean = false): Promis return reject(err); } - notifyOfChange(CHANGE_INSERT, newDoc, fromSync); resolve(newDoc); + + // NOTE: This needs to be after we resolve + notifyOfChange(CHANGE_INSERT, newDoc, fromSync); }); }); } @@ -254,9 +260,10 @@ export function update (doc: T, fromSync: boolean = false): Promis return reject(err); } - notifyOfChange(CHANGE_UPDATE, docWithDefaults, fromSync); - resolve(docWithDefaults); + + // NOTE: This needs to be after we resolve + notifyOfChange(CHANGE_UPDATE, docWithDefaults, fromSync); }); }); } @@ -311,7 +318,10 @@ export async function docUpdate (originalDoc: T, patch: Object = { return update(doc); } -export async function docCreate (type: string, ...patches: Array): Promise { +export async function docCreate ( + type: string, + ...patches: Array +): Promise { const doc = await initModel( type, ...patches, diff --git a/app/ui/components/modals/workspace-environments-edit-modal.js b/app/ui/components/modals/workspace-environments-edit-modal.js index 735c78946..5b54a5ebc 100644 --- a/app/ui/components/modals/workspace-environments-edit-modal.js +++ b/app/ui/components/modals/workspace-environments-edit-modal.js @@ -17,6 +17,7 @@ import {trackEvent} from '../../../analytics/index'; import {DEBOUNCE_MILLIS} from '../../../common/constants'; import type {Workspace} from '../../../models/workspace'; import type {Environment} from '../../../models/environment'; +import * as db from '../../../common/database'; type Props = { activeEnvironment: Environment | null, @@ -43,6 +44,7 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent { colorChangeTimeout: any; saveTimeout: any; modal: Modal; + editorKey: number; constructor (props: Props) { super(props); @@ -55,6 +57,7 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent { }; this.colorChangeTimeout = null; + this.editorKey = 0; } hide () { @@ -212,6 +215,26 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent { this._handleChangeEnvironmentColor(environment, null); } + componentDidMount () { + db.onChange(async changes => { + const {selectedEnvironmentId} = this.state; + + for (const change of changes) { + const [ + _, // eslint-disable-line no-unused-vars + doc, + fromSync + ] = change; + + // Force an editor refresh if any changes from sync come in + if (doc._id === selectedEnvironmentId && fromSync) { + this.editorKey = doc.modified; + await this._load(this.state.workspace); + } + } + }); + } + async _handleClickColorChange (environment: Environment) { let el = document.querySelector('#env-color-picker'); @@ -244,7 +267,14 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent { return; } - const data = this.environmentEditorRef.getValue(); + let data; + try { + data = this.environmentEditorRef.getValue(); + } catch (err) { + // Invalid JSON probably + return; + } + const activeEnvironment = this._getActiveEnvironment(); if (activeEnvironment) { @@ -385,7 +415,7 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent { editorKeyMap={editorKeyMap} lineWrapping={lineWrapping} ref={this._setEditorRef} - key={activeEnvironment ? activeEnvironment._id : 'n/a'} + key={`${this.editorKey}::${activeEnvironment ? activeEnvironment._id : 'n/a'}`} environment={activeEnvironment ? activeEnvironment.data : {}} didChange={this._didChange} render={render}