Skip to content

Commit

Permalink
Completed documentation and typescript definition files. TypeScript c…
Browse files Browse the repository at this point in the history
…ompiler is used to generate d.ts files from jsdoc descriptions
  • Loading branch information
kaisalmen committed Jul 23, 2020
1 parent 023f8ab commit addd3d4
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 32 deletions.
86 changes: 86 additions & 0 deletions examples/jsm/loaders/TaskManager.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
export class TaskManager {
constructor(maxParallelExecutions?: number);
taskTypes: Map<string, WorkerTypeDefinition>;
verbose: boolean;
maxParallelExecutions: number;
actualExecutionCount: number;
storedExecutions: StoredExecution[];
setVerbose(verbose: boolean): TaskManager;
setMaxParallelExecutions(maxParallelExecutions: number): TaskManager;
getMaxParallelExecutions(): number;
supportsTaskType(taskType: string): boolean;
registerTaskType(taskType: string, initFunction: Function, executeFunction: Function, comRoutingFunction: Function, fallback: boolean, dependencyUrls?: string[]): TaskManager;
registerTaskTypeModule(taskType: string, workerModuleUrl: string): TaskManager;
initTaskType(taskType: string, config: object, transferables?: Transferable[]): Promise<void | TaskWorker[]>;
enqueueForExecution(taskType: string, config: object, transferables?: Transferable[]): Promise<any>;
_kickExecutions(): void;
dispose(): TaskManager;
}
declare class WorkerTypeDefinition {
constructor(taskType: string, maximumCount: number, fallback: boolean, verbose?: boolean);
taskType: string;
fallback: boolean;
verbose: boolean;
functions: {
init: {
ref: Function;
code: string;
};
execute: {
ref: Function;
code: string;
};
comRouting: {
ref: Function;
code: string;
};
dependencies: {
urls: URL[];
code: string[];
};
workerModuleUrl: URL;
};
workers: {
code: string[];
instances: TaskWorker[] | MockedTaskWorker[];
available: TaskWorker[] | MockedTaskWorker[];
};
getTaskType(): string;
setFunctions(initFunction: Function, executeFunction: Function, comRoutingFunction?: Function): void;
setDependencyUrls(dependencyUrls: string[]): void;
setWorkerModule(workerModuleUrl: string): void;
isWorkerModule(): boolean;
loadDependencies(): Promise<ArrayBuffer[]>;
generateWorkerCode(dependencies: ArrayBuffer[]): Promise<string[]>;
createWorkers(code: string): Promise<TaskWorker[]>;
createWorkerModules(): Promise<TaskWorker[]>;
initWorkers(instances: TaskWorker[] | MockedTaskWorker[], config: object, transferables: Transferable[]): Promise<TaskWorker[]>;
getAvailableTask(): TaskWorker | MockedTaskWorker | undefined;
hasTask(): boolean;
returnAvailableTask(taskWorker: TaskWorker | MockedTaskWorker): void;
dispose(): void;
}
declare class StoredExecution {
constructor(taskType: string, config: object, resolve: Function, reject: Function, transferables?: Transferable[]);
taskType: string;
config: any;
resolve: Function;
reject: Function;
transferables: Transferable[];
}
declare class TaskWorker extends Worker {
constructor(id: number, aURL: string, options?: object);
id: number;
getId(): number;
}
declare class MockedTaskWorker {
constructor(id: number, initFunction: Function, executeFunction: Function);
id: number;
functions: {
init: Function;
execute: Function;
};
getId(): number;
postMessage(message: string, transfer?: Transferable[]): void;
}
export {};
102 changes: 72 additions & 30 deletions examples/jsm/loaders/TaskManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ class TaskManager {
*/
constructor ( maxParallelExecutions ) {

/**
* @type {Map<string, WorkerTypeDefinition>}
*/
this.taskTypes = new Map();
this.verbose = false;
this.maxParallelExecutions = maxParallelExecutions ? maxParallelExecutions : 4;
this.actualExecutionCount = 0;
this.storedPromises = [];
/**
* @type {StoredExecution[]}
*/
this.storedExecutions = [];

}

Expand Down Expand Up @@ -150,30 +156,22 @@ class TaskManager {

let localPromise = new Promise( ( resolveUser, rejectUser ) => {

this.storedPromises.push( {
taskType: taskType,
config: config,
transferables: transferables,
resolve: resolveUser,
reject: rejectUser
} );

} )
this.storedExecutions.push( new StoredExecution( taskType, config, resolveUser, rejectUser, transferables ) );

} );
this._kickExecutions();

return localPromise;

}

