Skip to content

Commit e2d1eb4

Browse files
lydellsupermario
authored andcommitted
Clear model early, track late cmds with bugsnag, finish die() for backend
1 parent 382a342 commit e2d1eb4

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

extra/Lamdera/Injection.hs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,14 @@ injections mode isBackend isLocalDev =
473473
return Math.floor(hrTime[0] * 1000000 + hrTime[1] / 1000);
474474
}
475475

476+
var isBuried = false;
477+
476478
function sendToApp(msg, viewMetadata)
477479
{
478-
//console.log('sendToApp.active',msg);
480+
if (isBuried) {
481+
bugsnag.notify(new Error('Got message after app was buried: ' + (msg.$ || '(unknown message)')));
482+
return;
483+
}
479484

480485
$shouldProxy
481486

@@ -519,16 +524,24 @@ injections mode isBackend isLocalDev =
519524
}
520525

521526
const die = function() {
522-
//console.log('App dying');
523-
// @TODO: Compare to the frontend die and bury functions. Investigate what needs to be done here, and measure memory usage.
524-
// Even if this function isn't ideal, clearing the model should at least go a long way towards not leaking too much memory.
525-
managers = null;
527+
// In case there still are any pending commands, setting this flag means
528+
// that nothing happens when they finish.
529+
isBuried = true;
530+
531+
// The app won't be garbage collected until all pending commands are done.
532+
// We can reclaim most memory immediately by manually clearing the model early.
526533
model = null;
527-
stepper = null;
528-
ports = null;
529-
_Platform_effectsQueue = [];
534+
535+
// On the frontend, we have to clear the effect managers, since they prevent sendToApp from being GC:ed,
536+
// which prevents the whole app from being GC:ed. On the backend, it does not seem to.
537+
// We still do it here for consistency (it doesn't hurt).
538+
_Platform_effectManagers = {};
530539
}
531540

541+
// On the frontend, clearing args helps garbage collection. On the backend, it does not seem to.
542+
// We still do it here for consistency (it doesn't hurt).
543+
args = null;
544+
532545
return ports ? {
533546
ports: ports,
534547
die: die,
@@ -657,7 +670,7 @@ injections mode isBackend isLocalDev =
657670
function sendToApp(msg, viewMetadata)
658671
{
659672
if (isBuried) {
660-
console.warn('Got message after app was buried:', msg);
673+
window.lamdera.bs.notify(new Error('Got message after app was buried: ' + (msg.$ || '(unknown message)')));
661674
return;
662675
}
663676

@@ -728,12 +741,17 @@ injections mode isBackend isLocalDev =
728741
// trigger an outgoing port to redirect messages. This is supposed to be called
729742
// when all pending commands are done.
730743
const bury = function() {
731-
// Clear effect managers, since they prevent sendToApp from being GC:ed,
732-
// which prevents the whole app from being GC:ed.
733-
_Platform_effectManagers = {};
734744
// In case there still are any pending commands, setting this flag means
735745
// that nothing happens when they finish.
736746
isBuried = true;
747+
748+
// The app won't be garbage collected until all pending commands are done.
749+
// We can reclaim most memory immediately by manually clearing the model early.
750+
model = null;
751+
752+
// Clear effect managers, since they prevent sendToApp from being GC:ed,
753+
// which prevents the whole app from being GC:ed.
754+
_Platform_effectManagers = {};
737755
};
738756

739757
// Clearing args means the flags (like the passed in model) can be GC:ed (in the new app).

0 commit comments

Comments
 (0)