Skip to content

Commit

Permalink
modify event object
Browse files Browse the repository at this point in the history
  • Loading branch information
idleberg committed Oct 20, 2023
1 parent 072bfb8 commit 1cfb809
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/porridge-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export class PorridgeDB {
* ```
*/
public async setItem(keyName: string, keyValue: unknown, options: WebPorridge.StorageOptions = {}): Promise<void> {
const oldValue = await this.getItem(keyName, options);

if (options?.prop?.length) {
const item = await this.getItem(keyName) || {};
setProperty(item, options.prop, keyValue);
Expand All @@ -90,7 +92,8 @@ export class PorridgeDB {
eventDispatcher(this.eventName, {
store: storageType,
key: keyName,
value: keyValue
oldValue: oldValue,
newValue: keyValue
});

return await setItemIdb(keyName, newValue, this.customStore);
Expand Down Expand Up @@ -137,6 +140,8 @@ export class PorridgeDB {
* ```
*/
public async removeItem(keyName: string, options?: WebPorridge.StorageOptions): Promise<void> {
const oldValue = await this.getItem(keyName, options);

if (options?.prop?.length) {
const item = await this.getItem(keyName) || {};
deleteProperty(item, options.prop);
Expand All @@ -147,7 +152,8 @@ export class PorridgeDB {
eventDispatcher(this.eventName, {
store: storageType,
key: keyName,
value: null
oldValue: oldValue,
newValue: null
});

return await removeItemIdb(keyName, this.customStore);
Expand Down Expand Up @@ -193,7 +199,8 @@ export class PorridgeDB {
public async clear(): Promise<void> {
eventDispatcher(this.eventName, {
store: storageType,
value: null
oldValue: null,
newValue: null
});

return await clear(this.customStore);
Expand Down
13 changes: 10 additions & 3 deletions src/porridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export class Porridge {
* ```
*/
public setItem(keyName: string, keyValue: unknown, options?: WebPorridge.StorageOptions): void {
const oldValue = this.getItem(keyName, options);

if (options?.prop?.length) {
const item = this.getItem(keyName) || {};
setProperty(item, options.prop, keyValue);
Expand All @@ -87,7 +89,8 @@ export class Porridge {
eventDispatcher(this.eventName, {
store: this.store,
key: keyName,
value: keyValue
oldValue: oldValue,
newValue: keyValue
});

return (<any>globalThis)[this.store].setItem(keyName, JSON.stringify(newValue));
Expand Down Expand Up @@ -142,6 +145,8 @@ export class Porridge {
* ```
*/
public removeItem(keyName: string, options?: WebPorridge.StorageOptions): void {
const oldValue = this.getItem(keyName, options);

if (options?.prop?.length) {
const item = this.getItem(keyName) || {};
deleteProperty(item, options.prop);
Expand All @@ -152,7 +157,8 @@ export class Porridge {
eventDispatcher(this.eventName, {
store: this.store,
key: keyName,
value: null
oldValue: oldValue,
newValue: null
});

return (<any>globalThis)[this.store].removeItem(keyName);
Expand Down Expand Up @@ -191,7 +197,8 @@ export class Porridge {
public clear(): void {
eventDispatcher(this.eventName, {
store: this.store,
value: null
oldValue: null,
newValue: null
});

return (<any>globalThis)[this.store].clear();
Expand Down

0 comments on commit 1cfb809

Please sign in to comment.