Skip to content

Commit 089a669

Browse files
committed
Use stimulus connect/disconnect instead
1 parent 976add6 commit 089a669

File tree

3 files changed

+16
-36
lines changed

3 files changed

+16
-36
lines changed

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,21 +1238,13 @@ class default_1 extends Controller {
12381238
this.isRerenderRequested = false;
12391239
this.requestDebounceTimeout = null;
12401240
this.pollingIntervals = [];
1241-
this.isWindowUnloaded = false;
1241+
this.isConnected = false;
12421242
this.originalDataJSON = '{}';
12431243
this.mutationObserver = null;
12441244
this.childComponentControllers = [];
12451245
this.pendingActionTriggerModelElement = null;
1246-
this.markAsWindowUnloaded = () => {
1247-
this.isWindowUnloaded = true;
1248-
};
1249-
this.markAsWindowNotUnloaded = () => {
1250-
this.isWindowUnloaded = false;
1251-
};
12521246
}
12531247
initialize() {
1254-
this.markAsWindowUnloaded = this.markAsWindowUnloaded.bind(this);
1255-
this.markAsWindowNotUnloaded = this.markAsWindowNotUnloaded.bind(this);
12561248
this.handleUpdateModelEvent = this.handleUpdateModelEvent.bind(this);
12571249
this.handleInputEvent = this.handleInputEvent.bind(this);
12581250
this.handleChangeEvent = this.handleChangeEvent.bind(this);
@@ -1265,13 +1257,12 @@ class default_1 extends Controller {
12651257
this.synchronizeValueOfModelFields();
12661258
}
12671259
connect() {
1260+
this.isConnected = true;
12681261
this._onLoadingFinish();
12691262
if (!(this.element instanceof HTMLElement)) {
12701263
throw new Error('Invalid Element Type');
12711264
}
12721265
this._initiatePolling();
1273-
window.addEventListener('pagehide', this.markAsWindowUnloaded);
1274-
window.addEventListener('pageshow', this.markAsWindowNotUnloaded);
12751266
this._startAttributesMutationObserver();
12761267
this.element.addEventListener('live:update-model', this.handleUpdateModelEvent);
12771268
this.element.addEventListener('input', this.handleInputEvent);
@@ -1282,8 +1273,6 @@ class default_1 extends Controller {
12821273
disconnect() {
12831274
this._stopAllPolling();
12841275
__classPrivateFieldGet(this, _instances, "m", _clearRequestDebounceTimeout).call(this);
1285-
window.removeEventListener('pagehide', this.markAsWindowUnloaded);
1286-
window.removeEventListener('pageshow', this.markAsWindowNotUnloaded);
12871276
this.element.removeEventListener('live:update-model', this.handleUpdateModelEvent);
12881277
this.element.removeEventListener('input', this.handleInputEvent);
12891278
this.element.removeEventListener('change', this.handleChangeEvent);
@@ -1293,6 +1282,7 @@ class default_1 extends Controller {
12931282
if (this.mutationObserver) {
12941283
this.mutationObserver.disconnect();
12951284
}
1285+
this.isConnected = false;
12961286
}
12971287
update(event) {
12981288
if (event.type === 'input' || event.type === 'change') {
@@ -1950,7 +1940,7 @@ _instances = new WeakSet(), _startPendingRequest = function _startPendingRequest
19501940
__classPrivateFieldGet(this, _instances, "m", _startPendingRequest).call(this);
19511941
});
19521942
}, _processRerender = function _processRerender(html, response) {
1953-
if (this.isWindowUnloaded) {
1943+
if (!this.isConnected) {
19541944
return;
19551945
}
19561946
if (response.headers.get('Location')) {

src/LiveComponent/assets/src/live_controller.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default class extends Controller implements LiveController {
7070

7171
pollingIntervals: NodeJS.Timer[] = [];
7272

73-
isWindowUnloaded = false;
73+
isConnected = false;
7474

7575
originalDataJSON = '{}';
7676

@@ -86,8 +86,6 @@ export default class extends Controller implements LiveController {
8686
pendingActionTriggerModelElement: HTMLElement|null = null;
8787

8888
initialize() {
89-
this.markAsWindowUnloaded = this.markAsWindowUnloaded.bind(this);
90-
this.markAsWindowNotUnloaded = this.markAsWindowNotUnloaded.bind(this);
9189
this.handleUpdateModelEvent = this.handleUpdateModelEvent.bind(this);
9290
this.handleInputEvent = this.handleInputEvent.bind(this);
9391
this.handleChangeEvent = this.handleChangeEvent.bind(this);
@@ -101,6 +99,7 @@ export default class extends Controller implements LiveController {
10199
}
102100

103101
connect() {
102+
this.isConnected = true;
104103
// hide "loading" elements to begin with
105104
// This is done with CSS, but only for the most basic cases
106105
this._onLoadingFinish();
@@ -112,8 +111,6 @@ export default class extends Controller implements LiveController {
112111

113112
this._initiatePolling();
114113

115-
window.addEventListener('pagehide', this.markAsWindowUnloaded);
116-
window.addEventListener('pageshow', this.markAsWindowNotUnloaded);
117114
this._startAttributesMutationObserver();
118115
this.element.addEventListener('live:update-model', this.handleUpdateModelEvent);
119116
this.element.addEventListener('input', this.handleInputEvent);
@@ -127,8 +124,6 @@ export default class extends Controller implements LiveController {
127124
this._stopAllPolling();
128125
this.#clearRequestDebounceTimeout();
129126

130-
window.removeEventListener('pagehide', this.markAsWindowUnloaded);
131-
window.removeEventListener('pageshow', this.markAsWindowNotUnloaded);
132127
this.element.removeEventListener('live:update-model', this.handleUpdateModelEvent);
133128
this.element.removeEventListener('input', this.handleInputEvent);
134129
this.element.removeEventListener('change', this.handleChangeEvent);
@@ -140,6 +135,8 @@ export default class extends Controller implements LiveController {
140135
if (this.mutationObserver) {
141136
this.mutationObserver.disconnect();
142137
}
138+
139+
this.isConnected = false;
143140
}
144141

145142
/**
@@ -496,7 +493,7 @@ export default class extends Controller implements LiveController {
496493
*/
497494
#processRerender(html: string, response: Response) {
498495
// check if the page is navigating away
499-
if (this.isWindowUnloaded) {
496+
if (!this.isConnected) {
500497
return;
501498
}
502499

@@ -798,14 +795,6 @@ export default class extends Controller implements LiveController {
798795
this._exposeOriginalData();
799796
}
800797

801-
markAsWindowUnloaded = () => {
802-
this.isWindowUnloaded = true;
803-
};
804-
805-
markAsWindowNotUnloaded = () => {
806-
this.isWindowUnloaded = false;
807-
};
808-
809798
handleConnectedControllerEvent(event: any) {
810799
if (event.target === this.element) {
811800
return;

src/LiveComponent/assets/test/controller/render.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ describe('LiveController rendering Tests', () => {
231231
.init();
232232

233233
test.controller.$render();
234-
// imitate navigating away
235-
fireEvent(window, createEvent('pagehide', window));
234+
// trigger disconnect
235+
test.element.removeAttribute('data-controller')
236236

237237
// wait for the fetch to finish
238238
await fetchMock.flush();
@@ -255,15 +255,16 @@ describe('LiveController rendering Tests', () => {
255255
.init();
256256

257257
test.controller.$render();
258-
// imitate navigating away
259-
fireEvent(window, createEvent('pagehide', window));
258+
259+
// trigger controller disconnect
260+
test.element.removeAttribute('data-controller')
260261
// wait for the fetch to finish
261262
await fetchMock.flush();
262263

263264
expect(test.element).toHaveTextContent('aloha!')
264265

265-
// navigate back
266-
fireEvent(window, createEvent('pageshow', window));
266+
// trigger connect
267+
test.element.setAttribute('data-controller', 'live')
267268
test.controller.$render();
268269
// wait for the fetch to finish
269270
await fetchMock.flush();

0 commit comments

Comments
 (0)