-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add base class for Stores (#2493)
* refactor: add base abstract classes for kv and event stores * fix: revert filename change for macos e2e github action
- Loading branch information
Showing
14 changed files
with
146 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import KeyValueStore from 'orbit-db-kvstore' | ||
import Store from 'orbit-db-store' | ||
import EventStore from 'orbit-db-eventstore' | ||
import { EventEmitter } from 'events' | ||
import { createLogger } from '../common/logger' | ||
|
||
const logger = createLogger('store') | ||
|
||
abstract class StoreBase<V, S extends KeyValueStore<V> | EventStore<V>> extends EventEmitter { | ||
protected abstract store: S | undefined | ||
|
||
getStore() { | ||
if (!this.store) { | ||
throw new Error('Store not initialized') | ||
} | ||
return this.store | ||
} | ||
|
||
getAddress(): Store['address'] { | ||
return this.getStore().address | ||
} | ||
|
||
async close(): Promise<void> { | ||
logger.info('Closing', this.getAddress().path) | ||
await this.store?.close() | ||
logger.info('Closed', this.getAddress().path) | ||
} | ||
|
||
abstract init(): Promise<void> | ||
abstract clean(): void | ||
} | ||
|
||
export abstract class KeyValueStoreBase<V> extends StoreBase<V, KeyValueStore<V>> { | ||
protected store: KeyValueStore<V> | undefined | ||
abstract setEntry(key: string, value: V): Promise<V> | ||
abstract getEntry(key?: string): V | null | ||
} | ||
|
||
export abstract class EventStoreBase<V> extends StoreBase<V, EventStore<V>> { | ||
protected store: EventStore<V> | undefined | ||
abstract addEntry(value: V): Promise<string> | ||
abstract getEntries(): Promise<V[]> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.