Skip to content

Commit

Permalink
refactor(localSync): make app models work inside electron context #690
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Jul 22, 2021
1 parent 9f2bcd7 commit 662d24e
Show file tree
Hide file tree
Showing 24 changed files with 90 additions and 102 deletions.
2 changes: 1 addition & 1 deletion src/app/core/data-init/data-init.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { WorkContextService } from '../../features/work-context/work-context.ser
import { Store } from '@ngrx/store';
import { allDataWasLoaded } from '../../root-store/meta/all-data-was-loaded.actions';
import { PersistenceService } from '../persistence/persistence.service';
import { ProjectState } from '../../features/project/store/project.reducer';
import { MigrationService } from '../migration/migration.service';
import { loadAllData } from '../../root-store/meta/load-all-data.action';
import { isValidAppData } from '../../imex/sync/is-valid-app-data.util';
import { DataRepairService } from '../data-repair/data-repair.service';
import { ProjectState } from '../../features/project/project.model';

@Injectable({ providedIn: 'root' })
export class DataInitService {
Expand Down
3 changes: 1 addition & 2 deletions src/app/core/data-repair/data-repair.util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { fakeEntityStateFromArray } from '../../util/fake-entity-state-from-arra
import { DEFAULT_TASK, Task } from '../../features/tasks/task.model';
import { createEmptyEntity } from '../../util/create-empty-entity';
import { Tag, TagState } from '../../features/tag/tag.model';
import { ProjectState } from '../../features/project/store/project.reducer';
import { Project } from '../../features/project/project.model';
import { Project, ProjectState } from '../../features/project/project.model';
import { DEFAULT_PROJECT } from '../../features/project/project.const';
import { DEFAULT_TAG, TODAY_TAG } from '../../features/tag/tag.const';

Expand Down
6 changes: 3 additions & 3 deletions src/app/core/migration/legacy-models.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { ProjectState } from '../../features/project/store/project.reducer';
import { GlobalConfigState } from '../../features/config/global-config.model';
import { TaskArchive, TaskState } from '../../features/tasks/task.model';
import { BookmarkState } from '../../features/bookmark/store/bookmark.reducer';
import { NoteState } from '../../features/note/store/note.reducer';
import { Reminder } from '../../features/reminder/reminder.model';
import { MetricState } from '../../features/metric/metric.model';
import { ImprovementState } from '../../features/metric/improvement/improvement.model';
Expand All @@ -11,6 +8,9 @@ import { TaskRepeatCfgState } from '../../features/task-repeat-cfg/task-repeat-c
import { EntityState } from '@ngrx/entity';
import { TaskAttachment } from '../../features/tasks/task-attachment/task-attachment.model';
import { ProjectArchive } from '../../features/project/project-archive.model';
import { ProjectState } from '../../features/project/project.model';
import { BookmarkState } from '../../features/bookmark/bookmark.model';
import { NoteState } from '../../features/note/note.model';

export interface LegacyAppBaseData {
project: ProjectState;
Expand Down
8 changes: 3 additions & 5 deletions src/app/core/migration/legacy-persistence.sevice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ProjectState } from '../../features/project/store/project.reducer';
import {
LS_BOOKMARK_STATE,
LS_GLOBAL_CFG,
Expand Down Expand Up @@ -40,10 +39,8 @@ import {
LegacyPersistenceBaseModel,
LegacyPersistenceForProjectModel,
} from './legacy-models';
import { BookmarkState } from '../../features/bookmark/store/bookmark.reducer';
import { Bookmark } from '../../features/bookmark/bookmark.model';
import { NoteState } from '../../features/note/store/note.reducer';
import { Note } from '../../features/note/note.model';
import { Bookmark, BookmarkState } from '../../features/bookmark/bookmark.model';
import { Note, NoteState } from '../../features/note/note.model';
import { Metric, MetricState } from '../../features/metric/metric.model';
import {
Improvement,
Expand All @@ -57,6 +54,7 @@ import { DatabaseService } from '../persistence/database.service';
import { DEFAULT_PROJECT_ID } from '../../features/project/project.const';
import { Action } from '@ngrx/store';
import { Injectable } from '@angular/core';
import { ProjectState } from '../../features/project/project.model';

@Injectable({
providedIn: 'root',
Expand Down
3 changes: 1 addition & 2 deletions src/app/core/migration/migration.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable } from '@angular/core';
import { PersistenceService } from '../persistence/persistence.service';
import { ProjectState } from '../../features/project/store/project.reducer';
import { EMPTY, from, Observable, of } from 'rxjs';
import { TaskArchive, TaskState } from 'src/app/features/tasks/task.model';
import { Dictionary, EntityState } from '@ngrx/entity';
Expand All @@ -14,7 +13,7 @@ import { LegacyPersistenceService } from './legacy-persistence.sevice';
import { AppDataComplete } from '../../imex/sync/sync.model';
import { initialTaskState } from '../../features/tasks/store/task.reducer';
import { initialTagState } from '../../features/tag/store/tag.reducer';
import { Project } from '../../features/project/project.model';
import { Project, ProjectState } from '../../features/project/project.model';
import { concatMap, map } from 'rxjs/operators';
import { migrateTaskState } from '../../features/tasks/migrate-task-state.util';
import { initialSimpleCounterState } from '../../features/simple-counter/store/simple-counter.reducer';
Expand Down
3 changes: 2 additions & 1 deletion src/app/core/persistence/persistence.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { TestScheduler } from 'rxjs/testing';
import { of } from 'rxjs';
import { createEmptyEntity } from '../../util/create-empty-entity';
import { provideMockStore } from '@ngrx/store/testing';
import { AppDataComplete, DEFAULT_APP_BASE_DATA } from '../../imex/sync/sync.model';
import { AppDataComplete } from '../../imex/sync/sync.model';
import { skip } from 'rxjs/operators';
import { DEFAULT_APP_BASE_DATA } from '../../imex/sync/sync.const';

const testScheduler = new TestScheduler((actual, expected) => {
// asserting the two objects are equal
Expand Down
15 changes: 5 additions & 10 deletions src/app/core/persistence/persistence.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import {
LS_TASK_STATE,
} from './ls-keys.const';
import { GlobalConfigState } from '../../features/config/global-config.model';
import {
projectReducer,
ProjectState,
} from '../../features/project/store/project.reducer';
import { projectReducer } from '../../features/project/store/project.reducer';
import {
ArchiveTask,
Task,
Expand All @@ -35,10 +32,7 @@ import {
AppDataComplete,
AppDataCompleteOptionalSyncModelChange,
AppDataForProjects,
DEFAULT_APP_BASE_DATA,
} from '../../imex/sync/sync.model';
import { BookmarkState } from '../../features/bookmark/store/bookmark.reducer';
import { NoteState } from '../../features/note/store/note.reducer';
import { Reminder } from '../../features/reminder/reminder.model';
import { DatabaseService } from './database.service';
import { DEFAULT_PROJECT_ID } from '../../features/project/project.const';
Expand All @@ -47,7 +41,7 @@ import {
ProjectArchive,
ProjectArchivedRelatedData,
} from '../../features/project/project-archive.model';
import { Project } from '../../features/project/project.model';
import { Project, ProjectState } from '../../features/project/project.model';
import { CompressionService } from '../compression/compression.service';
import {
PersistenceBaseEntityModel,
Expand All @@ -67,8 +61,8 @@ import {
TaskRepeatCfg,
TaskRepeatCfgState,
} from '../../features/task-repeat-cfg/task-repeat-cfg.model';
import { Bookmark } from '../../features/bookmark/bookmark.model';
import { Note } from '../../features/note/note.model';
import { Bookmark, BookmarkState } from '../../features/bookmark/bookmark.model';
import { Note, NoteState } from '../../features/note/note.model';
import { Action, Store } from '@ngrx/store';
import { taskRepeatCfgReducer } from '../../features/task-repeat-cfg/store/task-repeat-cfg.reducer';
import { Tag, TagState } from '../../features/tag/tag.model';
Expand Down Expand Up @@ -102,6 +96,7 @@ import {
migrateMetricState,
migrateObstructionState,
} from '../../features/metric/migrate-metric-states.util';
import { DEFAULT_APP_BASE_DATA } from '../../imex/sync/sync.const';

@Injectable({
providedIn: 'root',
Expand Down
6 changes: 6 additions & 0 deletions src/app/features/bookmark/bookmark.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
DropPasteInput,
DropPasteInputType,
} from '../../core/drop-paste-input/drop-paste.model';
import { EntityState } from '@ngrx/entity';

export type BookmarkType = DropPasteInputType;

Expand All @@ -10,3 +11,8 @@ export interface BookmarkCopy extends DropPasteInput {
}

export type Bookmark = Readonly<BookmarkCopy>;

export interface BookmarkState extends EntityState<Bookmark> {
// additional entities state properties
isShowBookmarks: boolean;
}
3 changes: 1 addition & 2 deletions src/app/features/bookmark/bookmark.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable } from '@angular/core';
import { select, Store } from '@ngrx/store';
import {
BookmarkState,
initialBookmarkState,
selectAllBookmarks,
selectIsShowBookmarkBar,
Expand All @@ -17,7 +16,7 @@ import {
UpdateBookmark,
} from './store/bookmark.actions';
import { Observable } from 'rxjs';
import { Bookmark } from './bookmark.model';
import { Bookmark, BookmarkState } from './bookmark.model';
import * as shortid from 'shortid';
import { DialogEditBookmarkComponent } from './dialog-edit-bookmark/dialog-edit-bookmark.component';
import { MatDialog } from '@angular/material/dialog';
Expand Down
3 changes: 1 addition & 2 deletions src/app/features/bookmark/store/bookmark.actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Action } from '@ngrx/store';
import { Update } from '@ngrx/entity';
import { Bookmark } from '../bookmark.model';
import { BookmarkState } from './bookmark.reducer';
import { Bookmark, BookmarkState } from '../bookmark.model';

export enum BookmarkActionTypes {
'LoadBookmarkState' = '[Bookmark] Load Bookmark State',
Expand Down
3 changes: 2 additions & 1 deletion src/app/features/bookmark/store/bookmark.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { Actions, Effect, ofType } from '@ngrx/effects';
import { first, switchMap, tap } from 'rxjs/operators';
import { select, Store } from '@ngrx/store';
import { BookmarkActionTypes } from './bookmark.actions';
import { BookmarkState, selectBookmarkFeatureState } from './bookmark.reducer';
import { selectBookmarkFeatureState } from './bookmark.reducer';
import { PersistenceService } from '../../../core/persistence/persistence.service';
import { combineLatest, Observable } from 'rxjs';
import { WorkContextService } from '../../work-context/work-context.service';
import { BookmarkState } from '../bookmark.model';

@Injectable()
export class BookmarkEffects {
Expand Down
9 changes: 2 additions & 7 deletions src/app/features/bookmark/store/bookmark.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
import { createEntityAdapter, EntityAdapter } from '@ngrx/entity';
import {
AddBookmark,
BookmarkActions,
Expand All @@ -8,16 +8,11 @@ import {
ReorderBookmarks,
UpdateBookmark,
} from './bookmark.actions';
import { Bookmark } from '../bookmark.model';
import { Bookmark, BookmarkState } from '../bookmark.model';
import { createFeatureSelector, createSelector } from '@ngrx/store';

export const BOOKMARK_FEATURE_NAME = 'bookmark';

export interface BookmarkState extends EntityState<Bookmark> {
// additional entities state properties
isShowBookmarks: boolean;
}

export const adapter: EntityAdapter<Bookmark> = createEntityAdapter<Bookmark>();
export const selectBookmarkFeatureState =
createFeatureSelector<BookmarkState>(BOOKMARK_FEATURE_NAME);
Expand Down
4 changes: 4 additions & 0 deletions src/app/features/note/note.model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EntityState } from '@ngrx/entity';

export interface Note {
id: string;
content: string;
Expand All @@ -7,3 +9,5 @@ export interface Note {
created: number;
modified: number;
}

export type NoteState = EntityState<Note>;
9 changes: 2 additions & 7 deletions src/app/features/note/note.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Note } from './note.model';
import { Note, NoteState } from './note.model';
import { select, Store } from '@ngrx/store';
import {
addNote,
Expand All @@ -10,12 +10,7 @@ import {
updateNoteOrder,
} from './store/note.actions';
import * as shortid from 'shortid';
import {
initialNoteState,
NoteState,
selectAllNotes,
selectNoteById,
} from './store/note.reducer';
import { initialNoteState, selectAllNotes, selectNoteById } from './store/note.reducer';
import { PersistenceService } from '../../core/persistence/persistence.service';
import { take } from 'rxjs/operators';
import { createFromDrop } from '../../core/drop-paste-input/drop-paste-input';
Expand Down
3 changes: 1 addition & 2 deletions src/app/features/note/store/note.actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createAction, props } from '@ngrx/store';
import { Update } from '@ngrx/entity';
import { Note } from '../note.model';
import { NoteState } from './note.reducer';
import { Note, NoteState } from '../note.model';

export const loadNoteState = createAction(
'[Note] Load Note State',
Expand Down
3 changes: 2 additions & 1 deletion src/app/features/note/store/note.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { PersistenceService } from '../../../core/persistence/persistence.servic
import { select, Store } from '@ngrx/store';
import { first, switchMap, tap } from 'rxjs/operators';
import { addNote, deleteNote, updateNote, updateNoteOrder } from './note.actions';
import { NoteState, selectNoteFeatureState } from './note.reducer';
import { selectNoteFeatureState } from './note.reducer';
import { WorkContextService } from '../../work-context/work-context.service';
import { combineLatest, Observable } from 'rxjs';
import { NoteState } from '../note.model';

@Injectable()
export class NoteEffects {
Expand Down
6 changes: 2 additions & 4 deletions src/app/features/note/store/note.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
import { Note } from '../note.model';
import { createEntityAdapter, EntityAdapter } from '@ngrx/entity';
import { Note, NoteState } from '../note.model';
import {
addNote,
addNotes,
Expand All @@ -21,8 +21,6 @@ import {
on,
} from '@ngrx/store';

export type NoteState = EntityState<Note>;

export const adapter: EntityAdapter<Note> = createEntityAdapter<Note>();

export const initialNoteState: NoteState = adapter.getInitialState({});
Expand Down
3 changes: 1 addition & 2 deletions src/app/features/project/migrate-projects-state.util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ProjectState } from './store/project.reducer';
import { Dictionary } from '@ngrx/entity';
import { Project } from './project.model';
import { Project, ProjectState } from './project.model';
import { DEFAULT_PROJECT, PROJECT_MODEL_VERSION } from './project.const';
import { DEFAULT_ISSUE_PROVIDER_CFGS } from '../issue/issue.const';
import {
Expand Down
4 changes: 2 additions & 2 deletions src/app/features/project/project-archive.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { NoteState } from '../note/store/note.reducer';
import { BookmarkState } from '../bookmark/store/bookmark.reducer';
import { Task, TaskState } from '../tasks/task.model';
import { EntityState } from '@ngrx/entity';
import { TaskAttachment } from '../tasks/task-attachment/task-attachment.model';
Expand All @@ -8,6 +6,8 @@ import { MetricState } from '../metric/metric.model';
import { ImprovementState } from '../metric/improvement/improvement.model';
import { ObstructionState } from '../metric/obstruction/obstruction.model';
import { Project } from './project.model';
import { BookmarkState } from '../bookmark/bookmark.model';
import { NoteState } from '../note/note.model';

export interface ProjectArchivedRelatedData {
note?: NoteState;
Expand Down
6 changes: 6 additions & 0 deletions src/app/features/project/project.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
WorkContextAdvancedCfgKey,
WorkContextCommon,
} from '../work-context/work-context.model';
import { EntityState } from '@ngrx/entity';
import { MODEL_VERSION_KEY } from '../../app.constants';

export type RoundTimeOption = '5M' | 'QUARTER' | 'HALF' | 'HOUR' | null;

Expand Down Expand Up @@ -32,3 +34,7 @@ export type ProjectCfgFormKey =
| IssueProviderKey
| 'basic'
| 'theme';

export interface ProjectState extends EntityState<Project> {
[MODEL_VERSION_KEY]?: number;
}
8 changes: 2 additions & 6 deletions src/app/features/project/store/project.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createEntityAdapter, EntityAdapter, EntityState, Update } from '@ngrx/entity';
import { Project } from '../project.model';
import { createEntityAdapter, EntityAdapter, Update } from '@ngrx/entity';
import { Project, ProjectState } from '../project.model';
import { ProjectActions, ProjectActionTypes } from './project.actions';
import { createFeatureSelector, createSelector } from '@ngrx/store';
import { FIRST_PROJECT, PROJECT_MODEL_VERSION } from '../project.const';
Expand Down Expand Up @@ -58,10 +58,6 @@ import { CaldavCfg } from '../../issue/providers/caldav/caldav.model';
export const PROJECT_FEATURE_NAME = 'projects';
const WORK_CONTEXT_TYPE: WorkContextType = WorkContextType.PROJECT;

export interface ProjectState extends EntityState<Project> {
[MODEL_VERSION_KEY]?: number;
}

export const projectAdapter: EntityAdapter<Project> = createEntityAdapter<Project>();

// SELECTORS
Expand Down
31 changes: 31 additions & 0 deletions src/app/imex/sync/sync.const.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import { initialProjectState } from '../../features/project/store/project.reducer';
import { DEFAULT_GLOBAL_CONFIG } from '../../features/config/default-global-config.const';
import { initialTaskState } from '../../features/tasks/store/task.reducer';
import { initialTagState } from '../../features/tag/store/tag.reducer';
import { initialSimpleCounterState } from '../../features/simple-counter/store/simple-counter.reducer';
import { createEmptyEntity } from '../../util/create-empty-entity';
import { TaskArchive } from '../../features/tasks/task.model';
import { initialTaskRepeatCfgState } from '../../features/task-repeat-cfg/store/task-repeat-cfg.reducer';
import { initialMetricState } from '../../features/metric/store/metric.reducer';
import { initialImprovementState } from '../../features/metric/improvement/store/improvement.reducer';
import { initialObstructionState } from '../../features/metric/obstruction/store/obstruction.reducer';
import { AppBaseData } from './sync.model';

export const SYNC_INITIAL_SYNC_TRIGGER = 'INITIAL_SYNC_TRIGGER';
export const SYNC_DEFAULT_AUDIT_TIME = 10000;

Expand All @@ -6,3 +19,21 @@ export const SYNC_BEFORE_GOING_TO_SLEEP_THROTTLE_TIME = 1000 * 60 * 5;

export const SYNC_BEFORE_CLOSE_ID = 'SYNC_BEFORE_CLOSE_ID';
export const SYNC_MIN_INTERVAL = 5000;

export const DEFAULT_APP_BASE_DATA: AppBaseData = {
project: initialProjectState,
archivedProjects: {},
globalConfig: DEFAULT_GLOBAL_CONFIG,
reminders: [],

task: initialTaskState,
tag: initialTagState,
simpleCounter: initialSimpleCounterState,
taskArchive: createEmptyEntity() as TaskArchive,
taskRepeatCfg: initialTaskRepeatCfgState,

// metric
metric: initialMetricState,
improvement: initialImprovementState,
obstruction: initialObstructionState,
};
Loading

0 comments on commit 662d24e

Please sign in to comment.