UNPKG

spincycle

Version:

A reactive message router and object manager that lets clients subscribe to object property changes on the server

157 lines (131 loc) 5.85 kB
<script src="../../bower_components/q/q.js"></script> <script src="../../bower_components/uuid4/lib/browser/index.js"></script> <script src="../../bower_components/lrucache/index.js"></script> <script src="/socket.io/socket.io.js"></script> <!-- <script src="http://localhost:6006/socket.io/socket.io.js"></script> --> <script src="spinclient.js"></script> <dom-module id="ge-login"> <template> <style> </style> </template> <script> Polymer({ is: 'spin-client', properties: { client:{type:'Object', notify: true}, user: {type:'Object', notify: true, observer: 'onUserChanged'}, failure:{type:'String', notify:true}, serveruri:{type:String} }, listeners: { 'user-changed': 'onUserChanged' }, observers: [ 'onUserObserved(user.*)' ], ready: function() { }, onUserObserved:function(u) { //console.log('spin-client onUserObserved') //console.dir(u) //this.saveUser() }, onUserChanged: function ( e ) { //console.log( '********************* spin-client onUserChanged called.' ) if (e && e.id && !this.setting) { this.setting = true if (e.sid) { this.client.setSessionId( e.sid ) } this.set( 'user', e ) //console.dir(this.user) //this.saveUser() if (this.subscriptionid) { this.client.deRegisterObjectsSubscriber( this.subscriptionid, this.user ) } this.client.registerObjectSubscriber( { id: this.user.id, type: this.user.type, cb: function ( nm ) { //console.log( '*** spin-client got user update ***' ) //console.dir( nm ) }.bind( this ) } ).then( function ( sid ) { console.log( 'myview model subscribed' ) this.subscriptionid = sid }.bind( this ) ) this.setting = false } }, saveUser: function() { /* if(!this.saving) { this.saving = true //console.log('---- saving user..') this.client.emitMessage( { target: '_updateGEUser', obj: this.user } ).then( function ( ures ) { setTimeout( function(){ //console.log( 'user update result: ' + ures ) this.saving = false }.bind(this),100 ) }.bind( this ) ) } else { //console.log('---- waiting to save user becasue user is already being saved') setTimeout(function() { //console.log('---- tryin again to save user...') this.saveUser() }.bind(this), 100) } */ xyzzy }, registerObjectSubscriber: function(detail) { window.spinclient.registerObjectSubscriber(detail) }, attached: function() { window.spinclient = new window.SpinClient(this.serveruri || '') console.log('spin-client attached.') window.spinclient.setUser = this.onUserChanged.bind( this ) window.spinclient.onFailure = this.onFailure.bind(this) this.addEventListener('user-changed', this.onUserChanged.bind(this)) window.spinclient.failureListeners = [] window.spinclient.addFailureListener = this.addFailureListener.bind(this) this.set('client', window.spinclient) }, addFailureListener: function(fl) { this.client.failureListeners.push(fl) }, onFailure:function(ftext) { console.log('** spin-client.html onFailure called: '+ftext) this.set('failure', ftext) this.client.failureListeners.forEach(function(cb) { cb(ftext) }) } }); </script> </dom-module>