Skip to content

Commit 635333a

Browse files
authored
Merge pull request #307 from PrefectHQ/subscription-refresh-dispose
Enhancement: Create a new scope for each execution when refreshing subscriptions
2 parents d775ad5 + 3c31dbe commit 635333a

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/useSubscription/models/channel.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class Channel<T extends Action = Action> {
4444
private readonly action: T
4545
private readonly args: ActionArguments<T>
4646
private readonly subscriptions: Map<number, Subscription<T>> = new Map()
47-
private readonly scope = effectScope()
47+
private scope = effectScope()
4848
private timer: ReturnType<typeof setInterval> | null = null
4949
private lastExecution: number = 0
5050
private _loading: boolean = false
@@ -193,6 +193,14 @@ export default class Channel<T extends Action = Action> {
193193
}
194194
}
195195

196+
public refresh(): Promise<void> {
197+
this.scope.stop()
198+
this.scope = effectScope()
199+
const response = this.execute()
200+
201+
return response
202+
}
203+
196204
public isSubscribed(id: number): boolean {
197205
return this.subscriptions.has(id)
198206
}
@@ -217,6 +225,6 @@ export default class Channel<T extends Action = Action> {
217225
const sinceLastRun = Date.now() - this.lastExecution
218226
const timeTillNextExecution = this.interval - sinceLastRun
219227

220-
this.timer = setTimeout(() => this.execute(), timeTillNextExecution)
228+
this.timer = setTimeout(() => this.refresh(), timeTillNextExecution)
221229
}
222230
}

src/useSubscription/models/subscription.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default class Subscription<T extends Action> {
3636
}
3737

3838
public async refresh(): Promise<void> {
39-
await this.channel.execute()
39+
await this.channel.refresh()
4040
}
4141

4242
public unsubscribe(): void {

src/useSubscription/useSubscription.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,14 @@ describe('subscribe', () => {
571571
expect(spy).toBeCalledTimes(1)
572572
})
573573

574-
it('changing args unsubscribes nested subscriptions automatically', async () => {
574+
it('unsubscribes nested subscriptions when changing args', async () => {
575575
const number = ref(0)
576576
const manager = new Manager()
577577
const action = vi.fn()
578578
let childSubscription: UseSubscription<typeof action>
579579

580580
useSubscription((number: number): number => {
581-
childSubscription = useSubscription(action)
581+
childSubscription = useSubscription(action, [], { manager })
582582
return number
583583
}, [number], { manager })
584584

@@ -591,4 +591,20 @@ describe('subscribe', () => {
591591
expect(spy).toBeCalledTimes(1)
592592
})
593593

594+
it('it refreshes nested subscriptions when calling refresh', async () => {
595+
const action = vi.fn()
596+
const manager = new Manager()
597+
const subscription = useSubscription((): void => {
598+
useSubscription(action, [], { manager })
599+
}, [], { manager })
600+
601+
await timeout()
602+
603+
subscription.refresh()
604+
605+
await timeout()
606+
607+
expect(action).toBeCalledTimes(2)
608+
})
609+
594610
})

0 commit comments

Comments
 (0)