Force env editor to refresh on sync change (Closes #563)

This commit is contained in:
Gregory Schier 2017-11-10 12:29:18 +01:00
parent 99a7d6fe60
commit e0bed8c114
2 changed files with 47 additions and 7 deletions

View File

@ -40,7 +40,11 @@ function getDBFilePath (modelType) {
* @param forceReset * @param forceReset
* @returns {null} * @returns {null}
*/ */
export async function init (types: Array<string>, config: Object = {}, forceReset: boolean = false) { export async function init (
types: Array<string>,
config: Object = {},
forceReset: boolean = false
) {
if (forceReset) { if (forceReset) {
changeListeners = []; changeListeners = [];
db = {}; db = {};
@ -240,8 +244,10 @@ export function insert<T: BaseModel> (doc: T, fromSync: boolean = false): Promis
return reject(err); return reject(err);
} }
notifyOfChange(CHANGE_INSERT, newDoc, fromSync);
resolve(newDoc); resolve(newDoc);
// NOTE: This needs to be after we resolve
notifyOfChange(CHANGE_INSERT, newDoc, fromSync);
}); });
}); });
} }
@ -254,9 +260,10 @@ export function update<T: BaseModel> (doc: T, fromSync: boolean = false): Promis
return reject(err); return reject(err);
} }
notifyOfChange(CHANGE_UPDATE, docWithDefaults, fromSync);
resolve(docWithDefaults); resolve(docWithDefaults);
// NOTE: This needs to be after we resolve
notifyOfChange(CHANGE_UPDATE, docWithDefaults, fromSync);
}); });
}); });
} }
@ -311,7 +318,10 @@ export async function docUpdate<T: BaseModel> (originalDoc: T, patch: Object = {
return update(doc); return update(doc);
} }
export async function docCreate<T: BaseModel> (type: string, ...patches: Array<Object>): Promise<T> { export async function docCreate<T: BaseModel> (
type: string,
...patches: Array<Object>
): Promise<T> {
const doc = await initModel( const doc = await initModel(
type, type,
...patches, ...patches,

View File

@ -17,6 +17,7 @@ import {trackEvent} from '../../../analytics/index';
import {DEBOUNCE_MILLIS} from '../../../common/constants'; import {DEBOUNCE_MILLIS} from '../../../common/constants';
import type {Workspace} from '../../../models/workspace'; import type {Workspace} from '../../../models/workspace';
import type {Environment} from '../../../models/environment'; import type {Environment} from '../../../models/environment';
import * as db from '../../../common/database';
type Props = { type Props = {
activeEnvironment: Environment | null, activeEnvironment: Environment | null,
@ -43,6 +44,7 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent<Props, State> {
colorChangeTimeout: any; colorChangeTimeout: any;
saveTimeout: any; saveTimeout: any;
modal: Modal; modal: Modal;
editorKey: number;
constructor (props: Props) { constructor (props: Props) {
super(props); super(props);
@ -55,6 +57,7 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent<Props, State> {
}; };
this.colorChangeTimeout = null; this.colorChangeTimeout = null;
this.editorKey = 0;
} }
hide () { hide () {
@ -212,6 +215,26 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent<Props, State> {
this._handleChangeEnvironmentColor(environment, null); 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) { async _handleClickColorChange (environment: Environment) {
let el = document.querySelector('#env-color-picker'); let el = document.querySelector('#env-color-picker');
@ -244,7 +267,14 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent<Props, State> {
return; return;
} }
const data = this.environmentEditorRef.getValue(); let data;
try {
data = this.environmentEditorRef.getValue();
} catch (err) {
// Invalid JSON probably
return;
}
const activeEnvironment = this._getActiveEnvironment(); const activeEnvironment = this._getActiveEnvironment();
if (activeEnvironment) { if (activeEnvironment) {
@ -385,7 +415,7 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent<Props, State> {
editorKeyMap={editorKeyMap} editorKeyMap={editorKeyMap}
lineWrapping={lineWrapping} lineWrapping={lineWrapping}
ref={this._setEditorRef} ref={this._setEditorRef}
key={activeEnvironment ? activeEnvironment._id : 'n/a'} key={`${this.editorKey}::${activeEnvironment ? activeEnvironment._id : 'n/a'}`}
environment={activeEnvironment ? activeEnvironment.data : {}} environment={activeEnvironment ? activeEnvironment.data : {}}
didChange={this._didChange} didChange={this._didChange}
render={render} render={render}