Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Flow issues in SimpleCacheProvider #12942

Merged
merged 2 commits into from
May 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 42 additions & 21 deletions packages/simple-cache-provider/src/SimpleCacheProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,34 @@ const Pending = 1;
const Resolved = 2;
const Rejected = 3;

type EmptyRecord<K, V> = {|
type EmptyRecord<K> = {|
status: 0,
suspender: null,
key: K,
value: null,
error: null,
next: Record<K, V> | null,
previous: Record<K, V> | null,
next: any, // TODO: (issue #12941)
previous: any, // TODO: (issue #12941)
/**
* Proper types would be something like this:
* next: Record<K, V> | null,
* previous: Record<K, V> | null,
*/
|};

type PendingRecord<K, V> = {|
status: 1,
suspender: Promise<K, V>,
suspender: Promise<V>,
key: K,
value: null,
error: null,
next: Record<K, V> | null,
previous: Record<K, V> | null,
next: any, // TODO: (issue #12941)
previous: any, // TODO: (issue #12941)
/**
* Proper types would be something like this:
* next: Record<K, V> | null,
* previous: Record<K, V> | null,
*/
|};

type ResolvedRecord<K, V> = {|
Expand All @@ -43,30 +53,41 @@ type ResolvedRecord<K, V> = {|
key: K,
value: V,
error: null,
next: Record<K, V> | null,
previous: Record<K, V> | null,
next: any, // TODO: (issue #12941)
previous: any, // TODO: (issue #12941)
/**
* Proper types would be something like this:
* next: Record<K, V> | null,
* previous: Record<K, V> | null,
*/
|};

type RejectedRecord<K, V> = {|
type RejectedRecord<K> = {|
status: 3,
suspender: null,
key: K,
value: null,
error: Error,
next: Record<K, V> | null,
previous: Record<K, V> | null,
next: any, // TODO: (issue #12941)
previous: any, // TODO: (issue #12941)
/**
* Proper types would be something like this:
* next: Record<K, V> | null,
* previous: Record<K, V> | null,
*/
|};

type Record<K, V> =
| EmptyRecord<K, V>
| EmptyRecord<K>
| PendingRecord<K, V>
| ResolvedRecord<K, V>
| RejectedRecord<K, V>;
| RejectedRecord<K>;

type RecordCache<K, V> = {|
map: Map<K, Record<K, V>>,
head: Record<K, V> | null,
tail: Record<K, V> | null,
size: number,
|};

// TODO: How do you express this type with Flow?
Expand Down Expand Up @@ -107,7 +128,7 @@ if (__DEV__) {
const MAX_SIZE = 500;
const PAGE_SIZE = 50;

function createRecord<K, V>(key: K): EmptyRecord<K, V> {
function createRecord<K>(key: K): EmptyRecord<K> {
return {
status: Empty,
suspender: null,
Expand All @@ -131,7 +152,7 @@ function createRecordCache<K, V>(): RecordCache<K, V> {
export function createCache(invalidator: () => mixed): Cache {
const resourceMap: ResourceMap = new Map();

function accessRecord<K, V>(resourceType: any, key: K): Record<V> {
function accessRecord<K, V>(resourceType: any, key: K): Record<K, V> {
if (__DEV__) {
warning(
typeof resourceType !== 'string' && typeof resourceType !== 'number',
Expand Down Expand Up @@ -206,22 +227,22 @@ export function createCache(invalidator: () => mixed): Cache {
return newHead;
}

function load<V>(emptyRecord: EmptyRecord, suspender: Promise<V>) {
const pendingRecord: PendingRecord<V> = (emptyRecord: any);
function load<K, V>(emptyRecord: EmptyRecord<K>, suspender: Promise<V>) {
const pendingRecord: PendingRecord<K, V> = (emptyRecord: any);
pendingRecord.status = Pending;
pendingRecord.suspender = suspender;
suspender.then(
value => {
// Resource loaded successfully.
const resolvedRecord: ResolvedRecord<V> = (pendingRecord: any);
const resolvedRecord: ResolvedRecord<K, V> = (pendingRecord: any);
resolvedRecord.status = Resolved;
resolvedRecord.suspender = null;
resolvedRecord.value = value;
},
error => {
// Resource failed to load. Stash the error for later so we can throw it
// the next time it's requested.
const rejectedRecord: RejectedRecord = (pendingRecord: any);
const rejectedRecord: RejectedRecord<K> = (pendingRecord: any);
rejectedRecord.status = Rejected;
rejectedRecord.suspender = null;
rejectedRecord.error = error;
Expand All @@ -239,7 +260,7 @@ export function createCache(invalidator: () => mixed): Cache {
miss: A => Promise<V>,
missArg: A,
): void {
const record: Record<V> = accessRecord(resourceType, key);
const record: Record<K, V> = accessRecord(resourceType, key);
switch (record.status) {
case Empty:
// Warm the cache.
Expand All @@ -263,7 +284,7 @@ export function createCache(invalidator: () => mixed): Cache {
miss: A => Promise<V>,
missArg: A,
): V {
const record: Record<V> = accessRecord(resourceType, key);
const record: Record<K, V> = accessRecord(resourceType, key);
switch (record.status) {
case Empty:
// Load the requested resource.
Expand Down