From b61a20291634e124f55ea444728c515b863c71d0 Mon Sep 17 00:00:00 2001 From: Kabir Shah Date: Sun, 7 Apr 2019 00:52:51 -0700 Subject: [PATCH] async executor --- packages/moon/dist/moon.js | 34 +++++++--- packages/moon/dist/moon.min.js | 2 +- packages/moon/src/compiler/executor.js | 19 ++++-- .../moon/src/compiler/generator/generator.js | 2 +- packages/moon/src/index.js | 63 ++++++++++++------- 5 files changed, 83 insertions(+), 37 deletions(-) diff --git a/packages/moon/dist/moon.js b/packages/moon/dist/moon.js index 985fff0d..f40e4c46 100644 --- a/packages/moon/dist/moon.js +++ b/packages/moon/dist/moon.js @@ -574,7 +574,7 @@ childrenCreate += instruction(instructions.appendElement, [childCode.createVar, elementVar]); childrenUpdate += childCode.update; childrenDestroy += childCode.destroy; - total += childCode.total; + total = childCode.total; } return { @@ -659,14 +659,18 @@ * done. It runs the instructions over multiple frames to allow the browser to * handle other high-priority events. * + * @param {number} index * @param {number} start + * @param {Object} data * @param {string} code * @param {Function} next */ - function execute(start, data, code, next) { - main: for (var i = start; i < code.length;) { + function execute(index, start, data, code, next) { + var i = index; + + while (i < code.length) { switch (code.charCodeAt(i)) { case instructions.createElement: { @@ -736,9 +740,16 @@ case instructions.returnVar: { next(executeGet(code.charCodeAt(++i), data)); - break; + return; } } + + if (performance.now() - start >= 8) { + requestAnimationFrame(function () { + execute(i, performance.now(), data, code, next); + }); + break; + } } } @@ -752,13 +763,14 @@ * with the new element. * * @param {Function} [next] + * @param {number} [start] */ - function create(next) { + function create(next, start) { var _this = this; this.view.data(); - execute(0, this, this.view.create, function (element) { + execute(0, start === undefined ? performance.now() : start, this, this.view.create, function (element) { _this.emit("create", element); if (next !== undefined) { @@ -771,17 +783,18 @@ * * @param {Object} data * @param {Function} [next] + * @param {number} [start] */ - function update(data, next) { + function update(data, next, start) { var _this2 = this; for (var key in data) { this[key] = data[key]; } - execute(this.view.update, function () { + execute(0, start === undefined ? performance.now() : start, this, this.view.update, function () { _this2.emit("update"); if (next !== undefined) { @@ -793,13 +806,14 @@ * Destroys the view over multiple frames and calls the given function. * * @param {Function} [next] + * @param {number} [start] */ - function destroy(next) { + function destroy(next, start) { var _this3 = this; - execute(this.view.destroy, function () { + execute(0, start === undefined ? performance.now() : start, this, this.view.destroy, function () { _this3.emit("destroy"); if (next !== undefined) { diff --git a/packages/moon/dist/moon.min.js b/packages/moon/dist/moon.min.js index e846db03..d30e3c89 100644 --- a/packages/moon/dist/moon.min.js +++ b/packages/moon/dist/moon.min.js @@ -4,4 +4,4 @@ * Released under the MIT License * https://kbrsh.github.io/moon */ -!function(e,t){"undefined"==typeof module?e.Moon=t():module.exports=t()}(this,function(){"use strict";var A=/<([\w\d-_]+)([^>]*?)(\/?)>/g,E=/\s*([\w\d-_]*)(?:=(?:("[^"]*"|'[^']*')|{([^{}]*)}))?/g;function t(e){e=e.trim();for(var t=[],r=0;r",r+2),i=e.slice(r+2,o);0,t.push({type:"tagClose",value:i}),r=o+1;continue}if("!"===a&&"-"===e[r+2]&&"-"===e[r+3]){var s=e.indexOf("--\x3e",r+4);0,r=s+3;continue}A.lastIndex=r;var d=A.exec(e);0;for(var u=d[0],c=d[1],v=d[2],p=d[3],f={},h=void 0;null!==(h=E.exec(v));){var l=h[0],m=h[1],y=h[2],C=h[3];0===l.length?E.lastIndex+=1:f[m]=void 0===C?y:C}t.push({type:"tagOpen",value:c,attributes:f,closed:"/"===p}),r+=u.length}else if("{"===n){var g="";for(r+=1;r]*?)(\/?)>/g,E=/\s*([\w\d-_]*)(?:=(?:("[^"]*"|'[^']*')|{([^{}]*)}))?/g;function t(e){e=e.trim();for(var t=[],r=0;r",r+2),i=e.slice(r+2,o);0,t.push({type:"tagClose",value:i}),r=o+1;continue}if("!"===a&&"-"===e[r+2]&&"-"===e[r+3]){var s=e.indexOf("--\x3e",r+4);0,r=s+3;continue}A.lastIndex=r;var d=A.exec(e);0;for(var u=d[0],c=d[1],v=d[2],p=d[3],f={},h=void 0;null!==(h=E.exec(v));){var l=h[0],m=h[1],y=h[2],C=h[3];0===l.length?E.lastIndex+=1:f[m]=void 0===C?y:C}t.push({type:"tagOpen",value:c,attributes:f,closed:"/"===p}),r+=u.length}else if("{"===n){var g="";for(r+=1;r= 8) { + requestAnimationFrame(() => { + execute(i, performance.now(), data, code, next); + }); + + break; + } } } diff --git a/packages/moon/src/compiler/generator/generator.js b/packages/moon/src/compiler/generator/generator.js index ce8eb055..b9cf51eb 100644 --- a/packages/moon/src/compiler/generator/generator.js +++ b/packages/moon/src/compiler/generator/generator.js @@ -85,7 +85,7 @@ function generateAll(tree, data, total) { childrenDestroy += childCode.destroy; - total += childCode.total; + total = childCode.total; } return { diff --git a/packages/moon/src/index.js b/packages/moon/src/index.js index 77e05fb6..b2a330c1 100644 --- a/packages/moon/src/index.js +++ b/packages/moon/src/index.js @@ -15,17 +15,24 @@ const components = {}; * with the new element. * * @param {Function} [next] + * @param {number} [start] */ -function create(next) { +function create(next, start) { this.view.data(); - execute(0, this, this.view.create, (element) => { - this.emit("create", element); - - if (next !== undefined) { - next(element); + execute( + 0, + start === undefined ? performance.now() : start, + this, + this.view.create, + (element) => { + this.emit("create", element); + + if (next !== undefined) { + next(element); + } } - }); + ); } /** @@ -33,34 +40,48 @@ function create(next) { * * @param {Object} data * @param {Function} [next] + * @param {number} [start] */ -function update(data, next) { +function update(data, next, start) { for (let key in data) { this[key] = data[key]; } - execute(this.view.update, () => { - this.emit("update"); - - if (next !== undefined) { - next(); + execute( + 0, + start === undefined ? performance.now() : start, + this, + this.view.update, + () => { + this.emit("update"); + + if (next !== undefined) { + next(); + } } - }); + ); } /** * Destroys the view over multiple frames and calls the given function. * * @param {Function} [next] + * @param {number} [start] */ -function destroy(next) { - execute(this.view.destroy, () => { - this.emit("destroy"); - - if (next !== undefined) { - next(); +function destroy(next, start) { + execute( + 0, + start === undefined ? performance.now() : start, + this, + this.view.destroy, + () => { + this.emit("destroy"); + + if (next !== undefined) { + next(); + } } - }); + ); } /**