Skip to content

Commit

Permalink
Global subscriptions (ampproject#629)
Browse files Browse the repository at this point in the history
* Global Subscriptions are safe to apply to the global document
  • Loading branch information
kristoferbaxter authored Aug 24, 2019
1 parent a88f938 commit 6cb6ed3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
4 changes: 2 additions & 2 deletions config/rollup.worker-thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ const ESModules = [
],
}),
replace({
WORKER_DOM_DEBUG: false,
WORKER_DOM_DEBUG: true,
}),
babelPlugin({
transpileToES5: false,
allowConsole: false,
allowConsole: true,
}),
],
},
Expand Down
20 changes: 6 additions & 14 deletions src/main-thread/commands/event-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,34 +138,26 @@ export const EventSubscriptionProcessor: CommandExecutorInterface = (strings, no
* @param index number in the listeners array this event corresponds to.
*/
const processListenerChange = (target: RenderableElement, addEvent: boolean, type: string, index: number): void => {
if (target === nodeContext.baseElement) {
addEvent ? addEventListener(type, (knownListeners[index] = eventHandler(1))) : removeEventListener(type, knownListeners[index]);
return;
}

let changeEventSubscribed: boolean = target.onchange !== null;
const shouldTrack: boolean = shouldTrackChanges(target as HTMLElement);
const isChangeEvent = type === 'change';
const isResizeEvent = type === 'resize';

if (addEvent) {
if (isResizeEvent && target === nodeContext.baseElement) {
addEventListener(type, (knownListeners[index] = eventHandler(1)));
return;
}

if (isChangeEvent) {
changeEventSubscribed = true;
target.onchange = null;
}
(target as HTMLElement).addEventListener(type, (knownListeners[index] = eventHandler(target._index_)));
} else {
if (isResizeEvent && target === nodeContext.baseElement) {
removeEventListener(type, knownListeners[index]);
return;
}

if (isChangeEvent) {
changeEventSubscribed = false;
}
(target as HTMLElement).removeEventListener(type, knownListeners[index]);
}
if (shouldTrack && !changeEventSubscribed) {
if (shouldTrackChanges(target as HTMLElement) && !changeEventSubscribed) {
applyDefaultChangeListener(workerContext, target as RenderableElement);
}
};
Expand Down

0 comments on commit 6cb6ed3

Please sign in to comment.