Skip to content

Commit

Permalink
fix(core): skip from SSR logic
Browse files Browse the repository at this point in the history
  • Loading branch information
LongYinan committed Jun 15, 2021
1 parent e5b616d commit c3e1ee8
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions packages/core/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Action, Epic } from '@sigi/types'
import produce, { Draft } from 'immer'
import { Observable, merge } from 'rxjs'
import { map, filter, skip, ignoreElements } from 'rxjs/operators'
import { map, filter, ignoreElements } from 'rxjs/operators'

import { hmrEnabled, hmrInstanceCache } from './hmr'
import { getDecoratedActions, getActionsToSkip } from './metadata'
Expand Down Expand Up @@ -43,6 +43,8 @@ export abstract class EffectModule<S> {
private readonly actionStreams: any = {}
private readonly retryActionsCreator: any = {}
private readonly actionNames: string[] = []
private actionsToRetry: Set<string> = new Set()
private actionsToSkip!: string[]
private restoredFromSSR = false

get state$() {
Expand Down Expand Up @@ -116,13 +118,23 @@ export abstract class EffectModule<S> {
context.internalDefaultState = value
if (!context.store.ready) {
context.store.setup(context.getDefaultState())
context.actionsToRetry = new Set(_globalThis[RETRY_KEY_SYMBOL]?.[this.moduleName] || [])
context.actionsToSkip = context.restoredFromSSR
? // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
getActionsToSkip(context.constructor.prototype) || []
: []
}
return rawSetter.call(this, value)
}
} else if ('value' in attr) {
context.internalDefaultState = attr.value
if (!context.store.ready) {
context.store.setup(context.getDefaultState())
context.actionsToRetry = new Set(_globalThis[RETRY_KEY_SYMBOL]?.[context.moduleName] || [])
context.actionsToSkip = context.restoredFromSSR
? // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
getActionsToSkip(context.constructor.prototype) || []
: []
}
}
}
Expand All @@ -133,6 +145,11 @@ export abstract class EffectModule<S> {
context.internalDefaultState = value
if (!context.store.ready) {
context.store.setup(context.getDefaultState())
context.actionsToRetry = new Set(_globalThis[RETRY_KEY_SYMBOL]?.[context.moduleName] || [])
context.actionsToSkip = context.restoredFromSSR
? // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
getActionsToSkip(context.constructor.prototype) || []
: []
}
}
return Reflect.set(target, p, value, receiver)
Expand All @@ -144,6 +161,9 @@ export abstract class EffectModule<S> {
this.internalDefaultState = value
if (!this.store.ready) {
this.store.setup(this.getDefaultState())
this.actionsToRetry = new Set(_globalThis[RETRY_KEY_SYMBOL]?.[this.moduleName] || [])
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
this.actionsToSkip = this.restoredFromSSR ? getActionsToSkip(this.constructor.prototype) || [] : []
}
},
get: () => {
Expand Down Expand Up @@ -254,16 +274,16 @@ export abstract class EffectModule<S> {
}

this.actionNames.push(...effectKeys)
const actionsToRetry = new Set(_globalThis[RETRY_KEY_SYMBOL]?.[this.moduleName] || [])
const actionsToSkip = this.restoredFromSSR ? getActionsToSkip(this.constructor.prototype) : undefined

return (action$: Observable<Action>) => {
return merge(
...effectKeys.map((name) => {
const effect: Effect<unknown> = (this as any)[name]
const payload$ = action$.pipe(
filter(({ type }) => type === name),
skip(!actionsToRetry.has(name) && actionsToSkip?.includes(name) ? 1 : 0),
filter(({ type }, index) => {
const skipCount = !this.actionsToRetry.has(name) && this.actionsToSkip?.includes(name) ? 1 : 0
return type === name && skipCount <= index
}),
map(({ payload }) => payload),
)
this.retryActionsCreator[name] = () =>
Expand Down

0 comments on commit c3e1ee8

Please sign in to comment.