Skip to content

Commit c3e1ee8

Browse files
author
LongYinan
committed
fix(core): skip from SSR logic
1 parent e5b616d commit c3e1ee8

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

packages/core/src/module.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Action, Epic } from '@sigi/types'
22
import produce, { Draft } from 'immer'
33
import { Observable, merge } from 'rxjs'
4-
import { map, filter, skip, ignoreElements } from 'rxjs/operators'
4+
import { map, filter, ignoreElements } from 'rxjs/operators'
55

66
import { hmrEnabled, hmrInstanceCache } from './hmr'
77
import { getDecoratedActions, getActionsToSkip } from './metadata'
@@ -43,6 +43,8 @@ export abstract class EffectModule<S> {
4343
private readonly actionStreams: any = {}
4444
private readonly retryActionsCreator: any = {}
4545
private readonly actionNames: string[] = []
46+
private actionsToRetry: Set<string> = new Set()
47+
private actionsToSkip!: string[]
4648
private restoredFromSSR = false
4749

4850
get state$() {
@@ -116,13 +118,23 @@ export abstract class EffectModule<S> {
116118
context.internalDefaultState = value
117119
if (!context.store.ready) {
118120
context.store.setup(context.getDefaultState())
121+
context.actionsToRetry = new Set(_globalThis[RETRY_KEY_SYMBOL]?.[this.moduleName] || [])
122+
context.actionsToSkip = context.restoredFromSSR
123+
? // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
124+
getActionsToSkip(context.constructor.prototype) || []
125+
: []
119126
}
120127
return rawSetter.call(this, value)
121128
}
122129
} else if ('value' in attr) {
123130
context.internalDefaultState = attr.value
124131
if (!context.store.ready) {
125132
context.store.setup(context.getDefaultState())
133+
context.actionsToRetry = new Set(_globalThis[RETRY_KEY_SYMBOL]?.[context.moduleName] || [])
134+
context.actionsToSkip = context.restoredFromSSR
135+
? // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
136+
getActionsToSkip(context.constructor.prototype) || []
137+
: []
126138
}
127139
}
128140
}
@@ -133,6 +145,11 @@ export abstract class EffectModule<S> {
133145
context.internalDefaultState = value
134146
if (!context.store.ready) {
135147
context.store.setup(context.getDefaultState())
148+
context.actionsToRetry = new Set(_globalThis[RETRY_KEY_SYMBOL]?.[context.moduleName] || [])
149+
context.actionsToSkip = context.restoredFromSSR
150+
? // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
151+
getActionsToSkip(context.constructor.prototype) || []
152+
: []
136153
}
137154
}
138155
return Reflect.set(target, p, value, receiver)
@@ -144,6 +161,9 @@ export abstract class EffectModule<S> {
144161
this.internalDefaultState = value
145162
if (!this.store.ready) {
146163
this.store.setup(this.getDefaultState())
164+
this.actionsToRetry = new Set(_globalThis[RETRY_KEY_SYMBOL]?.[this.moduleName] || [])
165+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
166+
this.actionsToSkip = this.restoredFromSSR ? getActionsToSkip(this.constructor.prototype) || [] : []
147167
}
148168
},
149169
get: () => {
@@ -254,16 +274,16 @@ export abstract class EffectModule<S> {
254274
}
255275

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

260278
return (action$: Observable<Action>) => {
261279
return merge(
262280
...effectKeys.map((name) => {
263281
const effect: Effect<unknown> = (this as any)[name]
264282
const payload$ = action$.pipe(
265-
filter(({ type }) => type === name),
266-
skip(!actionsToRetry.has(name) && actionsToSkip?.includes(name) ? 1 : 0),
283+
filter(({ type }, index) => {
284+
const skipCount = !this.actionsToRetry.has(name) && this.actionsToSkip?.includes(name) ? 1 : 0
285+
return type === name && skipCount <= index
286+
}),
267287
map(({ payload }) => payload),
268288
)
269289
this.retryActionsCreator[name] = () =>

0 commit comments

Comments
 (0)