1.0.3 • Published 6 years ago

@spindox/venice v1.0.3

Weekly downloads
-
License
MPL-2.0
Repository
bitbucket
Last release
6 years ago

Venice JS

Manage your JS channels like a pro Venetian gondolier!

npm.io

Installation

npm install --save @spindox/venice

Usage

Use venice instance in your application:

import venice from '@spindox/venice';

or create your own instance with the exported classes:

import { Venice, VeniceChannel } from '@spindox/venice';

Classes

VeniceChannel

A Venice channel.

An abstraction layer on top of js-channel instance.

Kind: global class

new VeniceChannel(key, options)

ParamTypeDescription
keystringThe channel identifier
optionsChannelOptionsThe channel configuration options

veniceChannel.publish(topic, data, callback)

Publishes a message related to the specified topic.

Kind: instance method of VeniceChannel

ParamTypeDescription
topicstringThe message topic
data*Data to be sent to the other window. The data is serialized using the structured clone algorithm. This means you can pass a broad variety of data objects safely to the destination window without having to serialize them yourself
callbackVeniceCallbackThe message publication callback

Example

const payload = { type: 'geolocation' };
channel.publish('event.hardware', payload, (err, data) => {
  if (err) return err;
  console.log('position', data);
});

veniceChannel.subscribe(topic, handler)

Subscribes a message handler for the specified topic.

Kind: instance method of VeniceChannel

ParamTypeDescription
topicstringThe message topic
handlerSubscriptionHandlerThe message handler

Example

channel.subscribe('event.hardware', (data, tx) => {
  if (data.type === 'geolocation') {
    // async
    getCurrentPosition()
      .then((position) => {
        tx.complete(position);
      })
      .catch((ex) => {
        tx.error('fail', ex);
      });
      tx.delayReturn(true);
  } else {
    // sync
    return 'Sync data';
  }
});

veniceChannel.unsubscribe(topic)

Unsubscribes the handler for the specified topic.

Kind: instance method of VeniceChannel

ParamTypeDescription
topicstringThe message topic

Example

channel.unsubscribe('event.hardware');

veniceChannel.disconnect()

Destroys the channel

Kind: instance method of VeniceChannel
Example

channel.disconnect();

Venice

Contains all Venice channels of the application.

Kind: global class

venice.channel(key, options) ⇒ object

Creates a communication channel between the parent window and the iframe. If it's invoked without options, returns an existing channel.

Kind: instance method of Venice
Returns: object - - A VeniceChannel instance

ParamTypeDefaultDescription
keystringThe channel identifier
optionsChannelOptionsThe channel configuration options

Example

// Set Venice channel on parent
const channel = venice.channel('channel.sample', {
  window: iframe.contentWindow,
  onReady: () => {
    console.log('channel is ready!');
  },
});

Example

// Set Venice channel on iframe
const channel = venice.channel('channel.sample', {});

Example

// Get Venice channel
const channel = venice.channel('channel.sample');

venice.publish(params)

Publishes a message related to the specified topic on the specified channel.

Kind: instance method of Venice

ParamTypeDescription
paramsobject
params.channelstringThe Venice channel identifier
params.topicstringThe message topic
params.data*Data to be sent to the other window. The data is serialized using the structured clone algorithm. This means you can pass a broad variety of data objects safely to the destination window without having to serialize them yourself
params.callbackVeniceCallbackThe message publication callback

Example

venice.publish({  
  channel: 'channel.sample',  
  topic: 'event.hardware',  
  data: { type: 'geolocation' },  
  callback: (err, data) => {  
    if (err) return err;  
    console.log('position', data);  
  });  
});  

venice.subscribe(params)

Subscribes a message handler for the specified topic on the specified channel.

Kind: instance method of Venice

ParamTypeDescription
paramsobject
params.channelstringThe Venice channel identifier
params.topicstringThe message topic
params.handlerSubscriptionHandlerThe message handler

Example

venice.subscribe({  
  channel: 'channel.sample',  
  topic: 'event.hardware',  
  handler: (data, tx) => {  
    if (data.type === 'geolocation') {  
      // async  
      getCurrentPosition()  
        .then((position) => {  
          tx.complete(position);  
        })  
        .catch((ex) => {  
          tx.error('fail', ex);  
        });  
        tx.delayReturn(true);  
    } else {  
      // sync  
      return 'Sync data';  
    }  
  }  
});  

venice.unsubscribe(params)

Unsubscribes the handler for the specified topic on the specified channel.

Kind: instance method of Venice

ParamTypeDescription
paramsobject
params.channelstringThe Venice channel identifier
params.topicstringThe message topic

Example

venice.unsubscribe({
  channel: 'channel.sample',  
  topic: 'event.hardware',  
});

venice.disconnect(key)

Destroys the specified Venice channel.

Kind: instance method of Venice

ParamTypeDescription
keystringThe Venice channel identifier

Example

venice.disconnect('venice.channel');

Functions

Typedefs

SubscriptionHandler(data, transaction)

This method is invoked by the message topic listener. It receives data as the first argument.

If your implementation is asychronous, you'll have to use the transaction object that's automatically passed as the second argument.

Kind: global function

ParamTypeDescription
data*The message data
transactionTransactionThe transaction object

ChannelOptions: object

Venice channel options.

By defaul window is equal to window.parent, origin is equal to *, and scope has the same value of key parameter.

Kind: global typedef
Properties

NameTypeDescription
windowelementA reference to the window to communicate with
originstringSpecifies what the origin of other window must be for the event to be dispatched
scopestringAll messages will be safely tucked under the namespace you provide. If omitted, it's equal to the channel key
onReadyfunctionCallback called once actual communication has been established between the parent page and child frame. If the child frame hadn't set up its end of the channel, for instance, onReady would never get called

VeniceCallback: function

This callback is invoked after that the subscribed listener received the message data, handled it, and, eventually, returned a response.

It's a node-style method, whit the error (or null) as the first argument and the response data as the second argument.

Kind: global typedef

ParamType
errorstring
result*

Transaction: object

Transaction object for asychronous implementations of message.

Kind: global typedef
Properties

NameTypeDescription
completefunctionResolves transaction with data passed as argument
completedfunction
delayReturnfunctionIndicates that the handler implementation is asynchronous
errorfunctionRejects transaction with error (code and message) passed as argument. e.g. tx.error('mega_fail', 'I could not get your stuff.');
invokefunction
originstringContains the origin of the message

##Licence: MPL2.0 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.