Commit c3f7030
committed
🐛 Allow getting
There's currently an edge case involving trying to get presence in the
middle of being destroyed. For example, consider these synchronous
calls:
```js
var presence = connection.getPresence('channel')
presence.destroy()
var newPresence = connection.getPresence('channel')
```
Since `Presence` objects are per-channel singletons, the above code is
effectively the same as:
```js
var presence = connection.getPresence('channel')
presence.destroy()
var newPresence = presence
```
In other words, our "new" presence is actually already being destroyed.
What's worse, is that if we start trying to create local presence before
the `destroy()` callback is triggered, that gets "cleaned up" by the
initial call to `destroy()`:
```js
var presence = connection.getPresence('channel')
presence.destroy(() => {
// by the time we reach here, my newly-created localPresence
// will have been destroyed
})
var newPresence = connection.getPresence('channel')
var localPresence = newPresence.create('me')
```
This change attempts to address this edge case by adding a private
`_wantsDestroy` to our `Presence` instances, which is:
- `false` on construction
- set to `true` when calling `destroy()`
- reset to `false` if calling `connection.getPresence()`
We then check this flag when performing the `destroy()` tidy-up, and
avoid tidying up if we have since reset the flag.
Note that there's a corner case within this edge case, surrounding
`LocalPresence` instances created while a `destroy()` is in progress.
Since we might create `LocalPresence` instances at any point during the
asynchronous `destroy()`, we define the following behaviour:
- any `LocalPresence` instances that exist at the time of invoking
`destroy()` will be destroyed, **even if** we subsequently cancel
the destroy (by invoking `connection.getPresence()`)
- any `LocalPresence` instances that are created *after* invoking
`destroy()` will be kept if the destroy is cancelled
- creating a new `LocalPresence` while `_wantDestroy === true` will
throw, because clients shouldn't put themselves in this situation
and the behaviour when tidying this up is undefinedPresence while it's being destroyed1 parent c9e773d commit c3f7030
File tree
3 files changed
+134
-4
lines changed- lib/client
- presence
- test/client/presence
3 files changed
+134
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
776 | 776 | | |
777 | 777 | | |
778 | 778 | | |
779 | | - | |
| 779 | + | |
780 | 780 | | |
781 | 781 | | |
| 782 | + | |
| 783 | + | |
782 | 784 | | |
783 | 785 | | |
784 | 786 | | |
785 | 787 | | |
786 | 788 | | |
787 | | - | |
| 789 | + | |
788 | 790 | | |
789 | 791 | | |
| 792 | + | |
| 793 | + | |
790 | 794 | | |
791 | 795 | | |
792 | 796 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
42 | 46 | | |
43 | 47 | | |
44 | 48 | | |
45 | 49 | | |
| 50 | + | |
46 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
47 | 57 | | |
48 | 58 | | |
49 | | - | |
50 | 59 | | |
51 | 60 | | |
52 | 61 | | |
| |||
56 | 65 | | |
57 | 66 | | |
58 | 67 | | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
59 | 73 | | |
60 | 74 | | |
61 | 75 | | |
62 | 76 | | |
63 | 77 | | |
64 | 78 | | |
65 | | - | |
| 79 | + | |
66 | 80 | | |
67 | 81 | | |
68 | 82 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
101 | 213 | | |
102 | 214 | | |
103 | 215 | | |
| |||
0 commit comments