domenic / zones

Former home of the zones proposal for JavaScript
205 stars 13 forks source link

Zone Solutions.md doesn't always delegate upward to parent zones #5

Open domenic opened 8 years ago

domenic commented 8 years ago

Error handling and timer counting should probably do this.

bmeck commented 8 years ago

Would linking the props at construction and using prototypes be a valid workflow for this?

const zoneProps = new WeakMap();

class ZoneWithStorage extends Zone {
  constructor(options, props = Object.create(null)) {
    super(options);
    // object we will be assigning props to
    let base = null;
    let parent = this.parent;
    while (parent !== null) {
      if (parent instanceof ZoneWithStorage) {
        base = Object.create(zoneProps.get(parent));
        break;
      }
    }
    if (base === null) {
      base = {};
    }
    zoneProps.set(this, Object.assign(base, props));
  }

  get(key) {
    const props = zoneProps.get(this);

    if (key in props) {
      return props[key];
    }

    // undefined
  }

  // maybe implement has(key) too if you want.
}
domenic commented 8 years ago

Zone local storage example already delegates upward. No need for complicated proto tricks there.

mhevery commented 8 years ago

Our first implementation of https://github.com/angular/zone.js#deprecated-zonejs-pre-v0515 did exactly that (prototype tricks) and it was a mistake.

1.) slow 2.) can't handle proper bubbling of events. You need ZoneDelegate for proper bubbling/interception.