feat(dexie): add more stuff

This commit is contained in:
Johannes Millan 2019-10-30 23:26:34 +01:00
parent 3dabb43b88
commit 9d4ea12a58
2 changed files with 27 additions and 16 deletions

View file

@ -3,7 +3,7 @@ import 'dexie-observable';
import {Injectable} from '@angular/core';
import {Task} from '../../features/tasks/task.model';
import {from, merge, Observable, Subject} from 'rxjs';
import {filter, switchMap} from 'rxjs/operators';
import {debounceTime, filter, switchMap} from 'rxjs/operators';
import {IDatabaseChange} from 'dexie-observable/api';
import {TaskClass} from './task.class';
@ -15,7 +15,7 @@ const storeCfg = [{
name: 'tasks',
// tslint:disable-next-line
// fields: '++id, projectId, title, *subTaskIds, timeSpentOnDay, timeSpent, timeEstimate, created, isDone, notes, &issueId, issueType, parentId, attachmentIds, &reminderId, &repeatCfgId, _showSubTasksMode, _currentTab'
fields: '++id, projectId, title, created, parentId, *subTaskIds',
fields: '++id, order, projectId, title, created, parentId, *subTaskIds',
c: TaskClass,
}];
@ -25,9 +25,12 @@ export type TaskTable = Dexie.Table<Task, string>;
providedIn: 'root',
})
export class DexieService extends Dexie {
tasks: Ta;
tasks: any;
private _refresh$ = new Subject<IDatabaseChange[]>();
private _refreshDebounced$ = this._refresh$.pipe(
// debounceTime(10),
);
constructor() {
super('SUP_DEXIE');
@ -60,7 +63,7 @@ export class DexieService extends Dexie {
return merge(
from(query(this.tasks)),
this._refresh$.pipe(
this._refreshDebounced$.pipe(
filter(changes => this._isRefreshForTable(Tables.tasks, changes)),
switchMap(() => query(this.tasks))
)

View file

@ -9,7 +9,6 @@ import {
AddTask,
AddTaskReminder,
AddTimeSpent,
DeleteTask,
FocusLastActiveTask,
FocusTask,
LoadTaskState,
@ -64,7 +63,6 @@ import {
selectTasksWithMissingIssueData,
selectTasksWorkedOnOrDoneFlat,
selectTaskWithSubTasksByRepeatConfigId,
selectTodaysDoneTasksWithSubTasks,
selectTodaysTasksWithSubTasks,
selectTotalTimeWorkedOnTodaysTasks
} from './store/task.selectors';
@ -136,12 +134,11 @@ export class TaskService {
);
undoneTasks$: Observable<Task[]> = this._dexieService.getTasks$(
(taskTable: TaskTable) => taskTable.filter(task => !task.isDone).reverse().sortBy('created')
(taskTable: TaskTable) => taskTable.filter(task => !task.isDone && !task.parentId).reverse().sortBy('order')
);
doneTasks$: Observable<TaskWithSubTasks[]> = this._store.pipe(
select(selectTodaysDoneTasksWithSubTasks),
shareReplay(1),
doneTasks$: Observable<TaskWithSubTasks[]> = this._dexieService.getTasks$(
(taskTable: TaskTable) => taskTable.filter(task => task.isDone && !task.parentId).reverse().sortBy('order')
);
allRepeatableTasks$: Observable<TaskWithSubTasks[]> = this._store.pipe(
@ -282,7 +279,7 @@ export class TaskService {
isAddToBottom = false,
) {
const task = this.createNewTaskWithDefaults(title, additionalFields);
this._dexieService.tasks.put(task);
this._dexieService.tasks.add(task);
// this._store.dispatch(new AddTask({
// task: this.createNewTaskWithDefaults(title, additionalFields),
// isAddToBacklog,
@ -308,14 +305,25 @@ export class TaskService {
}
remove(task: TaskWithSubTasks) {
this._store.dispatch(new DeleteTask({task}));
// this._store.dispatch(new DeleteTask({task}));
if (task.subTasks) {
this._dexieService.tasks.bulkDelete([task.id, ...task.subTaskIds]);
} else {
// this._dexieService.tasks.delete(task.id);
this._dexieService.tasks.bulkDelete([task.id]);
}
}
update(id: string, changedFields: Partial<Task>) {
this._store.dispatch(new UpdateTask({
task: {id, changes: this._shortSyntax(changedFields) as Partial<Task>}
}));
async update(id: string, changedFields: Partial<Task>) {
this._dexieService.tasks.put({
id,
...(await this._dexieService.tasks.get(id)),
...changedFields,
});
// this._store.dispatch(new UpdateTask({
// task: {id, changes: this._shortSyntax(changedFields) as Partial<Task>}
// }));
}
// NOTE: side effects are not executed!!!