Skip to content

Commit

Permalink
wf
Browse files Browse the repository at this point in the history
  • Loading branch information
lajosbencz committed Feb 18, 2017
1 parent 0f0e8af commit 779577f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 24 deletions.
71 changes: 66 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
### Autobahn wrapper for Vue, served as a plugin

* Calls to _subscribe, register, publish, call, unsubscribe, unregister_ are deferred, so that they are executed as soon as the Session object of Autobahn is available
* Automatic garbage collection for Registration and Subscription objects component-wise
* Global, computed status variables
* Plugin packaging
* [Plugin packaging](#configuration)
* [Global, computed status variables](#global-status)
* [Global, static methods](#static-methods)
* [Vue prototype methods](#prototype-methods)
* [Vue mixin methods](#mixin-methods)
* Automatic garbage collection for Registration and Subscription objects component-wise when used with ```this.$wampSubscribe``` and ```this.$wampRegister``` (acknowledge options is forced)

```
npm install --save vue-wamp
Expand Down Expand Up @@ -38,10 +41,15 @@ Vue.use(VueWamp, {
});
```

### Usage
## Usage

```js
// component.vue
<template>
<div>
<p>Connected: {{ $wampIsConnected }}</p>
</div>
</template>
<script>
export default {
data() {
Expand Down Expand Up @@ -82,7 +90,9 @@ export default {
</script>
```

### Global status
## Global status

```this.$wampIsConnected```, ```this.$wampIsOpen```, ```this.$wampIsRetrying```

```html
<div class="btn-group btn-group-sm">
Expand All @@ -98,4 +108,55 @@ export default {
<span>Retrying...</span>
</a>
</div>
```

## Static methods

```Vue.Wamp.subscribe```, ```Vue.Wamp.publish```, ```Vue.Wamp.register```, ```Vue.Wamp.call```, ```Vue.Wamp.unsubscribe```, ```Vue.Wamp.unregister```

```js
// main.js
Vue.Wamp.subscribe('some-topic', function(args, kwArgs, details) {
// context is empty
}, {
acknowledge: true // option needed for promise
}).then(function(s) {
console.log('AutobahnJS Subscription object: ', s);
});
```

## Prototype methods

```this.$wamp.subscribe```, ```this.$wamp.publish```, ```this.$wamp.register```, ```this.$wamp.call```, ```this.$wamp.unsubscribe```, ```this.$wamp.unregister```

```js
export default {
mounted() {
this.$wamp.subscribe('some-topic', function(args, kwArgs, details) {
// context is still empty
}, {
acknowledge: true // option needed for promise
}).then(function(s) {
console.log('AutobahnJS Subscription object: ', s);
});
}
}
```

## Mixin methods

```this.$wampSubscribe```, ```this.$wampPublish```, ```this.$wampRegister```, ```this.$wampCall```, ```this.$wampUnsubscribe```, ```this.$wampUnregister```

```js
export default {
mounted() {
this.$wampSubscribe('some-topic', function(args, kwArgs, details) {
// context is VueComponent, Subscription will be unsubscribed if component is destroyed
}, {
// acknowledge: true // option not needed anymore, it's forced
}).then(function(s) {
console.log('AutobahnJS Subscription object: ', s);
});
}
}
```
68 changes: 49 additions & 19 deletions lib/vue-wamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module.exports.install = function(Vue, options) {
}
return true;
}
function _close(context, reason, message) {
function _close(reason, message) {
if(_connection.isOpen) {
_detach(context);
_connection.close(reason, message);
Expand Down Expand Up @@ -125,16 +125,19 @@ module.exports.install = function(Vue, options) {
var d = defer();
i.options.acknowledge = true;
_session[i.type](i.name, i.callback, i.options).then(r => {
const k = _key(i.context);
if(!_collect.hasOwnProperty(k)) {
_collect[k] = [];
if(i.context !== null) {
const k = _key(i.context);
if (!_collect.hasOwnProperty(k)) {
_collect[k] = [];
}
_collect[k].push({
name: i.name,
type: 'un' + i.type,
context: i.context,
instance: r
});
_log('$wamp::_relay collected', r);
}
_collect[k].push({
name: i.name,
type: 'un' + i.type,
context: i.context,
instance: r
});
_log('$wamp::_relay resolved', r);
d.resolve(r);
}, d.reject);
Expand Down Expand Up @@ -180,6 +183,7 @@ module.exports.install = function(Vue, options) {
},
methods: {
'$wampSubscribe'(topic, handler, options) {
if(!options || !options.acknowledge) _log('$wamp forcing acknowledge:');
options = Object.assign(options||{}, { acknowledge: true });
_log('$wampSubscribe', topic, options);
return _defer(this, 'subscribe', topic, handler, null, null, options);
Expand All @@ -189,6 +193,7 @@ module.exports.install = function(Vue, options) {
return _defer(this, 'publish', topic, null, args, kwArgs, options);
},
'$wampRegister'(procedure, options) {
if(!options || !options.acknowledge) _log('$wamp forcing acknowledge:');
options = Object.assign(options||{}, { acknowledge: true });
_log('$wampRegister', procedure, endpoint, options);
return _defer(this, 'register', procedure, endpoint, null, null, options);
Expand Down Expand Up @@ -238,14 +243,39 @@ module.exports.install = function(Vue, options) {
}
});

Vue.Wamp = (function(connection, open, close) {
return {
isConnected() { return connection.isConnected },
isOpen() { return connection.isOpen },
isRetrying() { return connection.isRetrying },
open: open,
close: close
};
})(_connection, _open, _close);
const proto = {
isConnected() { return _connection.isConnected },
isOpen() { return _connection.isOpen },
isRetrying() { return _connection.isRetrying },
open: _open,
close: _close,
subscribe(topic, handler, options) {
_log('$wamp.subscribe', topic, options);
return _defer(null, 'subscribe', topic, handler, null, null, options);
},
publish(topic, args, kwargs, options) {
_log('$wamp.publish', topic, args, kwargs, options);
return _defer(null, 'publish', topic, null, args, kwargs, options);
},
call(procedure, args, kwargs, options) {
_log('$wamp.call', topic, args, kwargs, options);
return _defer(null, 'call', procedure, null, args, kwargs, options);
},
register(procedure, endpoint, options) {
_log('$wamp.register', procedure, options);
return _defer(null, 'register', procedure, endpoint, null, null, options);
},
unsubscribe(topic) {
_log('$wamp.unsubscribe', topic, options);
return _defer(null, 'unsubscribe', topic, null, null, null, null);
},
unregister(procedure) {
_log('$wamp.unregister', procedure, options);
return _defer(null, 'unregister', procedure, null, null, null, null);
}
};

Vue.prototype.$wamp = proto;
Vue.Wamp = proto;

};

0 comments on commit 779577f

Please sign in to comment.