Add support for ko.tasks API in Knockout.js v3.4.0

Add support for Knockout’s microtask queue. Knockout’s microtask queue
supports scheduling tasks to run as soon as possible while still being
asynchronous, striving to schedule them to occur before yielding for
I/O, reflow, or redrawing.
This commit is contained in:
Matt Brooks
2016-04-28 17:28:40 +01:00
parent 08ed4e9f18
commit 864787b164
2 changed files with 47 additions and 5 deletions
+20 -3
View File
@@ -1,6 +1,6 @@
// Type definitions for Knockout v3.2.0
// Type definitions for Knockout v3.4.0
// Project: http://knockoutjs.com
// Definitions by: Boris Yankov <https://github.com/borisyankov/>, Igor Oleinikov <https://github.com/Igorbek/>, Clément Bourgeois <https://github.com/moonpyk/>
// Definitions by: Boris Yankov <https://github.com/borisyankov/>, Igor Oleinikov <https://github.com/Igorbek/>, Clément Bourgeois <https://github.com/moonpyk/>, Matt Brooks <https://github.com/EnableSoftware>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@@ -387,6 +387,17 @@ interface KnockoutTemplateEngine extends KnockoutNativeTemplateEngine {
rewriteTemplate(template: any, rewriterCallback: Function, templateDocument: Document): void;
}
//////////////////////////////////
// tasks.js
//////////////////////////////////
interface KnockoutTasks {
scheduler: (callback: Function) => any;
schedule(task: Function): number;
cancel(handle: number): void;
runEarly(): void;
}
/////////////////////////////////
interface KnockoutStatic {
@@ -544,7 +555,13 @@ interface KnockoutStatic {
deferUpdates: boolean,
useOnlyNativeEvents: boolean
}
};
/////////////////////////////////
// tasks.js
/////////////////////////////////
tasks: KnockoutTasks;
}
interface KnockoutBindingProvider {
+27 -2
View File
@@ -570,8 +570,8 @@ function test_misc() {
$(element).datepicker("destroy");
});
this.observableFactory = function(): KnockoutObservable<number>{
if (true) {
this.observableFactory = function(flag = true): KnockoutObservable<number>{
if (flag) {
return ko.computed({
read:function(){
return 3;
@@ -655,3 +655,28 @@ function testUnwrapUnion() {
var num = ko.unwrap(possibleObs);
}
function test_tasks() {
// Schedule an empty task
ko.tasks.schedule(function() {
});
// Schedule a task with arguments and return type
let logSomethingTask = (message: string) => {
console.log("Log message");
return true;
};
let taskHandle = ko.tasks.schedule(logSomethingTask);
// Cancel a task
ko.tasks.cancel(taskHandle);
// Process the current microtask queue on demand
ko.tasks.runEarly();
// Redefine or augment how Knockout schedules the event to process and flush the queue
ko.tasks.scheduler = function (callback) {
setTimeout(callback, 0);
};
}