Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 7, 2019
1 parent 1f357e9 commit cfe206d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
52 changes: 25 additions & 27 deletions packages/babel-core/src/config/caching.js
@@ -1,7 +1,5 @@
// @flow

/*:: declare var invariant; */

import gensync, { type Handler } from "gensync";
import {
maybeAsync,
Expand Down Expand Up @@ -73,6 +71,31 @@ export function makeStrongCacheSync<ArgT, ResultT, SideChannel>(
);
}

/* NOTE: Part of the logic explained in this comment is explained in the
* getCachedValueOrWait and setupAsyncLocks functions.
*
* > There are only two hard things in Computer Science: cache invalidation and naming things.
* > -- Phil Karlton
*
* I don't know if Phil was also thinking about handling a cache whose invalidation function is
* defined asynchronously is considered, but it is REALLY hard to do correctly.
*
* The implemented logic (only when gensync is run asynchronously) is the following:
* 1. If there is a valid cache associated to the current "arg" parameter,
* a. RETURN the cached value
* 3. If there is a FinishLock associated to the current "arg" parameter representing a valid cache,
* a. Wait for that lock to be released
* b. RETURN the value associated with that lock
* 5. Start executing the function to be cached
* a. If it pauses on a promise, then
* i. Let FinishLock be a new lock
* ii. Store FinishLock as associated to the current "arg" parameter
* iii. Wait for the function to finish executing
* iv. Release FinishLock
* v. Send the function result to anyone waiting on FinishLock
* 6. Store the result in the cache
* 7. RETURN the result
*/
function makeCachedFunction<ArgT, ResultT, SideChannel, Cache: *>(
CallCache: Class<Cache>,
handler: (ArgT, CacheConfigurator<SideChannel>) => Handler<ResultT> | ResultT,
Expand Down Expand Up @@ -150,30 +173,6 @@ function* getCachedValue<
return { valid: false, value: null };
}

/* NOTE: This comment refers to both the getCachedValueOrWait and setupAsyncLocks functions.
*
* > There are only two hard things in Computer Science: cache invalidation and naming things.
* > -- Phil Karlton
*
* I don't know if Phil was also thinking about handling a cache whose invalidation function is
* defined asynchronously is considered, but it is REALLY hard to do correctly.
*
* The implemented logic (only when gensync is run asynchronously) is the following:
* 1. If there is a valid cache associated to the current "arg" parameter,
* a. RETURN the cached value
* 3. If there is a FinishLock associated to the current "arg" parameter representing a valid cache,
* a. Wait for that lock to be released
* b. RETURN the value associated with that lock
* 5. Start executing the function to be cached
* a. If it pauses on a promise, then
* i. Let FinishLock be a new lock
* ii. Store FinishLock as associated to the current "arg" parameter
* iii. Wait for the function to finish executing
* iv. Release FinishLock
* v. Send the function result to anyone waiting on FinishLock
* 6. Store the result in the cache
* 7. RETURN the result
*/
function* getCachedValueOrWait<ArgT, ResultT, SideChannel>(
asyncContext: boolean,
callCache: CacheMap<ArgT, ResultT, SideChannel>,
Expand Down Expand Up @@ -253,7 +252,6 @@ class CacheConfigurator<SideChannel = void> {
_invalidate: boolean = false;

_configured: boolean = false;
_configuredHandler: ?Function = null;

_pairs: Array<[mixed, (SideChannel) => Handler<mixed>]> = [];

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/src/gensync-utils/async.js
Expand Up @@ -14,7 +14,7 @@ const runGenerator = gensync(function*(item) {
// asynchronous, otherwise it returns false.
export const isAsync = gensync<[], boolean>({
sync: () => false,
async: async () => true,
errback: cb => cb(null, true),
});

// This function wraps any functions (which could be either synchronous or
Expand Down

0 comments on commit cfe206d

Please sign in to comment.