1. Miscellaneous
  2. Managing Desktop Memory References

Miscellaneous

Managing Desktop Memory References

Our @todesktop/client-core NPM package enables access to native desktop features from your client-side application. This enables powerful functionality, such as allowing you to create windows with just a few lines of code:

        import { nativeWindow, webContents } from '@todesktop/client-core';

const windowRef = await nativeWindow.create({});
await webContents.loadURL(
  { ref: await nativeWindow.getWebContents({ ref: windowRef }) },
  'https://www.google.com'
);

      

To make this work, we retain a record of the references (e.g. windowRef) that are used between the desktop and the web process. We also provide functionality through the function object.releaseMemoryBindings to delete these references when they're no longer needed. This is important for managing the desktop app's memory footprint.

Following our previous example, we can destroy the created window and release it's associated memory reference with the following:

        import { nativeWindow, object } from '@todesktop/client-core';

const windowRef = await nativeWindow.create({});
// ...

await nativeWindow.destroy({ ref: windowRef });
await object.releaseMemoryBindings(windowRef);

      

While this works as expected, it's usually better to reactively release references in response to some termination event. This helps us catch cases where the window is closed outside of our direct control:

        import { nativeWindow, object } from '@todesktop/client-core';

const windowRef = await nativeWindow.create({});
// ...

nativeWindow.on(
  'closed',
  async () => {
    await object.releaseMemoryBindings(windowRef);
  },
  { ref: windowRef }
);