Skip to content

Commit

Permalink
poc working
Browse files Browse the repository at this point in the history
  • Loading branch information
edolix committed Apr 3, 2023
1 parent ce8845d commit c8efd77
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 56 deletions.
32 changes: 6 additions & 26 deletions packages/core/src/BaseComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ import {
BaseComponentContract,
} from './types'
import {
getAuthError,
getAuthState,
getAuthStatus,
} from './redux/features/session/sessionSelectors'
import { compoundEventAttachAction } from './redux/actions'
import { AuthError } from './CustomErrors'
import { proxyFactory } from './utils/proxyUtils'
import { executeActionWorker } from './workers'

Expand Down Expand Up @@ -837,34 +835,16 @@ export class BaseComponent<
case 'authorizing':
return new Promise((resolve, reject) => {
// @ts-expect-error
this.once('session.connected', (event: any) => {
console.log('session.connected', event)
this.once('session.connected', () => {
this.logger.info('session.connected')
resolve(this)
})

// @ts-expect-error
this.once('session.auth_error', (event: any) => {
console.log('session.auth_error', event)
// const error = authError
// ? new AuthError(authError.code, authError.message)
// : new Error('Unauthorized')
// reject(error)
reject(new Error('Unauthorized'))
this.once('session.auth_error', (error: any) => {
this.logger.info('session.auth_error', error)
reject(error)
})
// const unsubscribe = this.store.subscribe(() => {
// const authStatus = getAuthStatus(this.store.getState())
// const authError = getAuthError(this.store.getState())

// if (authStatus === 'authorized') {
// resolve(this)
// unsubscribe()
// } else if (authStatus === 'unauthorized') {
// const error = authError
// ? new AuthError(authError.code, authError.message)
// : new Error('Unauthorized')
// reject(error)
// unsubscribe()
// }
// })
})

case 'unauthorized':
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ const configureStore = (options: ConfigureStoreOptions) => {
runSaga,
channels,
dispatch: (action: any) => {
swStore.rootPut(action)
// store.dispatch(action)
return swStore.dispatch(action)
},
getState: () => {
return swStore.getState()
Expand Down
17 changes: 7 additions & 10 deletions packages/core/src/redux/rootSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { AuthError } from '../CustomErrors'
import { PubSubChannel, RootChannel, SessionChannel } from './interfaces'
import { createRestartableSaga } from './utils/sagaHelpers'
import { Action } from 'redux'
// import { componentCleanupSaga } from './features/component/componentSaga'

interface StartSagaOptions {
Expand Down Expand Up @@ -138,20 +139,18 @@ export function* initSessionSaga({
export function* reauthenticateWorker({
session,
token,
rootChannel,
pubSubChannel,
}: {
session: BaseSession
token: string
rootChannel: RootChannel
pubSubChannel: PubSubChannel
}) {
try {
if (session.reauthenticate) {
session.token = token
yield call(session.reauthenticate)
// Update the store with the new "connect result"
yield put(rootChannel, sessionActions.connected(session.rpcConnectResult))
yield put(sessionActions.connected(session.rpcConnectResult))
yield put(pubSubChannel, sessionConnectedAction())
}
} catch (error) {
Expand Down Expand Up @@ -180,11 +179,8 @@ export function* sessionStatusWatcher(options: StartSagaOptions): SagaIterator {
getLogger().info('sessionStatusWatcher', action.type, action.payload)
switch (action.type) {
case authSuccessAction.type: {
const { session, pubSubChannel, rootChannel } = options
yield put(
rootChannel,
sessionActions.connected(session.rpcConnectResult)
)
const { session, pubSubChannel } = options
yield put(sessionActions.connected(session.rpcConnectResult))
yield put(pubSubChannel, sessionConnectedAction())
break
}
Expand All @@ -203,7 +199,6 @@ export function* sessionStatusWatcher(options: StartSagaOptions): SagaIterator {
yield fork(reauthenticateWorker, {
session: options.session,
token: action.payload.token,
rootChannel: options.rootChannel,
pubSubChannel: options.pubSubChannel,
})
break
Expand Down Expand Up @@ -267,12 +262,14 @@ export default (options: RootSagaOptions) => {
setDebugOptions(userOptions.debug)
}

const actions: string[] = [initAction.type, reauthAction.type]
const filter = (action: Action) => actions.includes(action.type)
while (true) {
/**
* Wait for an initAction to start
*/
getLogger().info('RootSaga WAIT INIT')
const action = yield take(channels.rootChannel, initAction.type)
const action = yield take(channels.rootChannel, filter)
getLogger().info('RootSaga GO ...')

/**
Expand Down
69 changes: 51 additions & 18 deletions packages/core/src/redux/swStore.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { runSaga } from '@redux-saga/core'
import type { Task, Action, Saga } from '@redux-saga/types'
// import {
// fork,
// select,
// take,
// put,
// all,
// cancelled,
// } from '@redux-saga/core/effects'
import type { Task, Saga } from '@redux-saga/types'
import { getLogger } from '../utils'
import { SDKState } from './interfaces'
import { InternalChannels } from '../utils/interfaces'
import { AnyAction } from './toolkit'

interface CreateSwStoreParams {
channels: InternalChannels
Expand Down Expand Up @@ -42,8 +35,53 @@ export const createSWStore = ({ channels }: CreateSwStoreParams) => {
// this will be used to orchestrate take and put Effects
channel: channels.rootChannel,
// this will be used to resolve put Effects
dispatch(action: Action) {
dispatch(action: AnyAction) {
logger.warn('swStore >> Dispatch', action)

// Process the action within reducers
switch (action.type) {
case 'session/connected':
state.session = {
...state.session,
authStatus: 'authorized',
authState: action.payload?.authorization,
authCount: state.session.authCount + 1,
protocol: action.payload?.protocol ?? '',
iceServers: action.payload?.ice_servers ?? [],
}
break
case 'components/upsert':
if (action.payload.id in state.components.byId) {
state.components = {
...state,
byId: {
...state.components.byId,
[action.payload.id]: {
...state.components.byId[action.payload.id],
...action.payload,
},
},
}
} else {
state.components = {
...state,
byId: {
...state.components.byId,
[action.payload.id]: action.payload,
},
}
}
break
default:
getLogger().warn('Unhandled dispatch', action.type)
}

channels.rootChannel.put(action)
// runSaga(myIO, function* () {
// yield put(channels.rootChannel, action)
// })

return action
},
// this will be used to resolve select Effects
getState,
Expand All @@ -62,16 +100,11 @@ export const createSWStore = ({ channels }: CreateSwStoreParams) => {
rootTask.cancel()
},
runWorker: (worker: Saga, options: any) => {
logger.warn('swStore >> runWorker', worker, options)
// logger.warn('swStore >> runWorker', worker, options)
return runSaga(myIO, worker, options)
},
// statePut: (action: Action) => {
// logger.warn('swStore >> statePut', action)
// runSaga(myIO, function* () {
// yield put(action)
// })
// },
rootPut: (action: Action) => {
dispatch: (action: AnyAction) => myIO.dispatch(action),
rootPut: (action: AnyAction) => {
logger.warn('swStore >> rootPut', action)
channels.rootChannel.put(action)
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PubSubChannel,
SwEventChannel,
SessionChannel,
RootChannel,
} from './redux/interfaces'
import { BaseSession } from './BaseSession'
import { RPCConnectResult, InternalSDKLogger } from './utils/interfaces'
Expand Down Expand Up @@ -120,12 +121,14 @@ export const rpcConnectResultVRT: RPCConnectResult = {
{
urls: 'turn.swire.io:443',
credential: 'sFTwvi8ShXcYNOcyYjFy3ATIUpQ=',
// @ts-ignore
credentialType: 'password',
username: '1619521908:8f0a119a-cda7-4497-a47d-c81493b824d4',
},
],
}

export const createRootChannel = (): RootChannel => multicastChannel()
export const createPubSubChannel = (): PubSubChannel => multicastChannel()
export const createSwEventChannel = (): SwEventChannel => multicastChannel()
export const createSessionChannel = (): SessionChannel => channel()

0 comments on commit c8efd77

Please sign in to comment.