From 72fb2057f8d19508fe168d48828f7ff2ac25af49 Mon Sep 17 00:00:00 2001 From: eudaimos Date: Fri, 26 Jun 2020 08:07:17 -0700 Subject: [PATCH] feat(Channel): id and cloneId params can now take a function used to generate channel ID affects: @tao.js/utils new param type of function provides more control to the client for what channel IDs will be used the function will receive the same value that newChannelId() will produce to aid in uniqueness some more cleanup: * newChannelId() ensures maximum integer isn't overrun with mod against it after increment * removed old try at implementation that were commented out --- packages/tao-utils/src/Channel.js | 39 +++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/tao-utils/src/Channel.js b/packages/tao-utils/src/Channel.js index ee3f37d..f8313da 100644 --- a/packages/tao-utils/src/Channel.js +++ b/packages/tao-utils/src/Channel.js @@ -2,9 +2,12 @@ import { Network } from '@tao.js/core'; import seive from './seive'; +// for backwards compatibility +const MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; + let channelId = 0; function newChannelId() { - return ++channelId; + return (channelId = ++channelId % MAX_SAFE_INTEGER); } function channelControl(channelId) { @@ -12,17 +15,36 @@ function channelControl(channelId) { } export default class Channel { + /** + *Creates an instance of Channel. + * @param {Kernel} kernel - an instance of a Kernel or other TAO Network on which to build a Channel by sharing the same underlying Network + * @param {(string|function)} [id] - pass either a desired Channel ID value as a `string` or a `function` that will be used to generate a Channel ID + * the `function` will be called with a new Channel ID integer value to help ensure uniqueness + * @memberof Channel + */ constructor(kernel, id) { - //, { t, term, a, action, o, orient }) { - this._channelId = id || newChannelId(); + this._channelId = + typeof id === 'function' ? id(newChannelId()) : id || newChannelId(); this._channel = new Network(); this._channel.use(this.handleAppCon); - // this._kernel = new Kernel(kernel.canSetWildcard); this._network = kernel._network; + this._cloneWithId = typeof id === 'function' ? id : undefined; } + /** + * Clones a Channel + * + * @param {(string|function)} [cloneId] - used as the cloned `Channel`'s ID, + * same as the `id` param to the `Channel` constructor, either an + * explicit value as a `string` or a `function` used to generate the Channel ID + * @returns + * @memberof Channel + */ clone(cloneId) { - const clone = new Channel({ _network: this._network }, cloneId); + const clone = new Channel( + { _network: this._network }, + cloneId || this._cloneWithId + ); clone._channel = this._channel; return clone; } @@ -57,7 +79,6 @@ export default class Channel { console.log( `channel{${this._channelId}}::forwardAppCtx::control check passed` ); - // this._kernel.setAppCtx(ac); this._channel.setAppCtxControl(ac, control, a => this.setAppCtx(a)); } console.log( @@ -75,7 +96,6 @@ export default class Channel { } addInterceptHandler({ t, term, a, action, o, orient }, handler) { - // this._kernel.addInterceptHandler( this._channel.addInterceptHandler( { t, term, a, action, o, orient }, handler @@ -84,19 +104,16 @@ export default class Channel { } addAsyncHandler({ t, term, a, action, o, orient }, handler) { - // this._kernel.addAsyncHandler({ t, term, a, action, o, orient }, handler); this._channel.addAsyncHandler({ t, term, a, action, o, orient }, handler); return this; } addInlineHandler({ t, term, a, action, o, orient }, handler) { - // this._kernel.addInlineHandler({ t, term, a, action, o, orient }, handler); this._channel.addInlineHandler({ t, term, a, action, o, orient }, handler); return this; } removeInterceptHandler({ t, term, a, action, o, orient }, handler) { - // this._kernel.removeInterceptHandler( this._channel.removeInterceptHandler( { t, term, a, action, o, orient }, handler @@ -105,7 +122,6 @@ export default class Channel { } removeAsyncHandler({ t, term, a, action, o, orient }, handler) { - // this._kernel.removeAsyncHandler({ t, term, a, action, o, orient }, handler); this._channel.removeAsyncHandler( { t, term, a, action, o, orient }, handler @@ -114,7 +130,6 @@ export default class Channel { } removeInlineHandler({ t, term, a, action, o, orient }, handler) { - // this._kernel.removeInlineHandler( this._channel.removeInlineHandler( { t, term, a, action, o, orient }, handler