Skip to content

Commit

Permalink
use emptyAccountData factory
Browse files Browse the repository at this point in the history
  • Loading branch information
wighawag committed Nov 29, 2023
1 parent fef82df commit a628cad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 7 additions & 1 deletion web/src/lib/account/account-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ function fromOnChainActionToPendingTransaction(

export class JollyRogerAccountData extends BaseAccountHandler<AccountData, JollyRogerMetadata> {
constructor() {
super('jolly-roger', {onchainActions: {}}, fromOnChainActionToPendingTransaction);
super(
'jolly-roger',
() => ({
onchainActions: {},
}),
fromOnChainActionToPendingTransaction,
);
}

_merge(
Expand Down
12 changes: 5 additions & 7 deletions web/src/lib/account/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ export abstract class BaseAccountHandler<

private accountDB?: AccountDB<T>;
private unsubscribeFromSync: (() => void) | undefined;
protected readonly $data: T;
protected $data: T;
protected _onchainActions: Writable<OnChainActions<Metadata>>;
public readonly onchainActions: Readable<OnChainActions<Metadata>>;

constructor(
protected dbName: string,
protected emptyAccountData: T,
protected emptyAccountData: () => T,
protected fromOnChainActionToPendingTransaction: (
hash: `0x${string}`,
onchainAction: OnChainAction<Metadata>,
) => PendingTransaction,
) {
this.emitter = initEmitter();
this.$data = emptyAccountData;
this.$data = this.emptyAccountData();
this._onchainActions = writable(this.$data.onchainActions);
this.onchainActions = {
subscribe: this._onchainActions.subscribe,
Expand Down Expand Up @@ -96,9 +96,7 @@ export abstract class BaseAccountHandler<
this.accountDB = undefined;

// delete all
for (const hash of Object.keys(this.$data.onchainActions)) {
delete (this.$data.onchainActions as any)[hash];
}
this.$data = this.emptyAccountData();

this._onchainActions.set(this.$data.onchainActions);

Expand Down Expand Up @@ -174,7 +172,7 @@ export abstract class BaseAccountHandler<
this.accountDB = new AccountDB(this.dbName, info, this._merge, syncInfo);

this.unsubscribeFromSync = this.accountDB.subscribe(this._onSync);
return (await this.accountDB.requestSync(true)) || this.emptyAccountData;
return (await this.accountDB.requestSync(true)) || this.emptyAccountData();
}

_onSync() {}
Expand Down

0 comments on commit a628cad

Please sign in to comment.