UNPKG

@luminati-io/luminati-proxy

Version:

A configurable local proxy for brightdata.com

91 lines (88 loc) 3.14 kB
// LICENSE_CODE ZON 'use strict'; /*jslint react:true*/ define(['react', '/util/etask.js', '/util/setdb.js'], (React, etask, setdb)=>{ const LONG_CB_MS = 100; // XXX krzysztof: copied from android/who/app/components, removed local // dependency: zerr and event // XXX arik: need test class Pure_component extends React.PureComponent { constructor(props){ super(props); this.comp_name = this.constructor.name; this.listeners = {}; } componentWillUnmount(){ let t0 = this.unmount_time = Date.now(); if (this.root_et) { this.root_et.return(); delete this.root_et; } // XXX michaelg: 'let of' requires shim with babel+react+ie11 // requires further investigation, leave as is till 01-Feb-2018 /* for (let l of Object.values(this.listeners)) setdb.off(l);*/ Object.values(this.listeners).forEach(l=>{ setdb.off(l); }); if (this.willUnmount) this.willUnmount(); let t1 = Date.now(); if (this.debug && t1-t0 > LONG_CB_MS) { console.warn('long cb componentWillUnmount %s took %sms', this.comp_name, t1-t0); } } setdb_on(path, cb, opt){ this.listeners[path] = setdb.on(path, cb, opt); } setdb_off(path){ this.listeners[path] && setdb.off(this.listeners[path]); } setdb_get(path){ return setdb.get(path); } setdb_set(path, curr, opt){ return setdb.set(path, curr, opt); } etask(et){ if (et.constructor.name!='Etask') et = etask(et); // note: in some cases etask(sp) ends up calling componentWillUnmount // synchronously, so this.root_et can be null after initialization let root_et; if (!(root_et = this.root_et)) { root_et = etask('Component', function*(){ yield this.wait(); }); if (this.unmount_time) root_et.return(); else this.root_et = root_et; } root_et.spawn(et); return et; } setState(updater, cb){ let t0, t1, t2, t3; if (this.debug) { console.log('setState %s %s', this.comp_name, Object.keys(updater).join(', ')); t0 = Date.now(); } super.setState(updater, ()=>{ t2 = Date.now(); if (cb) cb.apply(this, arguments); t3 = Date.now(); if (this.debug && t3-t2 > LONG_CB_MS) { console.warn('long cb setState %s cb %s took %sms', this.comp_name, cb && cb.name, t3-t2); } if (this.debug && t3-t0 > LONG_CB_MS) { console.warn('long cb setState-done %s cb %s took %sms', this.comp_name, cb && cb.name, t3-t0); } }); t1 = Date.now(); if (this.debug && t1-t0 > LONG_CB_MS) { console.warn('long cb setState %s took %sms', this.comp_name, t1-t0); } } } return Pure_component; }); // eslint-disable-line