Skip to content

Commit

Permalink
make use of private class fields instead of Symbols
Browse files Browse the repository at this point in the history
https://github.com/tc39/proposal-class-fields#private-fields

[BREAKING] drop support for node.js <
12
  • Loading branch information
shinnn committed May 1, 2019
1 parent cd425bc commit 2affe41
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ const isPlainObject = require('lodash/isPlainObject');
const jsonStableStringifyWithoutJsonify = require('json-stable-stringify-without-jsonify');

const built = Symbol('built');
const emittedLength = Symbol('emittedLength');
const fullModernizrCode = Symbol('fullModernizrCode');
const read = Symbol('read');
const caches = new Map();

class ModernizrStream extends Readable {
#fullModernizrCode = null;
#emittedLength = 0;

constructor(...args) {
const argLen = args.length;
const [options = {}] = args;
Expand All @@ -36,22 +37,11 @@ class ModernizrStream extends Readable {

super(options);

Object.defineProperties(this, {
[fullModernizrCode]: {
writable: true,
value: null
},
[emittedLength]: {
writable: true,
value: 0
}
});

const cacheKey = jsonStableStringifyWithoutJsonify(options);
const cache = caches.get(cacheKey);

if (cache !== undefined) {
this[fullModernizrCode] = cache;
this.#fullModernizrCode = cache;
this.emit(built);

return;
Expand All @@ -61,24 +51,26 @@ class ModernizrStream extends Readable {
const codeUint8Array = new TextEncoder().encode(code);

caches.set(cacheKey, codeUint8Array);
this[fullModernizrCode] = codeUint8Array;
this.#fullModernizrCode = codeUint8Array;
this.emit(built);
});
}

// Replaceable with a private method `#read()` in the future
// https://github.com/tc39/proposal-private-methods
[read](size) {
const sliced = this[fullModernizrCode].slice(this[emittedLength], this[emittedLength] + size);
const sliced = this.#fullModernizrCode.slice(this.#emittedLength, this.#emittedLength + size);

this[emittedLength] += sliced.length;
this.#emittedLength += sliced.length;
this.push(sliced);

if (this[fullModernizrCode].length === this[emittedLength]) {
if (this.#fullModernizrCode.length === this.#emittedLength) {
this.push(null);
}
}

_read(size) {
if (this[fullModernizrCode] === null) {
if (this.#fullModernizrCode === null) {
this.once(built, () => this[read](size));
return;
}
Expand Down

0 comments on commit 2affe41

Please sign in to comment.