Skip to content

Commit

Permalink
Add id to schedule.
Browse files Browse the repository at this point in the history
  • Loading branch information
saul-jb committed Mar 5, 2024
1 parent 9f88c31 commit 46d921d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
32 changes: 26 additions & 6 deletions packages/daemon/src/modules/scheduler/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,34 @@ export class Schedule {
this.id = id
}

async put (entry: Entry): Promise<void> {
async get (id: string): Promise<Entry | null> {
const index = await this.database.store.latest()
const data = await this.database.store.selectors.get(index)(id)

if (data == null) {
return null
}

const ee = EncodedEntry.parse(data)

if (ee == null) {
return null
}

return decodeEntry(ee)
}

async put (entry: Entry): Promise<string> {
const key = this.makeKey()
const encodedEntry: EncodedEntry = encodeEntry(entry)
const op = this.database.store.creators.put(this.key, encodedEntry)
const op = this.database.store.creators.put(key, encodedEntry)

await this.database.replica.write(op)

return key
}

async * all (): AsyncGenerator<Entry> {
async * all (): AsyncGenerator<Entry & { id: string }> {
const index = await this.database.store.latest()

for await (const pair of index.query({ prefix: Path.join('/', SCHEDULE_KEY) })) {
Expand All @@ -41,11 +61,11 @@ export class Schedule {

const entry = decodeEntry(encodedEntry)

yield entry
yield { ...entry, id: pair.key.toString() }
}
}

private get key (): string {
private makeKey (): string {
const write = Date.now()

if (this.write <= write) {
Expand All @@ -55,6 +75,6 @@ export class Schedule {
this.write = write
}

return Path.join('/', SCHEDULE_KEY, uint8ArrayToString(this.id), `${this.write}`, `${this.sequence}`)
return Path.join('/', SCHEDULE_KEY, uint8ArrayToString(this.id, 'base58btc'), `${this.write}`, `${this.sequence}`)
}
}
1 change: 1 addition & 0 deletions packages/rpc-interfaces/src/rpc/get-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const Params = z.object({
export type Params = z.input<typeof Params>

export const Return = z.array(z.object({
id: z.string(),
from: z.number(),
to: z.number(),
context: z.record(z.any()),
Expand Down

0 comments on commit 46d921d

Please sign in to comment.