Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify if needed before deferring subscriber notifications #251

Merged
merged 9 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/throttledStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const createThrottledStore = (
): PrivateThrottledStore => {
let pendingBroadcastNotification = false
let pendingObservableNotifications: Set<AnyPrivateObservableState> | undefined
let deferNotifications = false
let isDeferrringNotifications = false
let pendingFlush = false

const onBroadcastNotify = () => {
Expand Down Expand Up @@ -225,7 +225,7 @@ export const createThrottledStore = (
}

const scheduledNotifyAll = () => {
if (deferNotifications) {
if (isDeferrringNotifications) {
return
}
notifyAll()
Expand All @@ -240,7 +240,7 @@ export const createThrottledStore = (
})

const flush = (config = { excecutionType: 'default' }) => {
if (deferNotifications && config.excecutionType !== 'immediate') {
if (isDeferrringNotifications && config.excecutionType !== 'immediate') {
pendingFlush = true
return
}
Expand All @@ -259,6 +259,15 @@ export const createThrottledStore = (
__shellName: shell.name
})

const executePendingActions = () => {
if (pendingFlush) {
pendingFlush = false
flush()
} else if (pendingBroadcastNotification || pendingObservableNotifications) {
notifyAll()
}
}

const result: PrivateThrottledStore = {
...store,
subscribe,
Expand All @@ -271,21 +280,17 @@ export const createThrottledStore = (
resetPendingNotifications: resetAllPendingNotifications,
hasPendingSubscribers: () => pendingBroadcastNotification,
deferSubscriberNotifications: async action => {
if (deferNotifications) {
if (isDeferrringNotifications) {
return action()
}
try {
deferNotifications = true
executePendingActions()
isDeferrringNotifications = true
const functionResult = await action()
return functionResult
} finally {
deferNotifications = false
if (pendingFlush) {
pendingFlush = false
flush()
} else {
notifyAll()
}
isDeferrringNotifications = false
executePendingActions()
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions test/connectWithShell.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,44 @@ describe('connectWithShell-useCases', () => {
expect(renderSpy).toHaveBeenCalledTimes(2)
})

it('should not have pending subscribers when starting to defer notifications', async () => {
const { host, shell, renderInShellContext } = createMocks(entryPointWithState, [entryPointSecondStateWithAPI])
const ConnectedComp = connectWithShell(mapStateToProps, undefined, shell, { allowOutOfEntryPoint: true })(PureComp)

const { testKit } = renderInShellContext(<ConnectedComp />)
if (!testKit) {
throw new Error('Connected component failed to render')
}

let hasPendingSubscribersWhileDeferring
await act(async () => {
host.getStore().dispatch({ type: 'SET_FIRST_STATE', value: 'update1' })
await host.getStore().deferSubscriberNotifications(() => {
hasPendingSubscribersWhileDeferring = shell.getStore().hasPendingSubscribers()
})
})

expect(hasPendingSubscribersWhileDeferring).toBe(false)
})

it('should notify subscribers of state changes before deferring notifications', async () => {
const { host, shell, renderInShellContext } = createMocks(entryPointWithState, [entryPointSecondStateWithAPI])
const ConnectedComp = connectWithShell(mapStateToProps, undefined, shell, { allowOutOfEntryPoint: true })(PureComp)

const { testKit } = renderInShellContext(<ConnectedComp />)
if (!testKit) {
throw new Error('Connected component failed to render')
}
expect(mapStateToPropsSpy).toHaveBeenCalledTimes(1)

await act(async () => {
host.getStore().dispatch({ type: 'SET_FIRST_STATE', value: 'update1' })
await host.getStore().deferSubscriberNotifications(() => {
expect(mapStateToPropsSpy).toHaveBeenCalledTimes(2)
})
})
})

it('should notify after action failed while deferring notifications', async () => {
const { host, shell, renderInShellContext } = createMocks(entryPointWithState, [entryPointSecondStateWithAPI])
const ConnectedComp = connectWithShell(mapStateToProps, undefined, shell, { allowOutOfEntryPoint: true })(PureComp)
Expand Down
Loading