browser-util.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. export function patchCallbacks(
  9. api: _ZonePrivate, target: any, targetName: string, method: string, callbacks: string[]) {
  10. const symbol = Zone.__symbol__(method);
  11. if (target[symbol]) {
  12. return;
  13. }
  14. const nativeDelegate = target[symbol] = target[method];
  15. target[method] = function(name: any, opts: any, options?: any) {
  16. if (opts && opts.prototype) {
  17. callbacks.forEach(function(callback) {
  18. const source = `${targetName}.${method}::` + callback;
  19. const prototype = opts.prototype;
  20. if (prototype.hasOwnProperty(callback)) {
  21. const descriptor = api.ObjectGetOwnPropertyDescriptor(prototype, callback);
  22. if (descriptor && descriptor.value) {
  23. descriptor.value = api.wrapWithCurrentZone(descriptor.value, source);
  24. api._redefineProperty(opts.prototype, callback, descriptor);
  25. } else if (prototype[callback]) {
  26. prototype[callback] = api.wrapWithCurrentZone(prototype[callback], source);
  27. }
  28. } else if (prototype[callback]) {
  29. prototype[callback] = api.wrapWithCurrentZone(prototype[callback], source);
  30. }
  31. });
  32. }
  33. return nativeDelegate.call(target, name, opts, options);
  34. };
  35. api.attachOriginToPatched(target[method], nativeDelegate);
  36. }