angular-signalr-hub
angular-signalr-hub copied to clipboard
Added client arguments interceptor in options
Added 'argsInterceptor' property in options. This allows specify a function that will be called before calling to every listener, passing 'arguments' variable as a parameter. This allows to modify arguments before reach the listener function. This is useful to fix objects returned by Json.NET when options in SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize. This allows Json.NET to serialize complex objects with loop references.
This is an example of argsInterceptor function (in typescript):
argsInterceptor: (args: IArguments): void => {
for (var i = 0; i < args.length; i++) {
if (typeof args[i] === 'object') {
handleReferenceLoop(args[i]);
}
}
},
This is an implementacion of handleReferenceLoop function:
function handleReferenceLoop(g: any): boolean {
if (typeof g['$id'] === 'undefined') return false;
var ids = {};
function relink(s: any): void {
// we care naught about primitives
if (s === null || typeof s !== 'object') { return s; }
var id = s['$id'];
delete s['$id'];
// either return previously known object, or
// remember this object linking for later
if (ids[id]) return ids[id];
ids[id] = s;
// then, recursively for each key/index, relink the sub-graph
if (s.hasOwnProperty('length')) {
// array or array-like; a different guard may be more appropriate
for (var i = 0; i < s.length; i++) s[i] = relink(s[i]);
} else {
// other objects
for (var p in s) {
if (s.hasOwnProperty(p)) s[p] = relink(s[p]);
}
}
}
relink(g);
return true;
}
(see: http://stackoverflow.com/questions/21686499/how-to-restore-circular-references-e-g-id-from-json-net-serialized-json)