Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

feature: add MatchMedia Zone support#543

Closed
poxrud wants to merge 1 commit intoangular:masterfrom
poxrud:MatchMedia-zone-patch
Closed

feature: add MatchMedia Zone support#543
poxrud wants to merge 1 commit intoangular:masterfrom
poxrud:MatchMedia-zone-patch

Conversation

@poxrud
Copy link
Copy Markdown
Contributor

@poxrud poxrud commented Dec 14, 2016

Add MatchMedia (js media queries) Zone support. Patches MediaQueryList.addListener and MediaQueryList.removeListener. Closes issue #243.

@JiaLiPassion
Copy link
Copy Markdown
Collaborator

In your PR, you patch event listener with bindArgument, it will make the event handler in zone, but will not trigger zoneSpec's scheduleTask/invokeTask/cancelTask callback.

there is already a event listener patch logic in utils.ts
I suggest you just use it.
so maybe your patch will just look like this. It is much more simple and can be treaded as eventTask.

    patchMethod(MatchMedia.prototype, ADD_EVENT_LISTENER, () => zoneAwareAddEventListener);
    patchMethod(MatchMedia.prototype, REMOVE_EVENT_LISTENER, () => zoneAwareRemoveEventListener);

@poxrud poxrud force-pushed the MatchMedia-zone-patch branch from 38aa29a to fdccd11 Compare December 15, 2016 00:11
@poxrud
Copy link
Copy Markdown
Contributor Author

poxrud commented Dec 15, 2016

@JiaLiPassion Even though MediaQueryList's addListener() and removeListener() have similar looking names to EventTarget' addEventListener() and removeEventListener(), they have completely different method signatures and behavior. Therefore you cannot use the existing event listener patch logic in utils.ts.

@JiaLiPassion
Copy link
Copy Markdown
Collaborator

Yes, you are right, but I still believe we should use the common logic, I will make a PR to generalize the zoneAwareAddEventListener part, I just run a little test about MediaQuery with the generalized version, it worked, so with common logic, we can add other listener patch later.

@JiaLiPassion
Copy link
Copy Markdown
Collaborator

I just make a PR for a more generic way to patch event target related methods.
so you can patch MediaQueryList with following logic.

patchEventTarget(MediaQuerlList.prototype, 'addListener', 'removeListener', (self, args) => {
    return {
     useCapturing: false,
     eventName: 'mediaQuery',
     handler: args[0],
     target: self || _global,
     name: 'mediaQuery',
     invokeAddFunc: function (addFnSymbol: any, delegate: Task | NestedEventListenerOrEventListenerObject) {
         if (delegate && (<Task>delegate).invoke) {
           return this.target[addFnSymbol]((<Task>delegate).invoke);
         } else {
           return this.target[addFnSymbol](delegate);
         }
     },
     invokeRemoveFunc: function (removeFnSymbol:any, delegate: Task | NestedEventListenerOrEventListenerObject) {
       if (delegate && (<Task>delegate).invoke) {
         return this.target[removeFnSymbol]((<Task>delegate).invoke);
       } else {
         return this.target[removeFnSymbol](delegate);
       }
     }
   };
})

Is that make sense?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants