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