UNPKG

web-worker-helper

Version:

Utilities for running tasks on worker threads

32 lines (31 loc) 1.09 kB
/** * Safely stringify JSON (drop non serializable values like functions and regexps) * @param value */ export 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)); } 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; }); }