_kickExecutions () {

while ( this.actualExecutionCount < this.maxParallelExecutions && this.storedPromises.length > 0 ) {
while ( this.actualExecutionCount < this.maxParallelExecutions && this.storedExecutions.length > 0 ) {

let storedExec = this.storedPromises.shift();
if ( storedExec ) {
let storedExecution = this.storedExecutions.shift();
if ( storedExecution ) {

let workerTypeDefinition = this.taskTypes.get( storedExec.taskType );
let workerTypeDefinition = this.taskTypes.get( storedExecution.taskType );
if ( workerTypeDefinition.hasTask() ) {

let taskWorker = workerTypeDefinition.getAvailableTask();
Expand All @@ -186,28 +184,28 @@ class TaskManager {
taskWorker.postMessage( {
cmd: "execute",
id: taskWorker.getId(),
config: storedExec.config
}, storedExec.transferables );
config: storedExecution.config
}, storedExecution.transferables );

} );
promiseWorker.then( ( e ) => {

workerTypeDefinition.returnAvailableTask( taskWorker );
storedExec.resolve( e.data );
storedExecution.resolve( e.data );
this.actualExecutionCount --;
this._kickExecutions();

} ).catch( ( e ) => {

storedExec.reject( "Execution error: " + e );
storedExecution.reject( "Execution error: " + e );

} );

}
else {

// try later again, add at the end for now
this.storedPromises.push( storedExec );
this.storedExecutions.push( storedExecution );

}

Expand Down Expand Up @@ -241,33 +239,38 @@ class WorkerTypeDefinition {
/**
* Creates a new instance of {@link WorkerTypeDefinition}.
*
* @param {string} type The name of the registered task type.
* @param {string} taskType The name of the registered task type.
* @param {Number} maximumCount Maximum worker count
* @param {boolean} fallback Set to true if execution should be performed in main
* @param {boolean} [verbose] Set if logging should be verbose
*/
/**
*/
constructor ( taskType, maximumCount, fallback, verbose ) {
this.taskType = taskType;
this.fallback = fallback;
this.verbose = verbose === true;
this.functions = {
init: {
/** @type {function} */
ref: null,
/** @type {string} */
code: null
},
execute: {
/** @type {function} */
ref: null,
/** @type {string} */
code: null
},
comRouting: {
/** @type {function} */
ref: null,
/** @type {string} */
code: null
},
dependencies: {
/** @type {URL[]} */
urls: [],
/** @type {string[]} */
code: []
},
/**
Expand All @@ -278,8 +281,11 @@ class WorkerTypeDefinition {


this.workers = {
/** @type {string[]} */
code: [],
/** @type {TaskWorker[]|MockedTaskWorker[]} */
instances: new Array ( maximumCount ),
/** @type {TaskWorker[]|MockedTaskWorker[]} */
available: []
};

Expand Down Expand Up @@ -383,6 +389,7 @@ class WorkerTypeDefinition {
}

/**
* Uses the configured values for init, execute and comRouting and embeds it in necessary glue code.
*
* @param {ArrayBuffer[]} dependencies
* @return {Promise<String[]>}
Expand All @@ -406,6 +413,7 @@ class WorkerTypeDefinition {
}

/**
* Creates workers based on the configured function and dependency strings.
*
* @param {string} code
* @return {Promise<TaskWorker[]>}
Expand Down Expand Up @@ -440,6 +448,7 @@ class WorkerTypeDefinition {
}

/**
* Creates module workers.
*
* @return {Promise<TaskWorker[]>}
*/
Expand All @@ -456,6 +465,7 @@ class WorkerTypeDefinition {
}

/**
* Initialises all workers with common configuration data.
*
* @param {TaskWorker[]|MockedTaskWorker[]} instances
* @param {object} config
Expand Down Expand Up @@ -533,6 +543,32 @@ class WorkerTypeDefinition {

}

/**
* Contains all things required for later executions of Worker.
*/
class StoredExecution {

/**
* Creates a new instance.
*
* @param {string} taskType
* @param {object} config
* @param {Function} resolve
* @param {Function} reject
* @param {Transferable[]} [transferables]
*/
constructor( taskType, config, resolve, reject, transferables ) {

this.taskType = taskType;
this.config = config;
this.resolve = resolve;
this.reject = reject;
this.transferables = transferables;

}

}

/**
* Extends the {@link Worker} with an id.
*/
Expand All @@ -553,8 +589,8 @@ class TaskWorker extends Worker {
}

/**
*
* @return {*}
* Returns the id.
* @return {number}
*/
getId() {

Expand Down Expand Up @@ -588,8 +624,8 @@ class MockedTaskWorker {
}

/**
*
* @return {*}
* Returns the id.
* @return {number}
*/
getId() {

Expand All @@ -599,9 +635,10 @@ class MockedTaskWorker {

/**
* Delegates the message to the registered functions
* @param message
* @param {String} message
* @param {Transferable[]} [transfer]
*/
postMessage( message ) {
postMessage( message, transfer ) {

let scope = this;
let comRouting = function ( message ) {
Expand All @@ -627,6 +664,11 @@ class MockedTaskWorker {

}

/**
* Mocking termination
*/
terminate () {}

}


Expand Down
21 changes: 21 additions & 0 deletions examples/jsm/loaders/obj2/utils/ResourceDescriptor.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export class ResourceDescriptor {
constructor(url: string);
url: URL;
path: string;
filename: string;
extension: string;
buffer: ArrayBuffer;
needStringOutput: boolean;
compressed: boolean;
getUrl(): URL;
getPath(): string;
getFilename(): string;
getExtension(): null | string;
setNeedStringOutput(needStringOutput: boolean): ResourceDescriptor;
isNeedStringOutput(): boolean;
setCompressed(compressed: boolean): ResourceDescriptor;
isCompressed(): boolean;
setBuffer(buffer: ArrayBuffer): ResourceDescriptor;
getBuffer(): ArrayBuffer;
getBufferAsString(): string;
}
1 change: 1 addition & 0 deletions examples/jsm/loaders/obj2/utils/ResourceDescriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ResourceDescriptor {
let filenameParts = this.filename.split( '.' );
if ( filenameParts.length > 1 ) this.extension = filenameParts[ filenameParts.length - 1 ];

/** @type {ArrayBuffer} */
this.buffer = null;
this.needStringOutput = false;
this.compressed = false;
Expand Down
Loading

0 comments on commit addd3d4

Please sign in to comment.