diff --git a/DamJS.js b/DamJS.js index 5cd1158..c7be332 100644 --- a/DamJS.js +++ b/DamJS.js @@ -1,275 +1,431 @@ function findPermissioningHandler() { - for (var i in x) { - if (x[i].messages[0].handler.getSubject().indexOf("/PERMISSIONS/MASTER/CONTAINER") !== -1) { - return x[i].messages[0].handler; - } - } + for (var i in x) { + if ( + x[i].messages[0].handler + .getSubject() + .indexOf("/PERMISSIONS/MASTER/CONTAINER") !== -1 + ) { + return x[i].messages[0].handler; + } + } } function disableTrading() { - var tempMessages = []; + var tempMessages = []; + // tempMessages.push({ + // getSubject: function() {return "/PERMISSIONS/MASTER/USER/user2@caplin.com"}, + // getFields: function() {return {PERMISSION_NAMESPACE: "FX_CURRENCY_PAIR_TRADE_LIST", AUTH: "USDDKK~DENY,USDJPY~DENY,EURUSD~DENY", TYPE: "PERMISSION", VALUE: ".*"}}, + // getKey: function() {return "PERMISSION:.*:FX_CURRENCY_PAIR_TRADE_LIST"} + // }); - // tempMessages.push({ - // getSubject: function() {return "/PERMISSIONS/MASTER/USER/user2@caplin.com"}, - // getFields: function() {return {PERMISSION_NAMESPACE: "FX_CURRENCY_PAIR_TRADE_LIST", AUTH: "USDDKK~DENY,USDJPY~DENY,EURUSD~DENY", TYPE: "PERMISSION", VALUE: ".*"}}, - // getKey: function() {return "PERMISSION:.*:FX_CURRENCY_PAIR_TRADE_LIST"} - // }); + function getStringReturner(string) { + return function () { + return string; + }; + } - function getStringReturner(string) { - return function() { - return string; - } - } + function getFieldsReturner(permName, auth, type, value) { + return function () { + return { + PERMISSION_NAMESPACE: permName, + AUTH: auth, + TYPE: type, + VALUE: value, + }; + }; + } - function getFieldsReturner(permName, auth, type, value) { - return function() { - return { - PERMISSION_NAMESPACE: permName, - AUTH: auth, - TYPE: type, - VALUE: value - } - } - } + var username = Object.keys( + findPermissioningHandler().getSubscriptionListener() + ._compositePermissionEngine.m_mEngines.MASTER.m_mUsers + )[0]; + var perms = + findPermissioningHandler().getSubscriptionListener() + ._compositePermissionEngine.m_mEngines.MASTER.m_mUsers[username] + .m_mPermissions; - var username = Object.keys(findPermissioningHandler().getSubscriptionListener()._compositePermissionEngine.m_mEngines.MASTER.m_mUsers)[0]; - var perms = findPermissioningHandler().getSubscriptionListener()._compositePermissionEngine.m_mEngines.MASTER.m_mUsers[username].m_mPermissions; + for (var permContext in perms) { + //.* + var permissions = perms[permContext].m_mPermissions; + for (var permId in permissions) { + //TRADE + var permission = permissions[permId]; - for (var permContext in perms) { //.* - var permissions = perms[permContext].m_mPermissions; - for (var permId in permissions) { //TRADE - var permission = permissions[permId]; + //permission.m_mPermissions["EURUSD"] = "DENY" + var auths = []; + for (var key in permission.m_mPermissions) { + auths.push(key + "~" + permission.m_mPermissions[key]); + } + //auths.push("EURUSD~DENY"); - //permission.m_mPermissions["EURUSD"] = "DENY" - var auths = []; - for (var key in permission.m_mPermissions) { - auths.push(key + "~" + permission.m_mPermissions[key]); - } - //auths.push("EURUSD~DENY"); + tempMessages.push({ + getSubject: getStringReturner("/PERMISSIONS/MASTER/USER/" + username), + getFields: getFieldsReturner( + permission.m_sNamespace, + auths.join(","), + "PERMISSION", + permContext + ), + getKey: getStringReturner("PERMISSION:" + permContext + ":" + permId), + //getKey: function() {return "PERMISSION:.*:FX_CURRENCY_PAIR_TRADE_LIST"} + }); + } + } - tempMessages.push({ - getSubject: getStringReturner("/PERMISSIONS/MASTER/USER/" + username), - getFields: getFieldsReturner(permission.m_sNamespace, auths.join(","), "PERMISSION", permContext), - getKey: getStringReturner("PERMISSION:" + permContext + ":" + permId) - //getKey: function() {return "PERMISSION:.*:FX_CURRENCY_PAIR_TRADE_LIST"} - }); - } - } + var messages = []; - var messages = []; - - a = findPermissioningHandler(); - b = a.getSubscriptionListener(); - messages.push({getSubject: function() {return "/PERMISSIONS/MASTER/CONTROL/START"}, getFields: function() {return {TXN_ID: "999"}}, getKey: function() {return "TXN_ID"}}); - messages.push({getSubject: function() {return "/PERMISSIONS/MASTER/USER/" + username}, getFields: function() {return {PERMISSION_NAMESPACE: "", AUTH: "", TYPE: "ROW_COUNT", VALUE: "3"}}, getKey: function() {return "ROW_COUNT"}}); - var parents = Object.keys(findPermissioningHandler().getSubscriptionListener()._compositePermissionEngine.m_mEngines.MASTER.m_mUsers[username].m_mGroups).join(","); - messages.push({getSubject: function() {return "/PERMISSIONS/MASTER/USER/" + username}, getFields: function() {return {PERMISSION_NAMESPACE: "", AUTH: "", TYPE: "PARENTS", VALUE: parents}}, getKey: function() {return "PARENTS"}}); - for (var i=0; i < tempMessages.length; i++) { - messages.push(tempMessages[i]); - } - //e5 = {getSubject: function() {return "/PERMISSIONS/MASTER/USER/user2@caplin.com"}, getFields: function() {return {AUTH: "FX-TRADE~DENY", PERMISSION_NAMESPACE: "FX_TRADE", TYPE: "PERMISSION", VALUE: ".*"}}, getKey: function() {return "PERMISSION:.*:FX_TRADE"}} - messages.push({getSubject: function() {return "/PERMISSIONS/MASTER/CONTROL/COMMIT"}, getFields: function() {return {TXN_ID: "999"}}, getKey: function() {return "TXN_ID"}}); - for (var i=0; i < messages.length; i++) { - b.onPermissionUpdate(a, messages[i]); - } + a = findPermissioningHandler(); + b = a.getSubscriptionListener(); + messages.push({ + getSubject: function () { + return "/PERMISSIONS/MASTER/CONTROL/START"; + }, + getFields: function () { + return { TXN_ID: "999" }; + }, + getKey: function () { + return "TXN_ID"; + }, + }); + messages.push({ + getSubject: function () { + return "/PERMISSIONS/MASTER/USER/" + username; + }, + getFields: function () { + return { + PERMISSION_NAMESPACE: "", + AUTH: "", + TYPE: "ROW_COUNT", + VALUE: "3", + }; + }, + getKey: function () { + return "ROW_COUNT"; + }, + }); + var parents = Object.keys( + findPermissioningHandler().getSubscriptionListener() + ._compositePermissionEngine.m_mEngines.MASTER.m_mUsers[username].m_mGroups + ).join(","); + messages.push({ + getSubject: function () { + return "/PERMISSIONS/MASTER/USER/" + username; + }, + getFields: function () { + return { + PERMISSION_NAMESPACE: "", + AUTH: "", + TYPE: "PARENTS", + VALUE: parents, + }; + }, + getKey: function () { + return "PARENTS"; + }, + }); + for (var i = 0; i < tempMessages.length; i++) { + messages.push(tempMessages[i]); + } + //e5 = {getSubject: function() {return "/PERMISSIONS/MASTER/USER/user2@caplin.com"}, getFields: function() {return {AUTH: "FX-TRADE~DENY", PERMISSION_NAMESPACE: "FX_TRADE", TYPE: "PERMISSION", VALUE: ".*"}}, getKey: function() {return "PERMISSION:.*:FX_TRADE"}} + messages.push({ + getSubject: function () { + return "/PERMISSIONS/MASTER/CONTROL/COMMIT"; + }, + getFields: function () { + return { TXN_ID: "999" }; + }, + getKey: function () { + return "TXN_ID"; + }, + }); + for (var i = 0; i < messages.length; i++) { + b.onPermissionUpdate(a, messages[i]); + } } function enableTrading() { - alert('currently disabled'); + alert("currently disabled"); } -define(['lib/react', 'DamJSMatcher', 'lib/meld'], function(React, DamJSMatcher, meld) { - function DamJS() { - this.matchers = []; - this.setListeners(); - this.react = null; - this.previous= { - didProceed: true, - time: null, - sequenceNum: null, - collection: null - }; - } - - DamJS.prototype = { - setReact: function(react) { - this.react = react; - this.updateReact(); - }, - clearReact: function() { - this.react = null; - }, - updateReact: function() { - - }, - addNewMatcher: function(matchString) { - for (var i=0; i < this.matchers.length; i++) { - if (this.matchers[i].matchString == matchString) { - return; - } - } - this.matchers.push(new DamJSMatcher(matchString)); - this.update(); - }, - update: function() { - if (this.listener) { - this.listener(); - } - }, - onUpdate: function(fn) { - this.listener = fn; - }, - alreadyExists: function(joinPoint){ - if (joinPoint.target._timeReceived === this.previous.time && this.previous.sequenceNum === joinPoint.target._rttpSequenceNumber){ - return true; - } else{ - this.previous.time = joinPoint.target._timeReceived - this.previous.sequenceNum = joinPoint.target._rttpSequenceNumber - this.previous.collection = [joinPoint]; - return false; - } - }, - handleInjectIncoming: function(matcher, joinPoint) { - matcher.incomingInjectionFields.forEach(function(injectionObj) { - joinPoint.target._fields[injectionObj.keyValue] = injectionObj.fieldValue; - }) - }, - handleInjectOutgoing: function(matcher, joinPoint) { - matcher.incomingInjectionFields.forEach(function(injectionObj) { - joinPoint.args[1][injectionObj.keyValue] = injectionObj.fieldValue; - }) - }, - handleUpdate: function(joinPoint) { - var proceed = true; - if ( !this.alreadyExists(joinPoint)) { - this.matchers.forEach(function(matcher) { - if (matcher.matches(joinPoint)) { - if (matcher.injectIncoming) { - this.handleInjectIncoming(matcher, joinPoint); - } - if (matcher.filterIncoming) { - matcher.addJoinPoint(this.previous.collection); - proceed = false; +define(["lib/react", "DamJSMatcher", "lib/meld"], function ( + React, + DamJSMatcher, + meld +) { + function DamJS() { + this.matchers = []; + this.setListeners(); + this.react = null; + this.previous = { + didProceed: true, + time: null, + sequenceNum: null, + collection: null, + }; + } - } - if (matcher.logIncoming) { - console.log("Incoming:", joinPoint.target.getSubject(), JSON.parse(JSON.stringify(joinPoint.target.getFields()))); - } - } - }. bind(this)) - this.previous.proceed = proceed; - if (proceed) { - joinPoint.proceed(); - } - } else { - this.previous.collection.push(joinPoint) - if(this.previous.proceed){ - joinPoint.proceed(); - } - } - - }, - handlePublish: function(joinPoint) { - var proceed = true; - this.matchers.forEach(function(matcher) { - if (matcher.matches(joinPoint)) { - if (matcher.injectOutgoing) { - //this.handleInjectOutgoing(matcher, joinPoint); - } - if (matcher.filterOutgoing) { - //matcher.addJoinPoint(joinPoint); - proceed = false; - } - if (matcher.logOutgoing) { - console.log("Outgoing:", joinPoint.args[0], joinPoint.args[1]) - } - } - }.bind(this)); - if (proceed) { - joinPoint.proceed(); - } - }, - handleSubscribe: function(joinPoint) { - var proceed = true; - this.matchers.forEach(function(matcher) { - if (matcher.matches(joinPoint)) { - if (matcher.injectOutgoing) { - //this.handleInjectOutgoing(matcher, joinPoint); - } - if (matcher.logOutgoing) { - console.log("Outgoing:", joinPoint.args[0], joinPoint.args[1]) - } - } - }.bind(this)); - if (proceed) { - return joinPoint.proceed(); - } - }, - setListeners: function() { - if (typeof caplin !== "undefined" && typeof caplin.streamlink !== "undefined") { - meld.around( - caplin.streamlink.impl.subscription.SubscriptionManager.prototype, 'send', function(joinPoint) { - var proceed = true; - this.matchers.forEach(function(matcher) { - if (matcher.matches(joinPoint.args[1].subject)) { - if (matcher.filterOutgoing) { - proceed = false; - } - } - }.bind(this)); - if (proceed) { - return joinPoint.proceed(); - } - }.bind(this)); -// meld.around( -// caplin.streamlink.impl.subscription.SubscriptionManager.prototype, 'onUpdate', function(joinPoint) { -// if (typeof x == "undefined") { -// if (joinPoint.args[0] instanceof caplin.streamlink.impl.event.RecordType1EventImpl) { -// x = joinPoint; -// var subs = x.target.subscriptions.subscriptions; -// for (var key in subs) { -// this.addNewMatcher(subs[key].messages[0].handler._subject); -// } -// } -// } -// joinPoint.proceed(); -// }.bind(this)); - meld.around( - caplin.streamlink.impl.StreamLinkCoreImpl.prototype, 'publishToSubject', function(joinPoint) { - this.handlePublish(joinPoint); - }.bind(this)); - meld.around( - caplin.streamlink.impl.event.RecordType1EventImpl.prototype, '_publishSubscriptionResponse', function(joinPoint) { - // CTSL.getSLJS().addConnectionListener({ - // onServiceStatusChange: function() { - // debugger; - // } - // }); - //console.log(services); - x = joinPoint.args[0]._subscriptionManager.subscriptions.subscriptions; - this.handleUpdate(joinPoint); - }.bind(this) - ) - meld.around( - caplin.streamlink.impl.event.PermissionEventImpl.prototype, '_publishSubscriptionResponse', function(joinPoint) { - x = joinPoint.args[0]._subscriptionManager.subscriptions.subscriptions; - this.handleUpdate(joinPoint); - }.bind(this) - ) - meld.around( - caplin.streamlink.StreamLink.prototype, 'subscribe', function(joinPoint) { - this.streamlink = joinPoint.target; - window.damJSStreamLink = this.streamlink; - return this.handleSubscribe(joinPoint); - }.bind(this) - ) - } + DamJS.prototype = { + setReact: function (react) { + this.react = react; + this.updateReact(); + }, + clearReact: function () { + this.react = null; + }, + updateReact: function () {}, + addNewMatcher: function (matchString) { + for (var i = 0; i < this.matchers.length; i++) { + if (this.matchers[i].matchString == matchString) { + return; + } + } + this.matchers.push(new DamJSMatcher(matchString)); + this.update(); + }, + update: function () { + if (this.listener) { + this.listener(); + } + }, + onUpdate: function (fn) { + this.listener = fn; + }, + alreadyExists: function (joinPoint) { + if ( + joinPoint.target._timeReceived === this.previous.time && + this.previous.sequenceNum === joinPoint.target._rttpSequenceNumber + ) { + return true; + } else { + this.previous.time = joinPoint.target._timeReceived; + this.previous.sequenceNum = joinPoint.target._rttpSequenceNumber; + this.previous.collection = [joinPoint]; + return false; + } + }, + handleInjectIncoming: function (matcher, joinPoint) { + matcher.incomingInjectionFields.forEach(function (injectionObj) { + joinPoint.target._fields[injectionObj.keyValue] = + injectionObj.fieldValue; + }); + }, + handleInjectOutgoing: function (matcher, joinPoint) { + matcher.incomingInjectionFields.forEach(function (injectionObj) { + joinPoint.args[1][injectionObj.keyValue] = injectionObj.fieldValue; + }); + }, + handleUpdate: function (joinPoint) { + var proceed = true; + if (!this.alreadyExists(joinPoint)) { + this.matchers.forEach( + function (matcher) { + if (matcher.matches(joinPoint)) { + if (matcher.injectIncoming) { + this.handleInjectIncoming(matcher, joinPoint); + } + if (matcher.filterIncoming) { + matcher.addJoinPoint(this.previous.collection); + proceed = false; + } + if (matcher.logIncoming) { + console.log( + "Incoming:", + joinPoint.target.getSubject(), + JSON.parse(JSON.stringify(joinPoint.target.getFields())) + ); + } + } + }.bind(this) + ); + this.previous.proceed = proceed; + if (proceed) { + joinPoint.proceed(); + } + } else { + this.previous.collection.push(joinPoint); + if (this.previous.proceed) { + joinPoint.proceed(); + } + } + }, + handlePublish: function (joinPoint) { + var proceed = true; + this.matchers.forEach( + function (matcher) { + if (matcher.matches(joinPoint)) { + if (matcher.injectOutgoing) { + this.handleInjectOutgoing(matcher, joinPoint); + } + if (matcher.filterOutgoing) { + //matcher.addJoinPoint(joinPoint); + proceed = false; + } + if (matcher.logOutgoing) { + console.log("Outgoing:", joinPoint.args[0], joinPoint.args[1]); + } + } + }.bind(this) + ); + if (proceed) { + joinPoint.proceed(); + } + }, + handleSubscribe: function (joinPoint) { + var proceed = true; + this.matchers.forEach( + function (matcher) { + if (matcher.matches(joinPoint)) { + if (matcher.injectOutgoing) { + // this.handleInjectOutgoing(matcher, joinPoint); + } + if (matcher.logOutgoing) { + console.log("Outgoing:", joinPoint.args[0], joinPoint.args[1]); + } + } + }.bind(this) + ); + if (proceed) { + return joinPoint.proceed(); + } + }, + setListeners: function () { + // >= StreamLinkTS 7.1.12 + if (window.caplin.streamlink.getVersion) { + meld.around( + caplin.streamlink._streamLinkCore._subscriptionManager, + "send", + function (joinPoint) { + var proceed = true; + this.matchers.forEach( + function (matcher) { + if (matcher.matches(joinPoint.args[1].subject)) { + if (matcher.filterOutgoing) { + proceed = false; + } + } + }.bind(this) + ); + if (proceed) { + return joinPoint.proceed(); + } + }.bind(this) + ); - } - } + meld.around( + caplin.streamlink, + "publishToSubject", + function (joinPoint) { + this.handlePublish(joinPoint); + }.bind(this) + ); + meld.around( + caplin.streamlink._streamLinkCore._subscriptionManager, + "onUpdate", + function (joinPoint) { + var event = joinPoint.args[0]; + var isRecordType1Event = event._getRttpCode().name === "6c"; + var isPermissionEvent = event._getRttpCode().name === "6k"; + if (isRecordType1Event || isPermissionEvent) { + meld.around( + event, + "_publishSubscriptionResponse", + function (joinPoint) { + this.handleUpdate(joinPoint); + }.bind(this) + ); + } + joinPoint.proceed(); + }.bind(this) + ); + meld.around( + caplin.streamlink, + "subscribe", + function (joinPoint) { + return this.handleSubscribe(joinPoint); + }.bind(this) + ); + // <= StreamLinkJS 7.0.4 + } else { + meld.around( + caplin.streamlink.impl.subscription.SubscriptionManager.prototype, + "send", + function (joinPoint) { + var proceed = true; + this.matchers.forEach( + function (matcher) { + if (matcher.matches(joinPoint.args[1].subject)) { + if (matcher.filterOutgoing) { + proceed = false; + } + } + }.bind(this) + ); + if (proceed) { + return joinPoint.proceed(); + } + }.bind(this) + ); + // meld.around( + // caplin.streamlink.impl.subscription.SubscriptionManager.prototype, 'onUpdate', function(joinPoint) { + // if (typeof x == "undefined") { + // if (joinPoint.args[0] instanceof caplin.streamlink.impl.event.RecordType1EventImpl) { + // x = joinPoint; + // var subs = x.target.subscriptions.subscriptions; + // for (var key in subs) { + // this.addNewMatcher(subs[key].messages[0].handler._subject); + // } + // } + // } + // joinPoint.proceed(); + // }.bind(this)); + meld.around( + caplin.streamlink.impl.StreamLinkCoreImpl.prototype, + "publishToSubject", + function (joinPoint) { + this.handlePublish(joinPoint); + }.bind(this) + ); + meld.around( + caplin.streamlink.impl.event.RecordType1EventImpl.prototype, + "_publishSubscriptionResponse", + function (joinPoint) { + // CTSL.getSLJS().addConnectionListener({ + // onServiceStatusChange: function() { + // debugger; + // } + // }); + //console.log(services); + x = + joinPoint.args[0]._subscriptionManager.subscriptions + .subscriptions; + this.handleUpdate(joinPoint); + }.bind(this) + ); + meld.around( + caplin.streamlink.impl.event.PermissionEventImpl.prototype, + "_publishSubscriptionResponse", + function (joinPoint) { + x = + joinPoint.args[0]._subscriptionManager.subscriptions + .subscriptions; + this.handleUpdate(joinPoint); + }.bind(this) + ); + meld.around( + caplin.streamlink.StreamLink.prototype, + "subscribe", + function (joinPoint) { + this.streamlink = joinPoint.target; + window.damJSStreamLink = this.streamlink; + return this.handleSubscribe(joinPoint); + }.bind(this) + ); + } + }, + }; - return DamJS; + return DamJS; }); diff --git a/DamJSMatcher.js b/DamJSMatcher.js index 1d91c61..98a1897 100644 --- a/DamJSMatcher.js +++ b/DamJSMatcher.js @@ -1,163 +1,187 @@ -define(['lib/react'], function(React) { - function DamJSMatcher(matchString) { - this.joinPointsCached = []; - this.joinPointToCollectionCache = {}; - this.matchString = matchString; - this.reacts = []; - this.filterIncoming = false; - this.filterOutgoing = false; - this.injectIncoming = false; - this.injectOutgoing = false; - this.logIncoming = false; - this.logOutgoing = false;; - this.incomingInjectionFields = [{fieldValue: "", keyValue: ""}]; - this.outgoingInjectionFields = [{fieldValue: "", keyValue: ""}]; - } +define(["lib/react"], function (React) { + function DamJSMatcher(matchString) { + this.joinPointsCached = []; + this.joinPointToCollectionCache = {}; + this.matchString = matchString; + this.reacts = []; + this.filterIncoming = false; + this.filterOutgoing = false; + this.injectIncoming = false; + this.injectOutgoing = false; + this.logIncoming = false; + this.logOutgoing = false; + this.incomingInjectionFields = [{ fieldValue: "", keyValue: "" }]; + this.outgoingInjectionFields = [{ fieldValue: "", keyValue: "" }]; + } - DamJSMatcher.prototype = { - setReact: function(listener) { - this.reacts.push(listener) - this.updateReact(); - }, - clearReact: function(listener) { - var listenerIndex = this.reacts.indexOf(listener); - if (listenerIndex) { - this.reacts.slice(listenerIndex,1); - } - }, - updateReact: function() { - this.reacts.forEach(function(react) { - if (react._lifeCycleState !== "UNMOUNTED") { - react.setState({matches: this.joinPointsCached}); - react.setState({incomingInjectionFields: this.incomingInjectionFields}); - react.setState({outgoingInjectionFields: this.outgoingInjectionFields}); - } - }.bind(this)) - }, - toggleIncomingFilter: function() { - this.filterIncoming = !this.filterIncoming; - return this.filterIncoming; - }, - toggleOutgoingFilter: function() { - this.filterOutgoing = !this.filterOutgoing; - return this.filterOutgoing; - }, - isIncomingFiltered: function() { - return this.filterIncoming; - }, - isOutgoingFiltered: function() { - return this.filterOutgoing; - }, - toggleIncomingInjection: function() { - this.injectIncoming = !this.injectIncoming; - return this.injectIncoming; - }, - toggleOutgoingInjection: function() { - this.injectOutgoing = !this.injectOutgoing; - return this.injectOutgoing; - }, - isIncomingInjected: function() { - return this.injectIncoming; - }, - isOutgoingInjected: function() { - return this.injectOutgoing; - }, - toggleIncomingLogging: function() { - this.logIncoming = !this.logIncoming; - return this.logIncoming; - }, - toggleOutgoingLogging: function() { - this.logOutgoing = !this.logOutgoing; - return this.logOutgoing; - }, - isIncomingLogged: function() { - return this.logIncoming; - }, - isOutgoingLogged: function() { - return this.logOutgoing; - }, - addIncomingInjectionField: function() { - this.incomingInjectionFields.push({}); - this.updateReact(); - }, - addOutgoingInjectionField: function() { - this.outgoingInjectionFields.push({}); - this.updateReact(); - }, - updateIncomingInjectionValue: function(row, value) { - this.incomingInjectionFields[row].fieldValue = value; - this.updateReact(); - }, - updateIncomingInjectionKey: function(row, value) { - this.incomingInjectionFields[row].keyValue = value; - this.updateReact(); - }, - updateOutgoingInjectionValue: function(row, value) { - this.outgoingInjectionFields[row].fieldValue = value; - this.updateReact(); - }, - updateOutgoingInjectionKey: function(row, value) { - this.outgoingInjectionFields[row].keyValue = value; - this.updateReact(); - }, - matches: function(joinPoint) { - function isIncoming() { - if (joinPoint && joinPoint.target) { - return joinPoint.target.getSubject - } + DamJSMatcher.prototype = { + setReact: function (listener) { + this.reacts.push(listener); + this.updateReact(); + }, + clearReact: function (listener) { + var listenerIndex = this.reacts.indexOf(listener); + if (listenerIndex) { + this.reacts.slice(listenerIndex, 1); + } + }, + updateReact: function () { + this.reacts.forEach( + function (react) { + if (react._lifeCycleState !== "UNMOUNTED") { + react.setState({ matches: this.joinPointsCached }); + react.setState({ + incomingInjectionFields: this.incomingInjectionFields, + }); + react.setState({ + outgoingInjectionFields: this.outgoingInjectionFields, + }); + } + }.bind(this) + ); + }, + toggleIncomingFilter: function () { + this.filterIncoming = !this.filterIncoming; + return this.filterIncoming; + }, + toggleOutgoingFilter: function () { + this.filterOutgoing = !this.filterOutgoing; + return this.filterOutgoing; + }, + isIncomingFiltered: function () { + return this.filterIncoming; + }, + isOutgoingFiltered: function () { + return this.filterOutgoing; + }, + toggleIncomingInjection: function () { + this.injectIncoming = !this.injectIncoming; + return this.injectIncoming; + }, + toggleOutgoingInjection: function () { + this.injectOutgoing = !this.injectOutgoing; + return this.injectOutgoing; + }, + isIncomingInjected: function () { + return this.injectIncoming; + }, + isOutgoingInjected: function () { + return this.injectOutgoing; + }, + toggleIncomingLogging: function () { + this.logIncoming = !this.logIncoming; + return this.logIncoming; + }, + toggleOutgoingLogging: function () { + this.logOutgoing = !this.logOutgoing; + return this.logOutgoing; + }, + isIncomingLogged: function () { + return this.logIncoming; + }, + isOutgoingLogged: function () { + return this.logOutgoing; + }, + addIncomingInjectionField: function () { + this.incomingInjectionFields.push({}); + this.updateReact(); + }, + addOutgoingInjectionField: function () { + this.outgoingInjectionFields.push({}); + this.updateReact(); + }, + updateIncomingInjectionValue: function (row, value) { + this.incomingInjectionFields[row].fieldValue = value; + this.updateReact(); + }, + updateIncomingInjectionKey: function (row, value) { + this.incomingInjectionFields[row].keyValue = value; + this.updateReact(); + }, + updateOutgoingInjectionValue: function (row, value) { + this.outgoingInjectionFields[row].fieldValue = value; + this.updateReact(); + }, + updateOutgoingInjectionKey: function (row, value) { + this.outgoingInjectionFields[row].keyValue = value; + this.updateReact(); + }, + matches: function (joinPoint) { + function isIncoming() { + if (joinPoint && joinPoint.target) { + return joinPoint.target.getSubject; + } + } + function isString() { + return typeof joinPoint === "string"; + } + if (isString()) { + if (joinPoint.match(this.matchString)) { + return true; + } + } else if (isIncoming()) { + if ( + joinPoint && + joinPoint.target && + joinPoint.target.getSubject && + joinPoint.target.getSubject().match(this.matchString) + ) { + return true; + } + } else { + if ( + joinPoint && + joinPoint.args && + joinPoint.args[0].match(this.matchString) + ) { + return true; + } + } - } - function isString() { - return typeof joinPoint === "string"; - } - if (isString()) { - if (joinPoint.match(this.matchString)) { - return true; - } - } else if (isIncoming()) { - if (joinPoint && joinPoint.target && joinPoint.target.getSubject && joinPoint.target.getSubject().match(this.matchString)) { - return true; - } - } else { - if (joinPoint && joinPoint.args && joinPoint.args[0].match(this.matchString)) { - return true; - } - } + return false; + }, + addJoinPoint: function (joinPointCollection) { + const joinPoint = joinPointCollection[0]; + this.joinPointsCached.push(joinPoint); + const key = this.calculateJoinPointKey(joinPoint); + this.joinPointToCollectionCache[key] = joinPointCollection; + joinPoint.matcher = this; + this.updateReact(); + }, + forwardJoinPoint: function (joinPoint) { + const key = this.calculateJoinPointKey(joinPoint); + const joinPointCollection = this.joinPointToCollectionCache[key]; + if (joinPointCollection) { + joinPointCollection.forEach(function (point) { + point.proceed(); + }); + console.log( + "Forwarding :", + joinPoint.target.getSubject(), + JSON.parse(JSON.stringify(joinPoint.target.getFields())) + ); + } else { + console.log( + "Warning: no messages when forwarding for ", + JSON.parse(JSON.stringify(joinPoint.target.getFields())) + ); + } + this.removeJoinPoint(joinPoint); + }, + removeJoinPoint: function (joinPoint) { + var index = this.joinPointsCached.indexOf(joinPoint); + this.joinPointsCached.splice(index, 1); + const key = this.calculateJoinPointKey(joinPoint); + this.joinPointToCollectionCache[key] = null; + this.updateReact(); + }, + calculateJoinPointKey: function (joinPoint) { + return ( + joinPoint.target._timeReceived.toString() + + joinPoint.target._rttpSequenceNumber + ); + }, + }; - return false; - }, - addJoinPoint: function(joinPointCollection) { - const joinPoint = joinPointCollection[0]; - this.joinPointsCached.push(joinPoint); - const key = this.calculateJoinPointKey(joinPoint); - this.joinPointToCollectionCache[key] = joinPointCollection - joinPoint.matcher = this; - this.updateReact(); - }, - forwardJoinPoint: function(joinPoint) { - const key = this.calculateJoinPointKey(joinPoint) - const joinPointCollection = this.joinPointToCollectionCache[key] - if(joinPointCollection){ - joinPointCollection.forEach(function(point){ - point.proceed(); - }); - console.log("Forwarding :", joinPoint.target.getSubject(), JSON.parse(JSON.stringify(joinPoint.target.getFields()))); - } else { - console.log("Warning: no messages when forwarding for ", JSON.parse(JSON.stringify(joinPoint.target.getFields()))); - } - this.removeJoinPoint(joinPoint); - }, - removeJoinPoint: function(joinPoint) { - var index = this.joinPointsCached.indexOf(joinPoint); - this.joinPointsCached.splice(index,1); - const key = this.calculateJoinPointKey(joinPoint) - this.joinPointToCollectionCache[key] = null; - this.updateReact(); - }, - calculateJoinPointKey: function(joinPoint) { - return joinPoint.target._timeReceived.toString() + joinPoint.target._rttpSequenceNumber - } - } - - return DamJSMatcher; -}); \ No newline at end of file + return DamJSMatcher; +}); diff --git a/README.md b/README.md index 469c730..c55d48a 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,4 @@ A scriptmarklet tool for analysing StreamLink communication -- -Visit http://JTMCaplin.github.io/DamJS to add it to your bookmarks +Visit http://caplin.github.io/DamJS to add it to your bookmarks diff --git a/ServicesListElement.js b/ServicesListElement.js index 033a84d..b572ce9 100644 --- a/ServicesListElement.js +++ b/ServicesListElement.js @@ -1,38 +1,58 @@ -define(['lib/react'], function(React) { - return React.createClass({ - setServiceUp: function(service) { - return function() { - var e = new caplin.streamlink.impl.event.ServiceStatusEventImpl('OK', service) - var b = CTSL.getSLJS()._streamLinkCore._protocolHandler.connection.connectionListeners[3]; +define(["lib/react"], function (React) { + return React.createClass({ + setServiceUp: function (service) { + return function () { + var e = new caplin.streamlink.impl.event.ServiceStatusEventImpl( + "OK", + service + ); + var b = + caplin.streamlink._streamLinkCore._protocolHandler.connection + .connectionListeners[2]; debugger; b.onServiceStatusChange(e); - } + }; }, - setServiceDown: function(service) { - return function() { - var e = new caplin.streamlink.impl.event.ServiceStatusEventImpl('DOWN', service) - var b = CTSL.getSLJS()._streamLinkCore._protocolHandler.connection.connectionListeners[3]; + setServiceDown: function (service) { + return function () { + var e = new caplin.streamlink.impl.event.ServiceStatusEventImpl( + "DOWN", + service + ); + var b = + caplin.streamlink._streamLinkCore._protocolHandler.connection + .connectionListeners[2]; debugger; b.onServiceStatusChange(e); - } + }; }, - render: function() { - var serviceElements = [] - var services = CTSL.getSLJS()._streamLinkCore._protocolHandler.connection.connectionListeners[3]._services; + render: function () { + var serviceElements = []; + var services = + caplin.streamlink._streamLinkCore._protocolHandler.connection + .connectionListeners[2]._state.mServices; if (services) { for (var service in services) { - serviceElements.push(React.DOM.div({}, - React.DOM.span({}, service), - React.DOM.button({onClick: this.setServiceUp(service)}, "Up"), - React.DOM.button({onClick: this.setServiceDown(service)}, "Down") - )) + serviceElements.push( + React.DOM.div( + {}, + React.DOM.span({}, service), + React.DOM.button({ onClick: this.setServiceUp(service) }, "Up"), + React.DOM.button( + { onClick: this.setServiceDown(service) }, + "Down" + ) + ) + ); } } - return React.DOM.div({}, - React.DOM.button({onClick: this.props.back}, "Back"), - serviceElements); - } - }); + return React.DOM.div( + {}, + React.DOM.button({ onClick: this.props.back }, "Back"), + serviceElements + ); + }, + }); }); diff --git a/SubscriberElement.js b/SubscriberElement.js index bf864bc..66a1c93 100644 --- a/SubscriberElement.js +++ b/SubscriberElement.js @@ -1,40 +1,61 @@ -define(['lib/react'], function(React) { - return React.createClass({ - subscribe: function() { - if (typeof damJSStreamLink !== "undefined") { - damJSStreamLink.subscribe(this.state.subject, - { - onSubscriptionStatus : function(subscription, event) { - console.log(subscription.getSubject() + " is now " + event.getStatus()); - }, +define(["lib/react"], function (React) { + return React.createClass({ + subscribe: function () { + var streamlink; - onSubscriptionError : function(subscription, event) { - console.log("Error: Subject " + subscription.getSubject() + " is " + event.getError()); - }, - - onRecordUpdate : function(subscription, event) { - console.log(event.getSubject(), JSON.parse(JSON.stringify(event.getFields()))) - } - } - ) + // >= StreamLinkTS 7.1.12 + if (window.caplin.streamlink.getVersion) { + streamlink = caplin.streamlink; + // <= StreamLinkJS 7.0.4 } else { - alert('Make a real subscription first!'); + if (typeof damJSStreamLink === "undefined") { + alert("Make a real subscription first!"); + return; + } + streamlink = window.damJSStreamLink; } + streamlink.subscribe(this.state.subject, { + onSubscriptionStatus: function (subscription, event) { + console.log( + subscription.getSubject() + " is now " + event.getStatus() + ); + }, + + onSubscriptionError: function (subscription, event) { + console.log( + "Error: Subject " + + subscription.getSubject() + + " is " + + event.getError() + ); + }, + + onRecordUpdate: function (subscription, event) { + console.log( + event.getSubject(), + JSON.parse(JSON.stringify(event.getFields())) + ); + }, + }); }, - getInitialState:function() { - return {subject: "/FX/EURUSD/SPOT/EUR/100"} + getInitialState: function () { + return { subject: "/FX/EURUSD/SPOT/EUR/100" }; }, - setSubject:function(e) { + setSubject: function (e) { this.setState({ - subject: e.target.value - }) + subject: e.target.value, + }); }, - render: function() { - return React.DOM.div({}, - React.DOM.button({onClick: this.props.back}, "Back"), - React.DOM.input({value: this.state.subject, onChange: this.setSubject}), - React.DOM.button({onClick: this.subscribe},"Subscribe") + render: function () { + return React.DOM.div( + {}, + React.DOM.button({ onClick: this.props.back }, "Back"), + React.DOM.input({ + value: this.state.subject, + onChange: this.setSubject, + }), + React.DOM.button({ onClick: this.subscribe }, "Subscribe") ); - } - }); + }, + }); }); diff --git a/index.html b/index.html index fe0ad07..9243aa6 100644 --- a/index.html +++ b/index.html @@ -1,5 +1,5 @@
- Just drag this DamJS url onto your bookmarks bar! + Just drag this DamJS url onto your bookmarks bar! diff --git a/index.js b/index.js index 9e242bd..48ed726 100644 --- a/index.js +++ b/index.js @@ -1,196 +1,195 @@ -(function() { - setTimeout(function () { - w = window; - d = w.document; - - var p = d.createElement('script'); - p.src= 'https://polyfill.io/v3/polyfill.js'; - - p.onload = loader; - - p.onreadystatechange = function() { - if (this.readyState === 'complete') { - loader(); - } - }; - d.head.appendChild(p); - - - }); +(function () { + setTimeout(function () { + w = window; + d = w.document; + + var p = d.createElement("script"); + p.src = "https://polyfill.io/v3/polyfill.js"; + + p.onload = loader; + + p.onreadystatechange = function () { + if (this.readyState === "complete") { + loader(); + } + }; + d.head.appendChild(p); + }); })(); -var loader = function() { - var s = d.createElement('script'); - s.type = 'text/javascript'; - s.src = damJSDomain + '/lib/require.js'; - - if (typeof(exports) !== "undefined") { - exportsBak = exports; - } - exports = undefined; - - if (typeof(define) !== "undefined") { - defineBak = define; - } - define = undefined; - - if (typeof(require) !== "undefined") { - requireBak = require; - } - require = undefined; - - - s.onload = callBack; - s.onreadystatechange = function() { - if (this.readyState === 'complete') { - callBack(); - } - }; - d.head.appendChild(s); +var loader = function () { + var s = d.createElement("script"); + s.type = "text/javascript"; + s.src = damJSDomain + "/lib/require.js"; + + if (typeof exports !== "undefined") { + exportsBak = exports; + } + exports = undefined; + + if (typeof define !== "undefined") { + defineBak = define; + } + define = undefined; + + if (typeof require !== "undefined") { + requireBak = require; + } + require = undefined; + + s.onload = callBack; + s.onreadystatechange = function () { + if (this.readyState === "complete") { + callBack(); + } + }; + d.head.appendChild(s); }; - -var callBack = function() { - require.config({ - baseUrl: damJSDomain - }); - require(['lib/react', 'DamJSElement', 'DamJS'], function(React, DamJSElement, DamJS) { - listStyle = { - border: "1px solid lightgrey", - borderRadius: "5px", - padding: "5px", - margin: "5px" - }; - - columnStyle = { - width: "300px", - height: "160px", - padding: "20px", - float: "left", - backgroundColor: "white", - overflowY: "auto" - }; - - var damJS = new DamJS(module.exports); - damJS.addNewMatcher("/"); - damJS.addNewMatcher("/FX/EURUSD"); - damJS.addNewMatcher("/FX/GBPUSD"); - damJS.addNewMatcher("/FX/USDJPY"); - damJS.addNewMatcher("/PRIVATE"); - damJS.addNewMatcher("/PRIVATE/FX"); - damJS.addNewMatcher("/PRIVATE/TRADE/FX"); - damJS.addNewMatcher("/PRIVATE/TRADE/MM"); - - var newElement = document.createElement('div'); - document.body.appendChild(newElement); - React.renderComponent(DamJSElement({damJS: damJS}), newElement); - - try { - exports = exportsBak; - define = defineBak; - require = requireBak; - } catch (e) { - //do nothing - } - - }); +var callBack = function () { + if (!(window.caplin && window.caplin.streamlink)) { + alert("Streamlink not found on window.caplin.streamlink!"); + return; + } + + require.config({ + baseUrl: damJSDomain, + }); + require(["lib/react", "DamJSElement", "DamJS"], function ( + React, + DamJSElement, + DamJS + ) { + listStyle = { + border: "1px solid lightgrey", + borderRadius: "5px", + padding: "5px", + margin: "5px", + }; + + columnStyle = { + width: "300px", + height: "160px", + padding: "20px", + float: "left", + backgroundColor: "white", + overflowY: "auto", + }; + + var damJS = new DamJS(module.exports); + damJS.addNewMatcher("/"); + damJS.addNewMatcher("/FX/EURUSD"); + damJS.addNewMatcher("/FX/GBPUSD"); + damJS.addNewMatcher("/FX/USDJPY"); + damJS.addNewMatcher("/PRIVATE"); + damJS.addNewMatcher("/PRIVATE/FX"); + damJS.addNewMatcher("/PRIVATE/TRADE/FX"); + damJS.addNewMatcher("/PRIVATE/TRADE/MM"); + + var newElement = document.createElement("div"); + document.body.appendChild(newElement); + React.renderComponent(DamJSElement({ damJS: damJS }), newElement); + + try { + exports = exportsBak; + define = defineBak; + require = requireBak; + } catch (e) { + //do nothing + } + }); }; -module = {exports: null}; - +module = { exports: null }; //DRAG AND DROP -- http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx -var _startX = 0; // mouse starting positions +var _startX = 0; // mouse starting positions var _startY = 0; -var _offsetX = 0; // current element offset +var _offsetX = 0; // current element offset var _offsetY = 0; -var _dragElement; // needs to be passed from OnMouseDown to OnMouseMove -var _oldZIndex = 0; // we temporarily increase the z-index during drag - -function OnMouseDown(e) -{ - // IE is retarded and doesn't pass the event object - if (e == null) - e = window.event; - - // IE uses srcElement, others use target - var target = e.target != null ? e.target : e.srcElement; - - // for IE, left click == 1 - // for Firefox, left click == 0 - if ((e.button == 1 && window.event != null || - e.button == 0) && - target.className == 'drag') - { - // grab the mouse position - _startX = e.clientX; - _startY = e.clientY; - - // grab the clicked element's position - _offsetX = ExtractNumber(target.style.left); - _offsetY = ExtractNumber(target.style.top); - - // bring the clicked element to the front while it is being dragged - _oldZIndex = target.style.zIndex; - target.style.zIndex = 10000; - - // we need to access the element in OnMouseMove - _dragElement = target; - - // tell our code to start moving the element with the mouse - document.onmousemove = OnMouseMove; - - // cancel out any text selections - document.body.focus(); - - // prevent text selection in IE - document.onselectstart = function () { return false; }; - // prevent IE from trying to drag an image - target.ondragstart = function() { return false; }; - - // prevent text selection (except IE) - return false; - } +var _dragElement; // needs to be passed from OnMouseDown to OnMouseMove +var _oldZIndex = 0; // we temporarily increase the z-index during drag + +function OnMouseDown(e) { + // IE is retarded and doesn't pass the event object + if (e == null) e = window.event; + + // IE uses srcElement, others use target + var target = e.target != null ? e.target : e.srcElement; + + // for IE, left click == 1 + // for Firefox, left click == 0 + if ( + ((e.button == 1 && window.event != null) || e.button == 0) && + target.className == "drag" + ) { + // grab the mouse position + _startX = e.clientX; + _startY = e.clientY; + + // grab the clicked element's position + _offsetX = ExtractNumber(target.style.left); + _offsetY = ExtractNumber(target.style.top); + + // bring the clicked element to the front while it is being dragged + _oldZIndex = target.style.zIndex; + target.style.zIndex = 10000; + + // we need to access the element in OnMouseMove + _dragElement = target; + + // tell our code to start moving the element with the mouse + document.onmousemove = OnMouseMove; + + // cancel out any text selections + document.body.focus(); + + // prevent text selection in IE + document.onselectstart = function () { + return false; + }; + // prevent IE from trying to drag an image + target.ondragstart = function () { + return false; + }; + + // prevent text selection (except IE) + return false; + } } -function OnMouseMove(e) -{ - if (e == null) - var e = window.event; +function OnMouseMove(e) { + if (e == null) var e = window.event; - // this is the actual "drag code" - _dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px'; - _dragElement.style.top = (_offsetY + e.clientY - _startY) + 'px'; + // this is the actual "drag code" + _dragElement.style.left = _offsetX + e.clientX - _startX + "px"; + _dragElement.style.top = _offsetY + e.clientY - _startY + "px"; } -function OnMouseUp(e) -{ - if (_dragElement != null) - { - _dragElement.style.zIndex = _oldZIndex; +function OnMouseUp(e) { + if (_dragElement != null) { + _dragElement.style.zIndex = _oldZIndex; - // we're done with these events until the next OnMouseDown - document.onmousemove = null; - document.onselectstart = null; - _dragElement.ondragstart = null; + // we're done with these events until the next OnMouseDown + document.onmousemove = null; + document.onselectstart = null; + _dragElement.ondragstart = null; - // this is how we know we're not dragging - _dragElement = null; - } + // this is how we know we're not dragging + _dragElement = null; + } } -function ExtractNumber(value) -{ - var n = parseInt(value); +function ExtractNumber(value) { + var n = parseInt(value); - return n == null || isNaN(n) ? 0 : n; + return n == null || isNaN(n) ? 0 : n; } -function InitDragDrop() -{ - document.onmousedown = OnMouseDown; - document.onmouseup = OnMouseUp; +function InitDragDrop() { + document.onmousedown = OnMouseDown; + document.onmouseup = OnMouseUp; } InitDragDrop();