Skip to content

Commit

Permalink
Refactor loading and processing of actions
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Sep 6, 2024
1 parent d3b13b7 commit 29cb855
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
19 changes: 9 additions & 10 deletions packages/js-sdk/src/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,10 @@ export class ConsentList {
/*
* Process actions and update internal consent list
*/
processActions(actionsMap?: ActionsMap) {
processActions(actionsMap: ActionsMap) {
const entries: ConsentListEntry[] = []
// actions to process
const actions = actionsMap
? Array.from(actionsMap.values())
: this.client.keystore.getPrivatePreferences()

// processing all actions, reset consent list
if (!actionsMap) {
this.reset()
}
const actions = Array.from(actionsMap.values())

// update the consent list
actions.forEach((action) => {
Expand Down Expand Up @@ -259,7 +252,13 @@ export class ConsentList {
.filter(([timestampNs]) => Boolean(timestampNs)) as [string, Uint8Array][]

// decode messages and save them to keystore
const actionsMap = await this.decodeMessages(new Map(messageEntries))
await this.decodeMessages(new Map(messageEntries))

// get all actions from keystore
const actionsMap = this.client.keystore.getPrivatePreferences()

// reset consent list
this.reset()

// process actions and update consent list
return this.processActions(actionsMap)
Expand Down
4 changes: 2 additions & 2 deletions packages/js-sdk/src/keystore/privatePreferencesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ export class PrivatePreferencesStore {
})
}

get actions(): privatePreferences.PrivatePreferencesAction[] {
get actions(): ActionsMap {
// sort actions by their keys (timestamps) in ascending order
const sortedActions = new Map(
[...this.actionsMap.entries()].sort(
(a, b) =>
fromNanoString(a[0])!.getTime() - fromNanoString(b[0])!.getTime()
)
)
return Array.from(sortedActions.values())
return sortedActions
}

lookup(key: string): privatePreferences.PrivatePreferencesAction | undefined {
Expand Down
2 changes: 1 addition & 1 deletion packages/js-sdk/src/keystore/rpcDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type PrivatePreferenceKeystoreMethods = {
createPrivatePreference: (
action: privatePreferences.PrivatePreferencesAction
) => Promise<PublishParams[]>
getPrivatePreferences: () => privatePreferences.PrivatePreferencesAction[]
getPrivatePreferences: () => ActionsMap
getPrivatePreferencesTopic: () => Promise<string>
savePrivatePreferences: (data: ActionsMap) => Promise<void>
}
Expand Down

0 comments on commit 29cb855

Please sign in to comment.