UNPKG

web-worker-helper

Version:

Utilities for running tasks on worker threads

36 lines (35 loc) 1.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.removeNontransferableOptions = void 0; /** * Safely stringify JSON (drop non serializable values like functions and regexps) * @param value */ function removeNontransferableOptions(object) { // options.log object contains functions which cannot be transferred // TODO - decide how to handle logging on workers // TODO - warn if options stringification is long return JSON.parse(stringifyJSON(object)); } exports.removeNontransferableOptions = removeNontransferableOptions; function stringifyJSON(v) { var cache = new Set(); return JSON.stringify(v, function (key, value) { if (typeof value === 'object' && value !== null) { if (cache.has(value)) { // Circular reference found try { // If this value does not reference a parent it can be deduped return JSON.parse(JSON.stringify(value)); } catch (err) { // discard key if value cannot be deduped return undefined; } } // Store value in our set cache.add(value); } return value; }); }