blob: a41c57803af9511a1313130ff8b0f9ec8b7f6780 [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
* Redirects a stream to another Vanadium name. It prompts the user to pick
* a Vanadium name before redirecting and allows the user to chose between
* redirecting all the data or just new incoming data.
* @fileoverview
*/
import { Logger } from 'libs/logs/logger'
import { register, trigger } from 'libs/mvc/actions'
import { page } from 'runtime/context'
import { RedirectPipeDialogView } from 'views/redirect-pipe-dialog/view'
import { pipe } from 'services/pipe-to-browser-client'
import { getAll as getAllPublishedP2BNames } from 'services/pipe-to-browser-namespace'
var log = new Logger('actions/redirect-pipe');
var ACTION_NAME = 'redirect-pipe';
/*
* Registers the redirect pipe action
*/
export function registerRedirectPipeAction() {
register(ACTION_NAME, actionHandler);
}
/*
* Triggers the redirect pipe action
* @param {stream} stream Stream object to redirect
* @param {string} currentPluginName name of the current plugin
*/
export function redirectPipe(stream, currentPluginName) {
return trigger(ACTION_NAME, stream, currentPluginName);
}
/*
* Handles the redirect pipe action.
*
* @private
*/
function actionHandler(stream, currentPluginName) {
log.debug('redirect pipe action triggered');
// display a dialog asking user where to redirect and whether to redirect
// all the data or just new data.
var dialog = new RedirectPipeDialogView();
dialog.open();
// if user decides to redirect, copy the stream and pipe it.
dialog.onRedirectAction((name, newDataOnly) => {
var copyStream = stream.copier.copy(newDataOnly);
pipe(name, copyStream).then(() => {
page.showToast('Redirected successfully to ' + name);
}).catch((e) => {
page.showToast('FAILED to redirect to ' + name + '. Please see console for error details.');
log.debug('FAILED to redirect to', name, e);
});
});
// also get the list of all existing P2B names in the namespace and supply it to the dialog
getAllPublishedP2BNames().then((allNames) => {
// append current plugin name to the Vanadium names for better UX
dialog.existingNames = allNames.map((n) => {
return n + '/' + currentPluginName;
});
}).catch((e) => {
log.debug('getAllPublishedP2BNames failed', e);
});
}