From ca32540960ddbfd31ecd8314440529216cdaf862 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Fri, 13 Nov 2020 16:46:06 +0100 Subject: [PATCH] load the sink just in time --- memory.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/memory.js b/memory.js index 4d8aa4f..d4e16c2 100644 --- a/memory.js +++ b/memory.js @@ -1,21 +1,28 @@ /* eslint-disable camelcase */ -var MessageChannel = global.MessageChannel -if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads')) function sodium_malloc (n) { return new Uint8Array(n) } -const sink = new MessageChannel() function sodium_free (n) { sodium_memzero(n) - sink.port1.postMessage(n.buffer, [n.buffer]) + loadSink().port1.postMessage(n.buffer, [n.buffer]) } function sodium_memzero (arr) { arr.fill(0) } +var sink + +function loadSink () { + if (sink) return sink + var MessageChannel = global.MessageChannel + if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads')) + sink = new MessageChannel() + return sink +} + module.exports = { sodium_malloc, sodium_free,