value: Convert from a getter to a method.
This commit is contained in:
parent
8ec8fdacb2
commit
5470201c79
3 changed files with 196 additions and 195 deletions
18
doc/API.md
18
doc/API.md
|
@ -6,7 +6,7 @@
|
|||
- [writeOnly() -> Channel](#writeonly---channel)
|
||||
- [Channel.select(promises) -> async channel](#channelselectpromises---async-channel)
|
||||
- [Examples](#examples)
|
||||
- [value](#value)
|
||||
- [value()](#value)
|
||||
- [Array-like Properties](#array-like-properties)
|
||||
- [Channel](#channel)
|
||||
- [Channel([bufferLength = 0]) -> Channel](#channelbufferlength--0---channel)
|
||||
|
@ -71,11 +71,11 @@ switch (await Channel.select([
|
|||
charlie.push(`Hi!`)
|
||||
])) {
|
||||
case alice:
|
||||
console.log(`Alice said ${alice.value}.`);
|
||||
console.log(`Alice said ${alice.value()}.`);
|
||||
break;
|
||||
|
||||
case bob:
|
||||
console.log(`Bob said ${bob.value}.`);
|
||||
console.log(`Bob said ${bob.value()}.`);
|
||||
break;
|
||||
|
||||
case charlie:
|
||||
|
@ -108,11 +108,11 @@ closed.close();
|
|||
|
||||
switch (await Channel.select([alice.shift(), bob.shift(), closed.shift())]) {
|
||||
case alice:
|
||||
console.log(`Alice said ${alice.value}.`);
|
||||
console.log(`Alice said ${alice.value()}.`);
|
||||
break;
|
||||
|
||||
case bob:
|
||||
console.log(`Bob said ${bob.value}.`);
|
||||
console.log(`Bob said ${bob.value()}.`);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -128,11 +128,11 @@ setTimeout(timeout.close, 1000);
|
|||
|
||||
switch (await Channel.select([alice.shift(), bob.shift(), timeout.shift())]) {
|
||||
case alice:
|
||||
console.log(`Alice said ${alice.value}.`);
|
||||
console.log(`Alice said ${alice.value()}.`);
|
||||
break;
|
||||
|
||||
case bob:
|
||||
console.log(`Bob said ${bob.value}.`);
|
||||
console.log(`Bob said ${bob.value()}.`);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -140,9 +140,9 @@ switch (await Channel.select([alice.shift(), bob.shift(), timeout.shift())]) {
|
|||
}
|
||||
```
|
||||
|
||||
## value
|
||||
## value()
|
||||
|
||||
Set to the most recently `shift`ed value. This is useful when used in
|
||||
Return the most recently `shift`ed value. This is useful when used in
|
||||
combination with `select`.
|
||||
|
||||
# Array-like Properties
|
||||
|
|
45
lib/index.js
45
lib/index.js
|
@ -36,7 +36,7 @@ const Order = channel => {
|
|||
|
||||
const prototype = {};
|
||||
|
||||
const Channel = function(bufferLength = 0) {
|
||||
const Channel = function(length = 0) {
|
||||
let buffered = 0;
|
||||
let closed = false;
|
||||
let lastValue;
|
||||
|
@ -70,7 +70,7 @@ const Channel = function(bufferLength = 0) {
|
|||
for (
|
||||
;
|
||||
resolvedIndex < index.push ||
|
||||
(resolvedIndex < pushes.length && buffered < bufferLength);
|
||||
(resolvedIndex < pushes.length && buffered < length);
|
||||
resolvedIndex++
|
||||
) {
|
||||
const { cancelled, resolve } = pushes[resolvedIndex];
|
||||
|
@ -80,7 +80,7 @@ const Channel = function(bufferLength = 0) {
|
|||
buffered++;
|
||||
}
|
||||
|
||||
resolve(bufferLength);
|
||||
resolve(length);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,7 +101,8 @@ const Channel = function(bufferLength = 0) {
|
|||
resolvedIndex -= index.push;
|
||||
};
|
||||
|
||||
const readOnly = Object.assign(Object.create(prototype), {
|
||||
const readOnly = Object.freeze(
|
||||
Object.assign(Object.create(prototype), {
|
||||
every: async (callbackfn, thisArg) => {
|
||||
for (;;) {
|
||||
const value = await readOnly.shift();
|
||||
|
@ -241,7 +242,9 @@ const Channel = function(bufferLength = 0) {
|
|||
}
|
||||
},
|
||||
|
||||
toString: () => `Channel(${bufferLength})`,
|
||||
toString: () => `Channel(${length})`,
|
||||
|
||||
value: () => lastValue,
|
||||
|
||||
values: async () => {
|
||||
const array = [];
|
||||
|
@ -252,16 +255,11 @@ const Channel = function(bufferLength = 0) {
|
|||
|
||||
return array;
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
Object.defineProperties(readOnly, {
|
||||
length: { get: () => bufferLength },
|
||||
value: { get: () => lastValue }
|
||||
});
|
||||
|
||||
Object.freeze(readOnly);
|
||||
|
||||
const writeOnly = Object.freeze({
|
||||
const writeOnly = Object.freeze(
|
||||
Object.assign(Object.create(prototype), {
|
||||
close: () =>
|
||||
new Promise((resolve, reject) => {
|
||||
if (closed) {
|
||||
|
@ -275,6 +273,8 @@ const Channel = function(bufferLength = 0) {
|
|||
}
|
||||
}),
|
||||
|
||||
length,
|
||||
|
||||
push: function(value) {
|
||||
const { order, promise } = Order(this);
|
||||
order.value = value;
|
||||
|
@ -283,7 +283,9 @@ const Channel = function(bufferLength = 0) {
|
|||
order.reject(new Error(`Can't push to closed channel.`));
|
||||
} else if (value === undefined) {
|
||||
order.reject(
|
||||
new TypeError(`Can't push 'undefined' to channel, use close instead.`)
|
||||
new TypeError(
|
||||
`Can't push 'undefined' to channel, use close instead.`
|
||||
)
|
||||
);
|
||||
} else if (arguments.length > 1) {
|
||||
order.reject(new Error(`Can't push more than one value at a time.`));
|
||||
|
@ -296,10 +298,12 @@ const Channel = function(bufferLength = 0) {
|
|||
},
|
||||
|
||||
writeOnly: () => writeOnly
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
// Use Object.create because readOnly has a getter.
|
||||
return Object.freeze(Object.assign(Object.create(readOnly), writeOnly));
|
||||
return Object.freeze(
|
||||
Object.assign(Object.create(prototype), readOnly, writeOnly)
|
||||
);
|
||||
};
|
||||
|
||||
Channel.from = values => {
|
||||
|
@ -384,7 +388,10 @@ Channel.select = methodPromises =>
|
|||
// Channel.slice(10)(Infinity)(channel)
|
||||
|
||||
const channel = Channel();
|
||||
const methods = Object.keys(channel).concat(Object.keys(channel.readOnly()));
|
||||
|
||||
const methods = Object.keys(channel).filter(
|
||||
method => typeof channel[method] === `function`
|
||||
);
|
||||
|
||||
methods.forEach(method => {
|
||||
const bound = function(...args) {
|
||||
|
|
|
@ -63,6 +63,8 @@ describe(`Channel`, function() {
|
|||
it(`isChannel`, function() {
|
||||
assert(Channel.isChannel(Channel.of(0, 1, 2)));
|
||||
assert(!Channel.isChannel(Array.of(0, 1, 2)));
|
||||
assert(Channel.isChannel(Channel().readOnly()));
|
||||
assert(Channel.isChannel(Channel().writeOnly()));
|
||||
});
|
||||
|
||||
it(`of`, async function() {
|
||||
|
@ -80,7 +82,7 @@ describe(`Channel`, function() {
|
|||
})();
|
||||
|
||||
assert.equal(await Channel.select([a.shift(), b.shift()]), b);
|
||||
assert.equal(b.value, 0);
|
||||
assert.equal(b.value(), 0);
|
||||
assert.equal(await a.shift(), 1);
|
||||
assert.equal(await Channel.select([a.push(0), b.shift()]), a);
|
||||
});
|
||||
|
@ -294,7 +296,7 @@ describe(`Channel object`, function() {
|
|||
|
||||
assert.equal(readOnly.readOnly(), readOnly);
|
||||
assert.equal(await readOnly.shift(), 1);
|
||||
assert.equal(readOnly.value, 1);
|
||||
assert.equal(readOnly.value(), 1);
|
||||
|
||||
assert.throws(() => {
|
||||
readOnly.frozen = false;
|
||||
|
@ -367,21 +369,13 @@ describe(`Channel object`, function() {
|
|||
});
|
||||
|
||||
it(`value`, async function() {
|
||||
const channel = Channel();
|
||||
(async () => {
|
||||
await channel.push(0);
|
||||
})();
|
||||
|
||||
const channel = Channel(1);
|
||||
await channel.push(null);
|
||||
await channel.shift();
|
||||
assert.equal(channel.value, 0);
|
||||
|
||||
assert.throws(() => {
|
||||
channel.value = 1;
|
||||
});
|
||||
|
||||
assert.equal(channel.value(), null);
|
||||
channel.close();
|
||||
await channel.shift();
|
||||
assert.equal(channel.value, undefined);
|
||||
assert.equal(channel.value(), undefined);
|
||||
});
|
||||
|
||||
it(`values`, async function() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue