webapis-media-query.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2025 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. function patchMediaQuery(Zone) {
  8. Zone.__load_patch('mediaQuery', (global, Zone, api) => {
  9. function patchAddListener(proto) {
  10. api.patchMethod(proto, 'addListener', (delegate) => (self, args) => {
  11. const callback = args.length > 0 ? args[0] : null;
  12. if (typeof callback === 'function') {
  13. const wrapperedCallback = Zone.current.wrap(callback, 'MediaQuery');
  14. callback[api.symbol('mediaQueryCallback')] = wrapperedCallback;
  15. return delegate.call(self, wrapperedCallback);
  16. }
  17. else {
  18. return delegate.apply(self, args);
  19. }
  20. });
  21. }
  22. function patchRemoveListener(proto) {
  23. api.patchMethod(proto, 'removeListener', (delegate) => (self, args) => {
  24. const callback = args.length > 0 ? args[0] : null;
  25. if (typeof callback === 'function') {
  26. const wrapperedCallback = callback[api.symbol('mediaQueryCallback')];
  27. if (wrapperedCallback) {
  28. return delegate.call(self, wrapperedCallback);
  29. }
  30. else {
  31. return delegate.apply(self, args);
  32. }
  33. }
  34. else {
  35. return delegate.apply(self, args);
  36. }
  37. });
  38. }
  39. if (global['MediaQueryList']) {
  40. const proto = global['MediaQueryList'].prototype;
  41. patchAddListener(proto);
  42. patchRemoveListener(proto);
  43. }
  44. else if (global['matchMedia']) {
  45. api.patchMethod(global, 'matchMedia', (delegate) => (self, args) => {
  46. const mql = delegate.apply(self, args);
  47. if (mql) {
  48. // try to patch MediaQueryList.prototype
  49. const proto = Object.getPrototypeOf(mql);
  50. if (proto && proto['addListener']) {
  51. // try to patch proto, don't need to worry about patch
  52. // multiple times, because, api.patchEventTarget will check it
  53. patchAddListener(proto);
  54. patchRemoveListener(proto);
  55. patchAddListener(mql);
  56. patchRemoveListener(mql);
  57. }
  58. else if (mql['addListener']) {
  59. // proto not exists, or proto has no addListener method
  60. // try to patch mql instance
  61. patchAddListener(mql);
  62. patchRemoveListener(mql);
  63. }
  64. }
  65. return mql;
  66. });
  67. }
  68. });
  69. }
  70. patchMediaQuery(Zone);