diff --git a/main.js b/main.js index 46d135d..5710c65 100644 --- a/main.js +++ b/main.js @@ -1,14 +1,18 @@ -import {BaseComponent, Application, compileToFunction} from './new-angular.js'; +import {BaseComponent, Application} from './new-angular.js'; import {setTimeoutPatch} from './new-zonejs/patch-settimeout.js'; +import {queueMicrotaskPatch} from "./new-zonejs/patch-queue-microtask.js"; +import {addEventListenerPatch} from "./new-zonejs/patch-event-listener.js"; + +setTimeoutPatch(); +queueMicrotaskPatch(); +addEventListenerPatch(); class MyComponent extends BaseComponent { constructor() { super("MyComponent"); this.count = 0; this.updateCount = () => { - setTimeout(() => { - this.count++; - }) + this.count++; } this.templateStr = ``; } @@ -26,7 +30,6 @@ app.run(component); /* ---- OR ---- */ -setTimeoutPatch(); setTimeout(() => { component.count += 1; }); diff --git a/new-angular.js b/new-angular.js index e4e6913..46a566c 100644 --- a/new-angular.js +++ b/new-angular.js @@ -79,7 +79,7 @@ export class Application { // This has a LOT of limitations, namely that it's bad // It only handles one element + child text // It only handles (eventName)="$1" and $1 is a templateArgs function syntax -export function compileToFunction(templateStr, templateArgs) { +function compileToFunction(templateStr, templateArgs) { const matchTemplateArgs = str => { if (str[0] === '$') { const key = str.slice(1); @@ -114,4 +114,4 @@ export function compileToFunction(templateStr, templateArgs) { el.innerHTML = textBetweenTags; return el; -} \ No newline at end of file +} diff --git a/new-zonejs/patch-event-listener.js b/new-zonejs/patch-event-listener.js new file mode 100644 index 0000000..00bac9d --- /dev/null +++ b/new-zonejs/patch-event-listener.js @@ -0,0 +1,16 @@ +import {currentZone} from "./index.js"; + +export const addEventListenerPatch = () => { + const originalAddEventListener = EventTarget.prototype.addEventListener; + EventTarget.prototype.addEventListener = function (type, listener, options) { + const context = this; + const originalListener = listener; + + const patchedListener = function (event) { + originalListener.apply(context, [event]); + currentZone.run(); + }; + + return originalAddEventListener.call(this, type, patchedListener, options); + } +} diff --git a/new-zonejs/patch-queue-microtask.js b/new-zonejs/patch-queue-microtask.js new file mode 100644 index 0000000..f576c15 --- /dev/null +++ b/new-zonejs/patch-queue-microtask.js @@ -0,0 +1,14 @@ +import {currentZone} from "./index.js"; + +export const queueMicrotaskPatch = () => { + const originalQueueMicrotask = queueMicrotask; + + queueMicrotask = (callback) => { + const context = this; + + return originalQueueMicrotask(() => { + callback.apply(context); + currentZone.run(); + }); + }; +};