From 656b12954331ce0e2b954797e14032bd762e924f Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 16 Aug 2019 11:09:24 +0200 Subject: [PATCH] Ability to remove handlers from worker-channel and metadata-manager --- www/common/make-backup.js | 7 +++---- www/common/metadata-manager.js | 9 +++++++++ www/common/outer/worker-channel.js | 12 ++++++++++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/www/common/make-backup.js b/www/common/make-backup.js index d9fb62d96..cb6f5bf2b 100644 --- a/www/common/make-backup.js +++ b/www/common/make-backup.js @@ -92,12 +92,11 @@ define([ var opts = { password: pData.password }; - var done = false; - ctx.sframeChan.on("EV_CRYPTGET_PROGRESS", function (data) { - if (done || data.hash !== parsed.hash) { return; } + var handler = ctx.sframeChan.on("EV_CRYPTGET_PROGRESS", function (data) { + if (data.hash !== parsed.hash) { return; } updateProgress.progress(data.progress); if (data.progress === 1) { - done = true; + handler.stop(); updateProgress.progress2(1); } }); diff --git a/www/common/metadata-manager.js b/www/common/metadata-manager.js index 02d516ff7..717705d84 100644 --- a/www/common/metadata-manager.js +++ b/www/common/metadata-manager.js @@ -194,6 +194,15 @@ define(['json.sortify'], function (Sortify) { onChange: function (f) { changeHandlers.push(f); }, onChangeLazy: function (f) { lazyChangeHandlers.push(f); }, onRequestSync: function (f) { syncHandlers.push(f); }, + off: function (name, f) { + var h = []; + if (name === 'change') { h = changeHandlers; } + else if (name === 'lazy') { h = lazyChangeHandlers; } + else if (name === 'title') { h = titleChangeHandlers; } + else if (name === 'sync') { h = syncHandlers; } + var idx = h.indexOf(f); + if (idx !== -1) { h.splice(idx, 1); } + }, isConnected : function () { return members.indexOf(meta.user.netfluxId) !== -1; }, diff --git a/www/common/outer/worker-channel.js b/www/common/outer/worker-channel.js index 3649ab0ea..42b91b802 100644 --- a/www/common/outer/worker-channel.js +++ b/www/common/outer/worker-channel.js @@ -81,17 +81,25 @@ define([ // If the type is a query, your handler will be invoked with a reply function that takes // one argument (the content to reply with). chan.on = function (queryType, handler, quiet) { - (handlers[queryType] = handlers[queryType] || []).push(function (data, msg) { + var h = function (data, msg) { handler(data.content, function (replyContent) { postMsg(JSON.stringify({ txid: data.txid, content: replyContent })); }, msg); - }); + }; + (handlers[queryType] = handlers[queryType] || []).push(h); if (!quiet) { event('EV_REGISTER_HANDLER', queryType); } + return { + stop: function () { + var idx = handlers[queryType].indexOf(h); + if (idx === -1) { return; } + handlers[queryType].splice(idx, 1); + } + }; }; // If a particular handler is registered, call the callback immediately, otherwise it will be called