Skip to content

freedom.js structure: module cleanup

soycode edited this page Sep 21, 2015 · 2 revisions

When a freedom.js module is closed/destroyed, it will automatically try to free the memory of any children objects/modules. You can also define more detailed/custom teardown behavior as needed for core modules, e.g. for a module that uses browser primitive resources or interfaces over a network.

To do this within the module, you must add the behavior to cap.provider.onClose, where cap is the capabilities object that freedom.js passes in as the first argument to the constructor of your module. You can do this with bind, and must wrap this setup in a setTimeout call for to-be-documented bugginess reasons.

setTimeout(cap.provider.onClose.bind(
  cap.provider,
  this,
  function () { ..actual close behavior... }
), 0);

Note that the last argument (the function with actual close behavior) can itself involve binding an existing close function to this, or specifying some continuation based on the situation, etc.

In addition to overriding the cap.provider.onClose behavior, you need to specify a flag to freedom.js so it knows to change its default behavior.

exports.flags = { provider: true };

This line should be at the bottom of your module, i.e. where your other exports-related statements are (naming the module and specifying what it provides, etc.).