Skip to content

Commit

Permalink
feat(seive): new seive used by Channel allows controlled bridging
Browse files Browse the repository at this point in the history
affects: @tao.js/utils

needed a way to bridge with control info from one network to another so
Channels can bridge ACs back
from Kernels without double/circular AC loops
  • Loading branch information
eudaimos committed Jul 27, 2019
1 parent 2910c63 commit c916870
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
11 changes: 11 additions & 0 deletions packages/tao-utils/src/Channel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// import { Kernel } from '@tao.js/core';
import { Network } from '@tao.js/core';
import seive from './seive';

let channelId = 0;
function newChannelId() {
Expand Down Expand Up @@ -108,4 +109,14 @@ export default class Channel {
handler
);
}

bridgeFrom(TAO, ...trigrams) {
return seive(
this._channelId,
TAO,
this,
(ac, control) => control.channelId !== this._channelId,
...trigrams
);
}
}
6 changes: 4 additions & 2 deletions packages/tao-utils/src/bridge.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Kernel, AppCtx, INTERCEPT, ASYNC, INLINE } from '@tao.js/core';

const NOOP = () => {};

function forwardHandler(destination) {
return (tao, data) => {
console.log('bridging::tao', tao);
Expand All @@ -25,10 +27,10 @@ function filteredForwardHandler(destination, filter) {

function bridge(type, source, destination, filters) {
if (type !== INTERCEPT && type !== ASYNC && type !== INLINE) {
return;
return NOOP;
}
if (!(source instanceof Kernel) || !(destination instanceof Kernel)) {
return;
return NOOP;
}
const filterFunction =
typeof filters[0] === 'function' ? filters.shift() : undefined;
Expand Down
3 changes: 2 additions & 1 deletion packages/tao-utils/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import trigramFilter from './trigram-filter';
import Channel from './Channel';
import Source from './Source';
import seive from './seive';
export * from './bridge';

export { trigramFilter, Channel, Source };
export { trigramFilter, Channel, Source, seive };
45 changes: 45 additions & 0 deletions packages/tao-utils/src/seive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Network } from '@tao.js/core';
import trigramFilter from './trigram-filter';

const NOOP = () => {};

function addControl(name, control) {
return { ...control, seive: name };
}

// filter function signature: (ac:AppCtx, control:Object)
// filters is: [filterFunc, [exact,]] ...trigrams

export default function seive(name, source, destination, ...filters) {
if (
!source ||
!(source._network instanceof Network) ||
!destination ||
!(destination._network instanceof Network)
) {
return NOOP;
}
let filterFunction =
typeof filters[0] === 'function' ? filters.shift() : undefined;
let handleFilter = trigramFilter(...filters);

let middleware = (handler, ac, forwardAppCtx, control) => {
if (filterFunction && !filterFunction(ac, control)) {
return;
}
if (handleFilter(ac)) {
destination._channel.setAppCtxControl(
ac,
addControl(name, control),
forwardAppCtx
);
}
};
source._network.use(middleware);
return () => {
source._network.stop(middleware);
middleware = null;
filterFunction = null;
handleFilter = null;
};
}
3 changes: 3 additions & 0 deletions packages/tao-utils/src/trigram-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export default function trigramFilter(...trigrams) {
if (typeof trigrams[0] === 'boolean') {
exact = trigrams.shift();
}
if (Array.isArray(trigrams[0])) {
trigrams = trigrams[0];
}
return ac => {
if (!(ac instanceof AppCtx)) {
return false;
Expand Down

0 comments on commit c916870

Please sign in to comment.