Friday, August 24, 2018

Is there a method to remove circular references in Ace

I'm wrapping AceEditor page in web view and returning the event results to the container and it is hanging on objects that contain circular references.

Is there a method in ace to remove circular references in the objects dispatched in the events of Ace Editor?

Solved

I didn't find one so I wrote one myself. It seems to work so far:

"use strict";

// returns an object that removes circular references
document.removeReferences = function (object, maxDepth, depth, objects, clone, debug) {
  if (objects==null) { objects = []; }
  if (clone==true) { clone = {}; }
  maxDepth = maxDepth==null ? 10 : maxDepth;
  depth = depth==null ? 0 : depth;
  var padding = "";
  var value;

  if (debug) { for (var i=0;i=maxDepth) {
          //object[key] = null;

          if (clone==null) {
            if (debug) console.log(padding+"max object limit - deleting:"+key);
            delete object[key];
          }
          else {

            if (typeof value=="array") {
              clone[key] = null;
            }
            else {
              clone[key] = null;
            }
            delete clone[key];
            if (debug) console.log(padding+"max object limit - not adding:"+key); 
          }

        }
        else {

          if (clone==null) {
            if (value!=null) {
              if (debug) console.log(padding+"diving into:"+key);
              document.removeReferences(value, maxDepth, depth+1, objects, null, debug);
            }
          }
          else {
            if (typeof value=="array") {
              clone[key] = value.slice();
            }
            else {
              clone[key] = value!=null ? {} : null;
            }

            if (value!=null) {
              if (debug) console.log(padding+"diving into:"+key);
              document.removeReferences(value, maxDepth, depth+1, objects, clone[key], debug);
            }
          }
        }
      }
      else if (typeof value == 'function') {
          //object[key] = null;
          if (clone==null) {
            delete object[key];
          }
          else {

          }
      }
      else {

        if (clone!=null) {
          if (debug) console.log(padding+" cloning key :"+key);

          if (typeof value=="array") {
            clone[key] = value.slice();
          }
          else {
            clone[key] = value!=null ? value : null;
          }
        }
      }
    }
  }

  if (clone!=null) {
    if (debug) console.log(padding+"return clone");
    return clone;
  }

  if (debug) console.log(padding+"return object");
  return object;
}

You then use something like this:

// inside an ace editor event: 
var debug = true;
var clonedEvent = document.removeReferences(event, 0, null, null, true, debug);

No comments:

Post a Comment