Giter VIP home page Giter VIP logo

apprun-leak-test's Introduction

AppRun Leak Test

Introduction

When integrating with 3rd party libraries, AppRun components create the 3rd party classes in the AppRun lifecycle function, the rendered function.

import app, { Component } from 'apprun';
import Test from './TestClass'

export default class extends Component {
  state = {}
  view = () => <div/>
  update = {}
  rendered = ()=> new Test(document.querySelector('input'))
}

Sometimes the 3rd party creates objects in the window object,in the DOM element or in the class itself. We can mimic the scenarios using the _TestClass below, which creates objects from the Leak class.

class Leak { }
export default class TestClass {

  _leak: Leak;

  constructor(el: HTMLInputElement) {
    window['_leak'] = new Leak();
    el['_leak'] = new Leak();
    this._leak = new Leak();
    el.addEventListener('input', _ => {
      console.log(el.value);
    });
  }

  destroy() {
    this._leak = null;
    window['_leak'] = null;
    console.log('TestClass.destroy');
  }
}

You can see the _leak object in the Chrome Developer Tools on the Home page.

When switch from the Home page to the About page, the _leak object stays in the window object.

If the 3rd party class provides a function to clean up the resource like the _destroy _function in the TestClass, we can use the AppRun lifecycle function unload to call the function.

export default class extends Component {

  test: Test;

  /* ... ... */

  rendered = () => {
    const input = document.querySelector('input')
    this.test = new Test(input);
  }

  unload = () => {
    this.test.destroy();
    this.test = null;
  }
}

Then, the _leak objects are cleared.

Run the Application

  • Use npm start to start the dev server
  • Use npm test to run unit tests
  • Use npm run build to build for production

This is an application built with AppRun.

apprun-leak-test's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.