Skip to content

Commit

Permalink
Merge e3a980e into 588ff8c
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Feb 12, 2020
2 parents 588ff8c + e3a980e commit b0e4c7c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 62 deletions.
2 changes: 1 addition & 1 deletion modules/core/src/lib/loader-utils/parse-with-worker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global __VERSION__ */ // __VERSION__ is injected by babel-plugin-version-inline
import {toArrayBuffer} from '../../javascript-utils/binary-utils';
import WorkerFarm from '../../worker-utils/worker-farm';
import {getTransferList} from '../../worker-utils/get-transfer-list';
import {getTransferList} from '@loaders.gl/loader-utils';
import {parse} from '../parse';

const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
Expand Down
41 changes: 0 additions & 41 deletions modules/core/src/worker-utils/get-transfer-list.js

This file was deleted.

2 changes: 1 addition & 1 deletion modules/core/src/worker-utils/worker-thread.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global Worker */
import {getWorkerURL} from './get-worker-url';
import {getTransferList} from './get-transfer-list';
import {getTransferList} from '@loaders.gl/loader-utils';

let count = 0;

Expand Down
17 changes: 0 additions & 17 deletions modules/core/src/worker-utils/worker-utils.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
/* global URL, Blob */
import assert from '../utils/assert';

export function getTransferList(object, recursive = true, transfers = []) {
if (!object) {
// ignore
} else if (object instanceof ArrayBuffer) {
transfers.push(object);
} else if (object.buffer && object.buffer instanceof ArrayBuffer) {
// Typed array
transfers.push(object.buffer);
} else if (recursive && typeof object === 'object') {
for (const key in object) {
// Avoid perf hit - only go one level deep
getTransferList(object[key], recursive, transfers);
}
}
return transfers;
}

const workerURLCache = new Map();

// Creates a URL from worker source that can be used to create `Worker` instances
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ function isTransferable(object) {
if (typeof MessagePort !== 'undefined' && object instanceof MessagePort) {
return true;
}
if (typeof ImageBitmap !== `undefined` && object instanceof ImageBitmap) {
if (typeof ImageBitmap !== 'undefined' && object instanceof ImageBitmap) {
return true;
}
if (typeof OffscreenCanvas !== `undefined` && object instanceof OffscreenCanvas) {
if (typeof OffscreenCanvas !== 'undefined' && object instanceof OffscreenCanvas) {
return true;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions modules/loader-utils/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import './lib/request-utils/request-scheduler.spec';

import './categories/mesh/mesh-utils.spec';

import './worker-utils/get-transfer-list.spec';
import './worker-utils/validate-loader-version.spec';
48 changes: 48 additions & 0 deletions modules/loader-utils/test/worker-utils/get-transfer-list.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import test from 'tape-promise/tape';
import {getTransferList} from '@loaders.gl/loader-utils';

test('getTransferList', t => {
const typedArray = new Uint8Array(4);
const typedArray2 = new Float32Array(typedArray.buffer);
/* global MessageChannel */
const messageChannel = typeof MessageChannel !== 'undefined' && new MessageChannel();

const TEST_CASES = [
{
title: 'empty',
input: null,
output: []
},
{
title: 'plain JS object',
input: {a: 1, b: 2},
output: []
},
{
title: 'ArrayBuffer',
input: typedArray.buffer,
output: [typedArray.buffer]
},
{
title: 'TypedArray',
input: {result: {data: typedArray}},
output: [typedArray.buffer]
},
{
title: 'TypedArrays with same underlying buffer',
input: [typedArray, typedArray2],
output: [typedArray.buffer]
},
messageChannel && {
title: 'MessagePort',
input: messageChannel,
output: [messageChannel.port1, messageChannel.port2]
}
].filter(Boolean);

for (const testCase of TEST_CASES) {
t.deepEqual(getTransferList(testCase.input), testCase.output, testCase.title);
}

t.end();
});

0 comments on commit b0e4c7c

Please sign in to comment.