mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(core): queueMacroTask (#8904)
This commit is contained in:
7
packages/core/utils/index.d.ts
vendored
7
packages/core/utils/index.d.ts
vendored
@@ -1,6 +1,7 @@
|
||||
import { dip, px } from '../ui/core/view';
|
||||
|
||||
export * from './mainthread-helper';
|
||||
export * from './macrotask-scheduler';
|
||||
export { Source } from './debug';
|
||||
|
||||
export * from './native-helper';
|
||||
@@ -192,6 +193,12 @@ export function GC();
|
||||
*/
|
||||
export function releaseNativeObject(object: any /*java.lang.Object | NSObject*/);
|
||||
|
||||
/**
|
||||
* Queues the passed function to be ran in a macroTask
|
||||
* @param task the function to execute as a macroTask
|
||||
*/
|
||||
export function queueMacrotask(task: () => void): void;
|
||||
|
||||
/**
|
||||
* Checks if the current thread is the main thread. Directly calls the passed function
|
||||
* if it is, or dispatches it to the main thread otherwise.
|
||||
|
||||
5
packages/core/utils/macrotask-scheduler.d.ts
vendored
Normal file
5
packages/core/utils/macrotask-scheduler.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* Queues the passed function to be ran in a macroTask
|
||||
* @param task the function to execute as a macroTask
|
||||
*/
|
||||
export function queueMacrotask(task: () => void): void;
|
||||
27
packages/core/utils/macrotask-scheduler.ts
Normal file
27
packages/core/utils/macrotask-scheduler.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Trace } from 'trace';
|
||||
import { dispatchToMainThread } from './mainthread-helper';
|
||||
|
||||
let scheduled = false;
|
||||
let macroTaskQueue: Array<() => void> = [];
|
||||
|
||||
function drainMacrotaskQueue() {
|
||||
const currentQueue = macroTaskQueue;
|
||||
macroTaskQueue = [];
|
||||
scheduled = false;
|
||||
currentQueue.forEach((task) => {
|
||||
try {
|
||||
task();
|
||||
} catch (err) {
|
||||
const msg = err ? err.stack || err : err;
|
||||
Trace.write(`Error in macroTask: ${msg}`, Trace.categories.Error, Trace.messageType.error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function queueMacrotask(task: () => void): void {
|
||||
macroTaskQueue.push(task);
|
||||
if (!scheduled) {
|
||||
scheduled = true;
|
||||
dispatchToMainThread(drainMacrotaskQueue);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import * as layout from './layout-helper';
|
||||
|
||||
export { layout };
|
||||
export * from './mainthread-helper';
|
||||
export * from './macrotask-scheduler';
|
||||
|
||||
export const RESOURCE_PREFIX = 'res://';
|
||||
export const FILE_PREFIX = 'file:///';
|
||||
|
||||
Reference in New Issue
Block a user