|
|
@@ -1,15 +1,15 @@ |
|
|
|
import Storage, { StorageMeta, OutOfSyncError } from '../../services/storages/Storage' |
|
|
|
import Storage, { Collection, OutOfSyncError } from '../core/storage' |
|
|
|
import Engine from './Engine' |
|
|
|
|
|
|
|
export default class LocalStorage<T> implements Storage<T> { |
|
|
|
private readonly engine: Engine<StorageMeta<T>> |
|
|
|
private readonly engine: Engine<Collection<T>> |
|
|
|
|
|
|
|
constructor( |
|
|
|
private readonly ownerId: string, |
|
|
|
private readonly storageId: string, |
|
|
|
private readonly getItemId = item => item['id'], |
|
|
|
) { |
|
|
|
this.engine = new Engine<StorageMeta<T>>(window.localStorage) |
|
|
|
this.engine = new Engine<Collection<T>>(window.localStorage) |
|
|
|
} |
|
|
|
|
|
|
|
private getMeta() { |
|
|
@@ -31,7 +31,7 @@ export default class LocalStorage<T> implements Storage<T> { |
|
|
|
async saveItem(newItem: T) { |
|
|
|
const oldMeta = this.getMeta() |
|
|
|
const isExistingItem = oldMeta.items.some(this.existenceCheck(newItem)) |
|
|
|
const newMeta: StorageMeta<T> = { |
|
|
|
const newMeta: Collection<T> = { |
|
|
|
items: isExistingItem |
|
|
|
? oldMeta.items.map(oldItem => this.existenceCheck(newItem)(oldItem) ? newItem : oldItem) |
|
|
|
: [...oldMeta.items, newItem], |
|
|
@@ -44,7 +44,7 @@ export default class LocalStorage<T> implements Storage<T> { |
|
|
|
async deleteItem(newItem: T) { |
|
|
|
const oldMeta = this.getMeta() |
|
|
|
const newItems = oldMeta.items.filter(oldItem => !this.existenceCheck(newItem)(oldItem)) |
|
|
|
const newMeta: StorageMeta<T> = { |
|
|
|
const newMeta: Collection<T> = { |
|
|
|
items: newItems, |
|
|
|
lastModifiedBy: this.ownerId, |
|
|
|
lastModifiedAt: new Date(), |