1 | import { Reaction } from "mobx"
|
2 | import {
|
3 | ReactionCleanupTracking,
|
4 | IReactionTracking,
|
5 | CLEANUP_TIMER_LOOP_MILLIS,
|
6 | createTrackingData
|
7 | } from "./reactionCleanupTrackingCommon"
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export function createTimerBasedReactionCleanupTracking(): ReactionCleanupTracking {
|
13 | |
14 |
|
15 |
|
16 | const uncommittedReactionRefs: Set<React.MutableRefObject<IReactionTracking | null>> = new Set()
|
17 |
|
18 | |
19 |
|
20 |
|
21 | let reactionCleanupHandle: ReturnType<typeof setTimeout> | undefined
|
22 |
|
23 |
|
24 | |
25 |
|
26 |
|
27 | function forceCleanupTimerToRunNowForTests() {
|
28 |
|
29 |
|
30 | if (reactionCleanupHandle) {
|
31 | clearTimeout(reactionCleanupHandle)
|
32 | cleanUncommittedReactions()
|
33 | }
|
34 | }
|
35 |
|
36 |
|
37 | function resetCleanupScheduleForTests() {
|
38 | if (uncommittedReactionRefs.size > 0) {
|
39 | for (const ref of uncommittedReactionRefs) {
|
40 | const tracking = ref.current
|
41 | if (tracking) {
|
42 | tracking.reaction.dispose()
|
43 | ref.current = null
|
44 | }
|
45 | }
|
46 | uncommittedReactionRefs.clear()
|
47 | }
|
48 |
|
49 | if (reactionCleanupHandle) {
|
50 | clearTimeout(reactionCleanupHandle)
|
51 | reactionCleanupHandle = undefined
|
52 | }
|
53 | }
|
54 |
|
55 | function ensureCleanupTimerRunning() {
|
56 | if (reactionCleanupHandle === undefined) {
|
57 | reactionCleanupHandle = setTimeout(cleanUncommittedReactions, CLEANUP_TIMER_LOOP_MILLIS)
|
58 | }
|
59 | }
|
60 |
|
61 | function scheduleCleanupOfReactionIfLeaked(
|
62 | ref: React.MutableRefObject<IReactionTracking | null>
|
63 | ) {
|
64 | uncommittedReactionRefs.add(ref)
|
65 |
|
66 | ensureCleanupTimerRunning()
|
67 | }
|
68 |
|
69 | function recordReactionAsCommitted(
|
70 | reactionRef: React.MutableRefObject<IReactionTracking | null>
|
71 | ) {
|
72 | uncommittedReactionRefs.delete(reactionRef)
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 | function cleanUncommittedReactions() {
|
79 | reactionCleanupHandle = undefined
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | const now = Date.now()
|
85 | uncommittedReactionRefs.forEach(ref => {
|
86 | const tracking = ref.current
|
87 | if (tracking) {
|
88 | if (now >= tracking.cleanAt) {
|
89 |
|
90 | tracking.reaction.dispose()
|
91 | ref.current = null
|
92 | uncommittedReactionRefs.delete(ref)
|
93 | }
|
94 | }
|
95 | })
|
96 |
|
97 | if (uncommittedReactionRefs.size > 0) {
|
98 |
|
99 |
|
100 | ensureCleanupTimerRunning()
|
101 | }
|
102 | }
|
103 |
|
104 | return {
|
105 | addReactionToTrack(
|
106 | reactionTrackingRef: React.MutableRefObject<IReactionTracking | null>,
|
107 | reaction: Reaction,
|
108 | |
109 |
|
110 |
|
111 |
|
112 | objectRetainedByReact: unknown
|
113 | ) {
|
114 | reactionTrackingRef.current = createTrackingData(reaction)
|
115 | scheduleCleanupOfReactionIfLeaked(reactionTrackingRef)
|
116 | return reactionTrackingRef.current
|
117 | },
|
118 | recordReactionAsCommitted,
|
119 | forceCleanupTimerToRunNowForTests,
|
120 | resetCleanupScheduleForTests
|
121 | }
|
122 | }
|