Skip to content

Use new reload behaviour in live #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: lamdera-next
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clear model early, track late cmds with bugsnag, finish die() for bac…
…kend
  • Loading branch information
lydell committed Jul 20, 2024
commit da8d1999d4ddf22d7df278d06d3445d3a43939b6
42 changes: 30 additions & 12 deletions extra/Lamdera/Injection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,14 @@ injections isBackend isLocalDev =
return Math.floor(hrTime[0] * 1000000 + hrTime[1] / 1000);
}

var isBuried = false;

function sendToApp(msg, viewMetadata)
{
//console.log('sendToApp.active',msg);
if (isBuried) {
bugsnag.notify(new Error('Got message after app was buried: ' + (msg.$ || '(unknown message)')));
return;
}

$shouldProxy

Expand Down Expand Up @@ -275,16 +280,24 @@ injections isBackend isLocalDev =
}

const die = function() {
//console.log('App dying');
// @TODO: Compare to the frontend die and bury functions. Investigate what needs to be done here, and measure memory usage.
// Even if this function isn't ideal, clearing the model should at least go a long way towards not leaking too much memory.
managers = null;
// In case there still are any pending commands, setting this flag means
// that nothing happens when they finish.
isBuried = true;

// The app won't be garbage collected until all pending commands are done.
// We can reclaim most memory immediately by manually clearing the model early.
model = null;
stepper = null;
ports = null;
_Platform_effectsQueue = [];

// On the frontend, we have to clear the effect managers, since they prevent sendToApp from being GC:ed,
// which prevents the whole app from being GC:ed. On the backend, it does not seem to.
// We still do it here for consistency (it doesn't hurt).
_Platform_effectManagers = {};
}

// On the frontend, clearing args helps garbage collection. On the backend, it does not seem to.
// We still do it here for consistency (it doesn't hurt).
args = null;

return ports ? {
ports: ports,
die: die,
Expand Down Expand Up @@ -413,7 +426,7 @@ injections isBackend isLocalDev =
function sendToApp(msg, viewMetadata)
{
if (isBuried) {
console.warn('Got message after app was buried:', msg);
window.lamdera.bs.notify(new Error('Got message after app was buried: ' + (msg.$ || '(unknown message)')));
return;
}

Expand Down Expand Up @@ -484,12 +497,17 @@ injections isBackend isLocalDev =
// trigger an outgoing port to redirect messages. This is supposed to be called
// when all pending commands are done.
const bury = function() {
// Clear effect managers, since they prevent sendToApp from being GC:ed,
// which prevents the whole app from being GC:ed.
_Platform_effectManagers = {};
// In case there still are any pending commands, setting this flag means
// that nothing happens when they finish.
isBuried = true;

// The app won't be garbage collected until all pending commands are done.
// We can reclaim most memory immediately by manually clearing the model early.
model = null;

// Clear effect managers, since they prevent sendToApp from being GC:ed,
// which prevents the whole app from being GC:ed.
_Platform_effectManagers = {};
};

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