-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(seive): new seive used by Channel allows controlled bridging
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
Showing
5 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters