Skip to content

Commit

Permalink
update XVIZ workers to accept non-envelopped messages
Browse files Browse the repository at this point in the history
  • Loading branch information
marionleborgne committed Mar 29, 2021
1 parent e5c62e0 commit 9c92973
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
5 changes: 3 additions & 2 deletions modules/io/src/common/xviz-data.js
Expand Up @@ -35,15 +35,16 @@ import {XVIZ_FORMAT} from './constants';
// - arraybuffer which is a JSON string
// - JSON object
// - arraybuffer which is a GLB
// opts.messageType is the XVIZ Envelope 'type', i.e. one of ('xviz/state_update', 'xviz/metadata', etc.)
export class XVIZData {
constructor(data) {
constructor(data, messageType = undefined) {
this._data = data;

// _dataFormat is an XVIZ_FORMAT for 'data'
this._dataFormat = undefined;

// _xvizType is the XVIZ Envelope 'type'
this._xvizType = undefined;
this._xvizType = messageType && { type: messageType };

// _message is an XVIZMessage and has been fully parsed
this._message = undefined;
Expand Down
5 changes: 3 additions & 2 deletions modules/parser/src/parsers/parse-xviz-message-sync.js
Expand Up @@ -28,6 +28,7 @@ import parseTimesliceDataV2 from './parse-timeslice-data-v2';
import {getXVIZConfig} from '../config/xviz-config';

// Post processes a stream message to make it easy to use for JavaScript applications
// opts.messageType is the XVIZ Envelope 'type', i.e. one of ('xviz/state_update', 'xviz/metadata', etc.)
export function parseXVIZMessageSync(message, onResult, onError, opts) {
// TODO(twojtasz): better message dispatching
// here, not all arraybuffer may be image (packed point cloud)
Expand All @@ -38,14 +39,14 @@ export function parseXVIZMessageSync(message, onResult, onError, opts) {
}

try {
const xvizData = new XVIZData(message);
const xvizData = new XVIZData(message, opts.messageType);
const xvizMsg = xvizData.message();

// Non-xviz messages will return null
if (xvizMsg) {
const data = xvizMsg.data;

const v2Type = xvizMsg.type || undefined;
const v2Type = opts.messageType || xvizMsg.type || undefined;

const result = parseXVIZData(data, {...opts, v2Type});

Expand Down
7 changes: 4 additions & 3 deletions modules/parser/src/parsers/parse-xviz-message.js
Expand Up @@ -30,7 +30,8 @@ export function parseXVIZMessage({
// worker options
worker = false,
maxConcurrency = 4,
capacity = null
capacity = null,
opts = {}
}) {
if (worker) {
if (!getWorkerFarm()) {
Expand All @@ -44,8 +45,8 @@ export function parseXVIZMessage({
}

const onMessage = data => onResult(postDeserialize(data));
workerFarm.process(message, onMessage, onError);
workerFarm.process(message, onMessage, onError, opts);
} else {
parseXVIZMessageSync(message, onResult, onError);
parseXVIZMessageSync(message, onResult, onError, opts);
}
}
4 changes: 2 additions & 2 deletions modules/parser/src/utils/worker-utils.js
Expand Up @@ -164,8 +164,8 @@ export class WorkerFarm {
}
}

process(data, onResult, onError) {
this.queue.push({data, onResult, onError});
process(data, onResult, onError, opts = {}) {
this.queue.push({data, onResult, onError, opts});
this.next();
}
}
2 changes: 1 addition & 1 deletion modules/parser/src/workers/stream-data-worker.js
Expand Up @@ -70,7 +70,7 @@ export default config => self => {
if (e.data && e.data.xvizConfig) {
setXVIZConfig(e.data.xvizConfig);
} else if (e.data) {
parseXVIZMessageSync(e.data, onResult, onError);
parseXVIZMessageSync(e.data, onResult, onError, e.opts);
}
};
};

0 comments on commit 9c92973

Please sign in to comment.