Skip to content

Commit 17cdcc7

Browse files
authored
Use a symbol to indicate an unset initial value, to avoid treating undefined initial/reduced values as unset (#33)
1 parent 9493301 commit 17cdcc7

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

observable.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const isDocumentFullyActive = (d) => d && d.defaultView !== null && d.defaultVie
1010
// check if we run in a browser
1111
const isBrowserContext = () => !!Window && globalThis instanceof Window;
1212

13+
const unset = Symbol("unset");
14+
1315
const [Observable, Subscriber] = (() => {
1416
function enumerate(obj, key, enumerable = true) {
1517
Object.defineProperty(obj, key, {
@@ -1641,13 +1643,13 @@ const [Observable, Subscriber] = (() => {
16411643
// 6. Let idx be an unsigned long long, initially 0.
16421644
let idx = 0;
16431645
// 7. Let accumulator be initialValue if it is given, and uninitialized otherwise.
1644-
let accumulator = initialValue;
1646+
let accumulator = arguments.length > 1 ? initialValue : unset;
16451647
// 8. Let observer be a new internal observer, initialized as follows:
16461648
const observer = new InternalObserver({
16471649
// next steps
16481650
next(value) {
16491651
// 8.1 If accumulator is uninitialized (meaning no initialValue was passed in), then set accumulator to the passed in value, set idx to idx + 1, and abort these steps.
1650-
if (accumulator === undefined) {
1652+
if (accumulator === unset) {
16511653
accumulator = value;
16521654
idx++;
16531655
return;
@@ -1675,7 +1677,7 @@ const [Observable, Subscriber] = (() => {
16751677
complete() {
16761678
// 1. If accumulator is not "unset", then resolve p with accumulator.
16771679
// Otherwise, reject p with a TypeError.
1678-
if (accumulator !== undefined) {
1680+
if (accumulator !== unset) {
16791681
resolve(accumulator);
16801682
} else {
16811683
reject(new TypeError('no initial value provided and no values emitted'));

0 commit comments

Comments
 (0)