Skip to content

Commit

Permalink
Create persistent preferences bucket, adding logic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Sep 8, 2020
1 parent 153779c commit 7177d7e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 50 deletions.
5 changes: 5 additions & 0 deletions lib/state/action-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ export type PreferencesBucketUpdate = Action<
'PREFERENCES_BUCKET_UPDATE',
{ id: T.EntityId; data: T.Preferences }
>;
export type RemoteAnalyticsUpdate = Action<
'REMOTE_ANALYTICS_UPDATE',
{ allowAnalytics: boolean }
>;
export type RemoteNoteUpdate = Action<
'REMOTE_NOTE_UPDATE',
{ noteId: T.EntityId; note: T.Note; remoteInfo?: RemoteInfo<T.Note> }
Expand Down Expand Up @@ -338,6 +342,7 @@ export type ActionType =
| PublishNote
| ReallyLogout
| RecordEvent
| RemoteAnalyticsUpdate
| RemoteNoteUpdate
| RemoteNoteDeleteForever
| RemoteTagDelete
Expand Down
1 change: 1 addition & 0 deletions lib/state/analytics/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const middleware: S.Middleware = (store) => {
}

switch (action.type) {
case 'REMOTE_ANALYTICS_UPDATE':
case 'SET_ANALYTICS':
// Global to be checked in analytics.tracks.recordEvent()
window.analyticsEnabled = action.allowAnalytics;
Expand Down
22 changes: 14 additions & 8 deletions lib/state/data/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export const analyticsAllowed: A.Reducer<boolean | null> = (
state = null,
action
) => {
return false;
// switch (action.type) {
// case 'SET_ANALYTICS':
// return action.allowAnalytics;
//
// default:
// return state;
// }
switch (action.type) {
case 'REMOTE_ANALYTICS_UPDATE':
case 'SET_ANALYTICS':
return action.allowAnalytics;

default:
return state;
}
};

const modified = <Entity extends { modificationDate: number }>(
Expand Down Expand Up @@ -310,6 +310,12 @@ export const preferences: A.Reducer<Map<T.EntityId, T.Preferences>> = (
action
) => {
switch (action.type) {
case 'SET_ANALYTICS':
return new Map(state).set('preferences-key', {
...(state.get('preferences-key') ?? {}),
analytics_enabled: action.allowAnalytics,
});

case 'PREFERENCES_BUCKET_REMOVE': {
const next = new Map(state);
return next.delete(action.id) ? next : state;
Expand Down
66 changes: 25 additions & 41 deletions lib/state/simperium/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import actions from '../actions';
import { BucketQueue } from './functions/bucket-queue';
import { NoteBucket } from './functions/note-bucket';
// import { NoteDoctor } from './functions/note-doctor';
// import { PreferencesBucket } from './functions/preferences-bucket';
import { PreferencesBucket } from './functions/preferences-bucket';
import { ReduxGhost } from './functions/redux-ghost';
import { TagBucket } from './functions/tag-bucket';
import { getUnconfirmedChanges } from './functions/unconfirmed-changes';
Expand All @@ -24,7 +24,7 @@ const debug = debugFactory('simperium-middleware');

type Buckets = {
note: T.Note;
// preferences: T.Preferences;
preferences: T.Preferences;
tag: T.Tag;
};

Expand All @@ -42,9 +42,9 @@ export const initSimperium = (
case 'note':
return new NoteBucket(store);

// case 'preferences':
// return new PreferencesBucket(store);
//
case 'preferences':
return new PreferencesBucket(store);

case 'tag':
return new TagBucket(store);
}
Expand Down Expand Up @@ -149,25 +149,21 @@ export const initSimperium = (
})
);

// const preferencesBucket = client.bucket('preferences');
// preferencesBucket.channel.on('update', (entityId, updatedEntity) => {
// if ('preferences-key' !== entityId) {
// return;
// }
//
// if (
// !!updatedEntity.analytics_enabled !== getState().data.analyticsAllowed
// ) {
// dispatch({
// type: 'SET_ANALYTICS',
// allowAnalytics: !!updatedEntity.analytics_enabled,
// });
// }
// });

// preferencesBucket.channel.on('ready', () =>
// preferencesBucket.channel.sendIndexRequest()
// );
const preferencesBucket = client.bucket('preferences');
preferencesBucket.channel.on('update', (entityId, updatedEntity) => {
if ('preferences-key' !== entityId) {
return;
}

if (
!!updatedEntity.analytics_enabled !== getState().data.analyticsAllowed
) {
dispatch({
type: 'REMOTE_ANALYTICS_UPDATE',
allowAnalytics: !!updatedEntity.analytics_enabled,
});
}
});

if (createWelcomeNote) {
import(
Expand Down Expand Up @@ -197,6 +193,10 @@ export const initSimperium = (
const queueTagUpdate = (tagHash: T.TagHash, delay = 20) =>
tagQueue.add(tagHash, Date.now() + delay);

const preferencesQueue = new BucketQueue(preferencesBucket);
const queuePreferencesUpdate = (entityId: T.EntityId, delay = 20) =>
preferencesQueue.add(entityId, Date.now() + delay);

if ('production' !== process.env.NODE_ENV) {
// window.preferencesBucket = preferencesBucket;
window.noteBucket = noteBucket;
Expand Down Expand Up @@ -347,23 +347,7 @@ export const initSimperium = (
return result;

case 'SET_ANALYTICS':
// preferencesBucket.get('preferences-key').then((preferences) => {
// if (
// preferences.data &&
// !!preferences.data.analytics_enabled === action.allowAnalytics
// ) {
// return;
// }
//
// preferencesBucket.update(
// 'preferences-key',
// {
// ...preferences.data,
// analytics_enabled: action.allowAnalytics,
// },
// { sync: false }
// );
// });
queuePreferencesUpdate('preferences-key' as T.EntityId);
return result;

case 'TRASH_TAG': {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"email": "[email protected]"
},
"productName": "Simplenote",
"version": "2.0.0-rc1",
"version": "2.0.0-rc2",
"main": "desktop/index.js",
"license": "GPL-2.0",
"homepage": "https://simplenote.com",
Expand Down

0 comments on commit 7177d7e

Please sign in to comment.