Skip to content

Commit

Permalink
fix: set connection password once
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Apr 12, 2023
1 parent 0e137ba commit 675b713
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 34 deletions.
15 changes: 2 additions & 13 deletions src/chrome/offscreen/message-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createMessage } from 'chrome/messages/create-message'
import { ConnectorClient } from 'connector/connector-client'
import { MessagesRouter } from 'message-router'
import { errAsync, okAsync, ResultAsync } from 'neverthrow'
import { errAsync, okAsync } from 'neverthrow'
import { Queue } from 'queues/queue'
import { AppLogger } from 'utils/logger'
import {
Expand Down Expand Up @@ -47,18 +47,7 @@ export const OffscreenMessageHandler = (input: {
const { interactionId } = message.data
return messageRouter
.add(tabId!, interactionId)
.asyncAndThen(() =>
ResultAsync.combine([
dAppRequestQueue.add(message.data, interactionId),
sendMessageWithConfirmation(
createMessage.getConnectionPassword('offScreen')
).andThen((connectionPassword) =>
connectorClient
.setConnectionPassword(connectionPassword)
.mapErr(() => ({ reason: 'setConnectionPasswordError' }))
),
])
)
.asyncAndThen(() => dAppRequestQueue.add(message.data, interactionId))
.andThen(() => okAsync({ sendConfirmation: true }))
}

Expand Down
15 changes: 1 addition & 14 deletions src/connector/connector-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
Subscription,
switchMap,
tap,
timer,
withLatestFrom,
} from 'rxjs'
import { WebRtcClient } from './webrtc/webrtc-client'
Expand All @@ -27,7 +26,7 @@ import { SignalingSubjects, SignalingSubjectsType } from './signaling/subjects'
import { MessageClient } from './messages/message-client'
import { waitForDataChannelStatus } from './webrtc/helpers/wait-for-data-channel-status'
import { sendMessage } from './messages/helpers/send-message'
import { err, ok, ResultAsync } from 'neverthrow'
import { ResultAsync } from 'neverthrow'
import { errorIdentity } from 'utils/error-identity'

export type ConnectorClient = ReturnType<typeof ConnectorClient>
Expand Down Expand Up @@ -154,17 +153,5 @@ export const ConnectorClient = (input: {
logger?.debug('🔌🧹 destroying connector client')
subscriptions.unsubscribe()
},
connectionPassword: (): ResultAsync<string, string> =>
ResultAsync.fromPromise(
firstValueFrom(
merge(
secretsClient.secrets$.pipe(
map((secrets) => ok(secrets.encryptionKey.toString('hex')))
),
timer(100).pipe(map(() => err('connectionPasswordNotSet')))
)
),
(error) => error as string
).andThen((result) => result),
}
}
11 changes: 4 additions & 7 deletions src/queues/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ export const Queue = <T>({
subjects.paused.next(paused)
const subscriptions = new Subscription()

const log = (name: string, ...args: any[]) =>
logger?.debug(`🚦 [${key}] ${name}`, ...args)

const decodeStateData = (
data: QueueStateRaw<T>
): Result<QueueState<T>, never> =>
Expand Down Expand Up @@ -103,7 +100,7 @@ export const Queue = <T>({
): ResultAsync<undefined, QueueInteractionError> =>
transformState(state)
.asyncAndThen((data) => {
log('saveState', data.ids)
logger?.trace('saveState', data.ids)
return storage.setData(key, data)
})
.mapErr(() => ({ reason: 'SaveStateError' }))
Expand All @@ -112,7 +109,7 @@ export const Queue = <T>({
job: Job<T>,
state: QueueState<T>
): ResultAsync<QueueState<T>, never> => {
log('addJob', { job })
logger?.debug('addJob', { job })
state.items.set(job.id, { ...job, updatedAt: Date.now() })
state.ids.pending.add(job.id)
return okAsync(state)
Expand All @@ -130,7 +127,7 @@ export const Queue = <T>({
numberOfRetries?: number
}): ResultAsync<QueueState<T>, QueueInteractionError> => {
const jobId = job.id
log('updateJobStatus', {
logger?.debug('updateJobStatus', {
jobId: job.id,
fromStatus: job.status,
toStatus: status,
Expand All @@ -151,7 +148,7 @@ export const Queue = <T>({
jobId: string,
state: QueueState<T>
): ResultAsync<QueueState<T>, QueueInteractionError> => {
log('removeJob', {
logger?.debug('removeJob', {
jobId,
})

Expand Down

0 comments on commit 675b713

Please sign in to comment.