Skip to content

Commit eaa0df6

Browse files
committed
Don't allocate the inner cache unnecessarily
1 parent dd40435 commit eaa0df6

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

packages/react-fs/src/ReactFilesystem.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ const Rejected = 2;
2020
type PendingRecord = {|
2121
status: 0,
2222
value: Wakeable,
23-
cache: Array<mixed>,
23+
cache: null,
2424
|};
2525

2626
type ResolvedRecord<T> = {|
2727
status: 1,
2828
value: T,
29-
cache: Array<mixed>,
29+
cache: null | Array<mixed>,
3030
|};
3131

3232
type RejectedRecord = {|
3333
status: 2,
3434
value: mixed,
35-
cache: Array<mixed>,
35+
cache: null,
3636
|};
3737

3838
type Record<T> = PendingRecord | ResolvedRecord<T> | RejectedRecord;
@@ -41,7 +41,7 @@ function createRecordFromThenable<T>(thenable: Thenable<T>): Record<T> {
4141
const record: Record<T> = {
4242
status: Pending,
4343
value: thenable,
44-
cache: [],
44+
cache: null,
4545
};
4646
thenable.then(
4747
value => {
@@ -62,9 +62,9 @@ function createRecordFromThenable<T>(thenable: Thenable<T>): Record<T> {
6262
return record;
6363
}
6464

65-
function readRecordValue<T>(record: Record<T>): T {
65+
function readRecord<T>(record: Record<T>): ResolvedRecord<T> {
6666
if (record.status === Resolved) {
67-
return record.value;
67+
return record;
6868
} else {
6969
throw record.value;
7070
}
@@ -114,7 +114,8 @@ export function readFile(
114114
record = createRecordFromThenable(thenable);
115115
map.set(path, record);
116116
}
117-
const buffer: Buffer = readRecordValue(record);
117+
const resolvedRecord = readRecord(record);
118+
const buffer: Buffer = resolvedRecord.value;
118119
if (!options) {
119120
return buffer;
120121
}
@@ -136,7 +137,7 @@ export function readFile(
136137
if (typeof encoding !== 'string') {
137138
return buffer;
138139
}
139-
const textCache = record.cache;
140+
const textCache = resolvedRecord.cache || (resolvedRecord.cache = []);
140141
for (let i = 0; i < textCache.length; i += 2) {
141142
if (textCache[i] === encoding) {
142143
return (textCache[i + 1]: any);

0 commit comments

Comments
 (0)