@azothjs/channels
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -0,22 +1,28 @@ | ||
import { ConflictingOptionsError, OptionMissingFunctionArgumentError } from './throw.js'; | ||
export function subject(transform, options) { | ||
if(!options && typeof transform === 'object') { | ||
options = transform; | ||
transform = null; | ||
} | ||
let initialValue, startWith; | ||
function resolveOptions(options, transform) { | ||
let initialValue, startWith, map = false; | ||
if(options) { | ||
initialValue = options.initialValue; | ||
startWith = options.startWith; | ||
map = options.map ?? false; | ||
if(initialValue !== undefined) { | ||
if(startWith !== undefined) { | ||
throw new Error('Cannot specify both initialValue and startWith option'); | ||
} | ||
if(!transform) { | ||
throw new Error('Cannot specify initialValue without a transform function'); | ||
} | ||
if(startWith !== undefined) new ConflictingOptionsError(); | ||
if(!transform) throw new OptionMissingFunctionArgumentError('initialValue'); | ||
} | ||
if(map && !transform) { | ||
throw new OptionMissingFunctionArgumentError(); | ||
} | ||
} | ||
return { initialValue, startWith, map }; | ||
} | ||
export function subject(transform, options) { | ||
if(!options && typeof transform === 'object') { | ||
options = transform; | ||
transform = null; | ||
} | ||
const { initialValue, startWith, map } = resolveOptions(options, transform); | ||
const relay = { resolve: null }; | ||
@@ -27,3 +33,7 @@ | ||
function dispatch(payload) { | ||
if(transform) payload = transform(payload); | ||
if(transform) { | ||
if(map) payload = payload.map(transform); | ||
else payload = transform(payload); | ||
} | ||
if(relay.resolve) relay.resolve(payload); | ||
@@ -30,0 +40,0 @@ else { |
{ | ||
"name": "@azothjs/channels", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Asynchronous layout channels", | ||
@@ -24,8 +24,8 @@ "author": "Marty Nelson", | ||
"files": [ | ||
"use.js", | ||
"repeat.js", | ||
"consume.js", | ||
"throw.js", | ||
"generators.js", | ||
"index.js", | ||
"repeat.js", | ||
"throw.js", | ||
"use.js", | ||
"with-resolvers-polyfill.js", | ||
@@ -32,0 +32,0 @@ "README" |
18
throw.js
@@ -8,2 +8,18 @@ | ||
} | ||
} | ||
} | ||
export class OptionMissingFunctionArgumentError extends TypeError { | ||
constructor(option = 'map: true') { | ||
super(`\ | ||
More arguments needed: option "${option}" requires a mapping function.`); | ||
} | ||
} | ||
export class ConflictingOptionsError extends TypeError { | ||
constructor() { | ||
super(`\ | ||
Cannot specify both initialValue and startWith options.`); | ||
} | ||
} | ||
34
use.js
import { Multicast } from './generators.js'; | ||
import { AsyncSourceTypeError } from './throw.js'; | ||
import { AsyncSourceTypeError, OptionMissingFunctionArgumentError } from './throw.js'; | ||
@@ -36,17 +36,37 @@ export function use(asyncSource, ...args) { | ||
const startWith = options?.startWith; | ||
const map = options?.map ?? false; | ||
if(startWith) { | ||
return fromPromiseStartWith(promise, channel, startWith); | ||
return fromPromiseStartWith(startWith, promise, channel, map); | ||
} | ||
return channel ? promise.then(channel) : promise; | ||
return promiseResolution(promise, channel, map); | ||
} | ||
async function* fromPromiseStartWith(promise, channel, startWith) { | ||
async function* fromPromiseStartWith(startWith, promise, channel, map) { | ||
yield startWith; | ||
yield channel ? promise.then(channel) : promise; | ||
yield promiseResolution(promise, channel, map); | ||
} | ||
function promiseResolution(promise, channel, map) { | ||
if(map) { | ||
if(!channel) throw new OptionMissingFunctionArgumentError(); | ||
// TODO: include or suppress index? which param??? | ||
// collapse "slottable" back into props??? | ||
return promise.then(array => array.map(channel)); | ||
} | ||
return channel ? promise.then(channel) : promise; | ||
} | ||
async function* fromAsyncIterator(iterator, channel, options) { | ||
const startWith = options?.startWith; | ||
const map = options?.map ?? false; | ||
if(map && !channel) throw new OptionMissingFunctionArgumentError(); | ||
if(startWith) yield startWith; | ||
for await(const value of iterator) { | ||
if(map) { | ||
yield value.map(channel); | ||
continue; | ||
} | ||
yield channel ? channel(value) : value; | ||
@@ -65,3 +85,3 @@ } | ||
function branchAsyncIterator(iterator, channels, options) { | ||
function branchAsyncIterator(iterator, channels) { | ||
const multicast = new Multicast(iterator); | ||
@@ -72,4 +92,4 @@ return channels.map(channel => { | ||
} | ||
return multicast.subscriber(channel, options); | ||
return multicast.subscriber(channel); | ||
}); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10568
259