1import { TalkbackFn, TeardownFn, Start, Push, SignalKind } from './types';
2
3/** Placeholder {@link TeardownFn | teardown functions} that's a no-op.
4 * @see {@link TeardownFn} for the definition and usage of teardowns.
5 * @internal
6 */
7export const teardownPlaceholder: TeardownFn = () => {
8 /*noop*/
9};
10
11/** Placeholder {@link TalkbackFn | talkback function} that's a no-op.
12 * @privateRemarks
13 * This is frequently used in the codebase as a no-op initializer value for talkback functions in
14 * the implementation of {@link Operator | Operators}. This is cheaper than initializing the
15 * variables of talkbacks to `undefined` or `null` and performing an extra check before calling
16 * them. Since the {@link Start | Start signal} is assumed to come first and carry a talkback, we can
17 * use this to our advantage and use a no-op placeholder before {@link Start} is received.
18 *
19 * @internal
20 */
21export const talkbackPlaceholder: TalkbackFn = teardownPlaceholder;
22
23/** Wraps the passed {@link TalkbackFn | talkback function} in a {@link Start | Start signal}.
24 * @internal
25 */
26export function start<T>(talkback: TalkbackFn): Start<T> {
27 return {
28 tag: SignalKind.Start,
29 0: talkback,
30 } as Start<T>;
31}
32
33/** Wraps the passed value in a {@link Push | Push signal}.
34 * @internal
35 */
36export function push<T>(value: T): Push<T> {
37 return {
38 tag: SignalKind.Push,
39 0: value,
40 } as Push<T>;
41}
42
43/** Returns the well-known symbol specifying the default AsyncIterator.
44 * @internal
45 */
46export const asyncIteratorSymbol = (): typeof Symbol.asyncIterator =>
47 (typeof Symbol === 'function' && Symbol.asyncIterator) || ('@@asyncIterator' as any);
48
49/** Returns the well-known symbol specifying the default ES Observable.
50 * @privateRemarks
51 * This symbol is used to mark an object as a default ES Observable. By the specification, an object
52 * that abides by the default Observable implementation must carry a method set to this well-known
53 * symbol that returns the Observable implementation. It's common for this object to be an
54 * Observable itself and return itself on this method.
55 *
56 * @see {@link https://github.com/0no-co/wonka/issues/122} for notes on the intercompatibility
57 * between Observable implementations.
58 *
59 * @internal
60 */
61export const observableSymbol = (): typeof Symbol.observable =>
62 (typeof Symbol === 'function' && Symbol.observable) || ('@@observable' as any);