callbag-toolkit
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -31,2 +31,20 @@ "use strict"; | ||
exports.createConsumer = createConsumer; | ||
const assertActive = (state, method) => { | ||
if (state === 'active') | ||
return true; | ||
// eslint-disable-next-line no-console | ||
console.error(`This callbag source ${state === 'disposed' | ||
? 'was previously disposed' | ||
: state === 'ended' | ||
? 'has previously ended' | ||
: 'has not been started'}, but '${method}' was invoked.`); | ||
return false; | ||
}; | ||
const assertInactive = (state, method) => { | ||
if (state !== 'active') | ||
return true; | ||
// eslint-disable-next-line no-console | ||
console.error(`This callbag source is already active, but '${method}' was invoked.`); | ||
return false; | ||
}; | ||
const createSource = (onConsume) => (...args) => { | ||
@@ -36,7 +54,5 @@ if (args[0] !== types_1.START) | ||
let state = 'inactive'; | ||
const deactivate = () => state === 'active' && (state = 'inactive'); | ||
const activate = () => state !== 'active' && (state = 'active'); | ||
const markDisposed = () => state !== 'disposed' && (state = 'disposed'); | ||
let init = false; | ||
/** talkback - sends messages back upstream */ | ||
/** talkback: handle messages from the sink back to this source */ | ||
// eslint-disable-next-line @typescript-eslint/no-shadow | ||
@@ -50,4 +66,4 @@ const upstream = (...args) => { | ||
askToPull(); | ||
else if (dispose && args[0] === types_1.END) | ||
markDisposed() && dispose(); | ||
else if (args[0] === types_1.END) | ||
markDisposed() && (dispose === null || dispose === void 0 ? void 0 : dispose()); | ||
} | ||
@@ -61,6 +77,25 @@ else { | ||
const consumer = onConsume({ | ||
start: () => void (activate() && downstream(types_1.START, upstream)), | ||
next: (data) => state === void ('active' && downstream(types_1.DATA, data)), | ||
error: (error) => void (deactivate() && downstream(types_1.END, error)), | ||
complete: () => void (deactivate() && downstream(types_1.END)), | ||
start: () => { | ||
if (!assertInactive(state, 'start')) | ||
return; | ||
downstream(types_1.START, upstream); | ||
state = 'active'; | ||
}, | ||
next: (data) => { | ||
if (!assertActive(state, 'next')) | ||
return; | ||
downstream(types_1.DATA, data); | ||
}, | ||
error: (error) => { | ||
// assert, but passthrough errors so that clean-up may take place | ||
assertActive(state, 'error'); | ||
downstream(types_1.END, error); | ||
state = 'ended'; | ||
}, | ||
complete: () => { | ||
// assert, but passthrough errors so that clean-up may take place | ||
assertActive(state, 'complete'); | ||
downstream(types_1.END); | ||
state = 'ended'; | ||
}, | ||
}); | ||
@@ -67,0 +102,0 @@ const dispose = typeof consumer === 'function' ? consumer : consumer === null || consumer === void 0 ? void 0 : consumer.stop; |
@@ -27,2 +27,20 @@ /* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-expressions,no-return-assign */ | ||
}; | ||
const assertActive = (state, method) => { | ||
if (state === 'active') | ||
return true; | ||
// eslint-disable-next-line no-console | ||
console.error(`This callbag source ${state === 'disposed' | ||
? 'was previously disposed' | ||
: state === 'ended' | ||
? 'has previously ended' | ||
: 'has not been started'}, but '${method}' was invoked.`); | ||
return false; | ||
}; | ||
const assertInactive = (state, method) => { | ||
if (state !== 'active') | ||
return true; | ||
// eslint-disable-next-line no-console | ||
console.error(`This callbag source is already active, but '${method}' was invoked.`); | ||
return false; | ||
}; | ||
export const createSource = (onConsume) => (...args) => { | ||
@@ -32,7 +50,5 @@ if (args[0] !== START) | ||
let state = 'inactive'; | ||
const deactivate = () => state === 'active' && (state = 'inactive'); | ||
const activate = () => state !== 'active' && (state = 'active'); | ||
const markDisposed = () => state !== 'disposed' && (state = 'disposed'); | ||
let init = false; | ||
/** talkback - sends messages back upstream */ | ||
/** talkback: handle messages from the sink back to this source */ | ||
// eslint-disable-next-line @typescript-eslint/no-shadow | ||
@@ -46,4 +62,4 @@ const upstream = (...args) => { | ||
askToPull(); | ||
else if (dispose && args[0] === END) | ||
markDisposed() && dispose(); | ||
else if (args[0] === END) | ||
markDisposed() && (dispose === null || dispose === void 0 ? void 0 : dispose()); | ||
} | ||
@@ -57,6 +73,25 @@ else { | ||
const consumer = onConsume({ | ||
start: () => void (activate() && downstream(START, upstream)), | ||
next: (data) => state === void ('active' && downstream(DATA, data)), | ||
error: (error) => void (deactivate() && downstream(END, error)), | ||
complete: () => void (deactivate() && downstream(END)), | ||
start: () => { | ||
if (!assertInactive(state, 'start')) | ||
return; | ||
downstream(START, upstream); | ||
state = 'active'; | ||
}, | ||
next: (data) => { | ||
if (!assertActive(state, 'next')) | ||
return; | ||
downstream(DATA, data); | ||
}, | ||
error: (error) => { | ||
// assert, but passthrough errors so that clean-up may take place | ||
assertActive(state, 'error'); | ||
downstream(END, error); | ||
state = 'ended'; | ||
}, | ||
complete: () => { | ||
// assert, but passthrough errors so that clean-up may take place | ||
assertActive(state, 'complete'); | ||
downstream(END); | ||
state = 'ended'; | ||
}, | ||
}); | ||
@@ -63,0 +98,0 @@ const dispose = typeof consumer === 'function' ? consumer : consumer === null || consumer === void 0 ? void 0 : consumer.stop; |
@@ -56,3 +56,3 @@ { | ||
}, | ||
"version": "1.0.1" | ||
"version": "1.1.0" | ||
} |
@@ -36,2 +36,28 @@ /* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-expressions,no-return-assign */ | ||
type SourceState = 'active' | 'disposed' | 'ended' | 'inactive' | ||
const assertActive = (state: SourceState, method: string) => { | ||
if (state === 'active') return true | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
`This callbag source ${ | ||
state === 'disposed' | ||
? 'was previously disposed' | ||
: state === 'ended' | ||
? 'has previously ended' | ||
: 'has not been started' | ||
}, but '${method}' was invoked.`, | ||
) | ||
return false | ||
} | ||
const assertInactive = (state: SourceState, method: string) => { | ||
if (state !== 'active') return true | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
`This callbag source is already active, but '${method}' was invoked.`, | ||
) | ||
return false | ||
} | ||
export const createSource = | ||
@@ -46,9 +72,7 @@ <Out>( | ||
let state: 'active' | 'disposed' | 'inactive' = 'inactive' | ||
const deactivate = () => state === 'active' && (state = 'inactive') | ||
const activate = () => state !== 'active' && (state = 'active') | ||
let state: SourceState = 'inactive' | ||
const markDisposed = () => state !== 'disposed' && (state = 'disposed') | ||
let init: CallbagArgs<never, Out> | boolean = false | ||
/** talkback - sends messages back upstream */ | ||
/** talkback: handle messages from the sink back to this source */ | ||
// eslint-disable-next-line @typescript-eslint/no-shadow | ||
@@ -60,3 +84,3 @@ const upstream: Source<Out> = (...args) => { | ||
if (askToPull && args[0] === DATA) askToPull() | ||
else if (dispose && args[0] === END) markDisposed() && dispose() | ||
else if (args[0] === END) markDisposed() && dispose?.() | ||
} else { | ||
@@ -71,6 +95,23 @@ init = args | ||
const consumer = onConsume({ | ||
start: () => void (activate() && downstream(START, upstream)), | ||
next: (data: Out) => state === void ('active' && downstream(DATA, data)), | ||
error: (error: unknown) => void (deactivate() && downstream(END, error)), | ||
complete: () => void (deactivate() && downstream(END)), | ||
start: () => { | ||
if (!assertInactive(state, 'start')) return | ||
downstream(START, upstream) | ||
state = 'active' | ||
}, | ||
next: (data: Out) => { | ||
if (!assertActive(state, 'next')) return | ||
downstream(DATA, data) | ||
}, | ||
error: (error: unknown) => { | ||
// assert, but passthrough errors so that clean-up may take place | ||
assertActive(state, 'error') | ||
downstream(END, error) | ||
state = 'ended' | ||
}, | ||
complete: () => { | ||
// assert, but passthrough errors so that clean-up may take place | ||
assertActive(state, 'complete') | ||
downstream(END) | ||
state = 'ended' | ||
}, | ||
}) | ||
@@ -77,0 +118,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
109450
1349