Skip to content

Commit

Permalink
feat(Channel): id and cloneId params can now take a function used to …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
eudaimos committed Jun 26, 2020
1 parent cf6497d commit 72fb205
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions packages/tao-utils/src/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,49 @@
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) {
return { 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;
}
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 72fb205

Please sign in to comment.