Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Simperium sync issues #1598

Merged
merged 1 commit into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@

## Future Release

- Updated Log in and Sign up form to match current styling
- Hovering over a clickable or editable UI element now show the correct cursor for its type
- Added tests to Checkbox component
- Renamed CHANGELOG.md to RELEASE-NOTES.txt
- Open new note automatically upon creation
- Open new note automatically upon creation [1566](https://github.com/Automattic/simplenote-electron/issues/1566)
- Updated colors to use Color Studio, the color palette for Automattic products
### Enhancements

- Open new note automatically upon creation [1582](https://github.com/Automattic/simplenote-electron/pull/1582)
- Updated colors to use Color Studio, the color palette for Automattic products [#1565](https://github.com/Automattic/simplenote-electron/pull/1565)

### Fixes

- Hovering over a clickable or editable UI element now show the correct cursor for its type [#1573](https://github.com/Automattic/simplenote-electron/pull/1573)
- Fixes vertical spacing with nested markdown lists
- Added a GitHub Action to deploy develop and master branches
- Fixes sort order on revision slider when the timestamps don't match the change sequence [#1605](https://github.com/Automattic/simplenote-electron/pull/1605)
- Prevents note corruption when receiving remote updates when local updates are pending
- [#1598](https://github.com/Automattic/simplenote-electron/pull/1598)
- [#1599](https://github.com/Automattic/simplenote-electron/pull/1599)

### Other changes

- Renamed CHANGELOG.md to RELEASE-NOTES.txt [#1576](https://github.com/Automattic/simplenote-electron/pull/1576)
- Added tests to Checkbox component [#1580](https://github.com/Automattic/simplenote-electron/pull/1580)
- Added a GitHub Action to deploy develop and master branches [#1603](https://github.com/Automattic/simplenote-electron/pull/1603)
- Stopped aborting development builds on `eslint` errors [#1594](https://github.com/Automattic/simplenote-electron/pull/1594)

## [v1.8.0](https://github.com/Automattic/simplenote-electron/releases/tag/v1.8.0)

- Updated Log in and Sign up form to match current styling [#1459](https://github.com/Automattic/simplenote-electron/pull/1459)

## [v1.7.0](https://github.com/Automattic/simplenote-electron/releases/tag/v1.7.0) (2019-08-12)

Expand Down
3 changes: 3 additions & 0 deletions lib/app-layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const AppLayout = ({
onNoteClosed,
onNoteOpened,
onUpdateContent,
syncNote,
}) => {
const mainClasses = classNames('app-layout', {
'is-focus-mode': isFocusMode,
Expand Down Expand Up @@ -74,6 +75,7 @@ export const AppLayout = ({
noteBucket={noteBucket}
onNoteClosed={onNoteClosed}
onUpdateContent={onUpdateContent}
syncNote={syncNote}
tags={
get(note, 'data.tags', []) /* flattened to trigger re-render */
}
Expand All @@ -96,6 +98,7 @@ AppLayout.propTypes = {
onNoteClosed: PropTypes.func.isRequired,
onNoteOpened: PropTypes.func.isRequired,
onUpdateContent: PropTypes.func.isRequired,
syncNote: PropTypes.func.isRequired,
};

export default AppLayout;
12 changes: 11 additions & 1 deletion lib/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ export const App = connect(
.on('index', this.onNotesIndex)
.on('update', this.onNoteUpdate)
.on('update', debounce(this.onNotesIndex, 200, { maxWait: 1000 })) // refresh notes list
.on('remove', this.onNoteRemoved);
.on('remove', this.onNoteRemoved)
.beforeNetworkChange(noteId =>
this.props.actions.onNoteBeforeRemoteUpdate({
noteId,
})
);

this.props.preferencesBucket.on('update', this.onLoadPreferences);

Expand Down Expand Up @@ -322,6 +327,10 @@ export const App = connect(
content,
});

syncNote = noteId => {
this.props.noteBucket.touch(noteId);
};

// gets the index of the note located before the currently selected one
getPreviousNoteIndex = note => {
const filteredNotes = filterNotes(this.props.appState);
Expand Down Expand Up @@ -411,6 +420,7 @@ export const App = connect(
onNoteClosed={() => this.setState({ isNoteOpen: false })}
onNoteOpened={() => this.setState({ isNoteOpen: true })}
onUpdateContent={this.onUpdateContent}
syncNote={this.syncNote}
/>
{state.showNoteInfo && <NoteInfo noteBucket={noteBucket} />}
</div>
Expand Down
29 changes: 27 additions & 2 deletions lib/flux/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,40 @@ export const actionMap = new ActionMap({
},
},

/**
* A note is being changed from somewhere else! If the same
* note is also open and being edited, we need to make sure
* any in-memory changes don't get blown away. This is our
* chance to tell node-simperium what we want the note to
* be.
*
* node-simperium will compare these changes with the changes
* from the server and merge them together.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a wonderful beautiful comment

*/
onNoteBeforeRemoteUpdate: {
creator({ noteId }) {
return (dispatch, getState) => {
const state = getState();
if (state.selectedNoteId !== noteId) {
return null;
}
return getState().note.data;

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

};
},
},

updateNoteContent: {
creator({ noteBucket, note, content }) {
return (dispatch, getState) => {
if (note) {
note.data.content = content;
note.data.modificationDate = Math.floor(Date.now() / 1000);

// update the bucket and sync
noteBucket.update(note.id, note.data);
// update the bucket don't sync right away
// as this happens per keystroke when the user is editing
// a note. The NoteEditor notify via props when its time
// to sync via Simperium
noteBucket.update(note.id, note.data, {}, { sync: false });

// Check if note is still selected (to avoid race conditions)
if (get(getState().appState, 'note.id') === note.id) {
Expand Down
30 changes: 23 additions & 7 deletions lib/note-detail/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SimplenoteCompactLogo from '../icons/simplenote-compact';
import renderToNode from './render-to-node';
import toggleTask from './toggle-task';

const saveDelay = 2000;
const syncDelay = 2000;

export class NoteDetail extends Component {
static displayName = 'NoteDetail';
Expand All @@ -23,6 +23,7 @@ export class NoteDetail extends Component {
fontSize: PropTypes.number,
isViewingRevisions: PropTypes.bool.isRequired,
onChangeContent: PropTypes.func.isRequired,
syncNote: PropTypes.func.isRequired,
onNotePrinted: PropTypes.func.isRequired,
note: PropTypes.object,
noteBucket: PropTypes.object.isRequired,
Expand All @@ -41,7 +42,7 @@ export class NoteDetail extends Component {
};

componentWillMount() {
this.queueNoteSave = debounce(this.saveNote, saveDelay);
this.queueNoteSync = debounce(this.syncNote, syncDelay);
document.addEventListener('copy', this.copyRenderedNote, false);
}

Expand All @@ -51,7 +52,7 @@ export class NoteDetail extends Component {
this.props.storeHasFocus(this.hasFocus);

// Ensures note gets saved if user abruptly quits the app
window.addEventListener('beforeunload', this.queueNoteSave.flush);
window.addEventListener('beforeunload', this.queueNoteSync.flush);

if (previewingMarkdown) {
this.updateMarkdown();
Expand All @@ -64,8 +65,14 @@ export class NoteDetail extends Component {

isValidNote = note => note && note.id;

componentWillReceiveProps() {
this.queueNoteSave.flush();
componentWillReceiveProps(nextProps) {
const isEditingNote = get(this.props, ['note', 'id'], false);
if (isEditingNote === false) {
return;
}
if (get(nextProps, ['note', 'id']) !== isEditingNote) {
this.queueNoteSync.flush();
}
}

componentDidUpdate(prevProps) {
Expand All @@ -90,7 +97,7 @@ export class NoteDetail extends Component {
}

componentWillUnmount() {
window.removeEventListener('beforeunload', this.queueNoteSave.flush);
window.removeEventListener('beforeunload', this.queueNoteSync.flush);
document.removeEventListener('copy', this.copyRenderedNote, false);
}

Expand Down Expand Up @@ -153,9 +160,18 @@ export class NoteDetail extends Component {
if (!this.isValidNote(note)) return;

this.props.onChangeContent(note, content);
this.queueNoteSync();
analytics.tracks.recordEvent('editor_note_edited');
};

syncNote = () => {
const { note } = this.props;

if (!this.isValidNote(note)) return;

this.props.syncNote(note.id);
};

storeEditorHasFocus = f => (this.editorHasFocus = f);

storeFocusContentEditor = f => (this.focusContentEditor = f);
Expand Down Expand Up @@ -222,7 +238,7 @@ export class NoteDetail extends Component {
noteId={get(note, 'id', null)}
content={content}
filter={filter}
onChangeContent={this.queueNoteSave}
onChangeContent={this.saveNote}
/>
</div>
)}
Expand Down
2 changes: 2 additions & 0 deletions lib/note-editor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class NoteEditor extends Component {
onUpdateContent: PropTypes.func.isRequired,
revision: PropTypes.object,
setEditorMode: PropTypes.func.isRequired,
syncNote: PropTypes.func.isRequired,
};

static defaultProps = {
Expand Down Expand Up @@ -133,6 +134,7 @@ export class NoteEditor extends Component {
noteBucket={noteBucket}
previewingMarkdown={markdownEnabled && editorMode === 'markdown'}
onChangeContent={this.props.onUpdateContent}
syncNote={this.props.syncNote}
fontSize={fontSize}
/>
{note && !isTrashed && (
Expand Down
Loading