-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement TypeScript types via JSDoc comments.
Fixes jaydenseric/graphql-react#6 .
- Loading branch information
1 parent
63130d7
commit 047ed7f
Showing
80 changed files
with
1,924 additions
and
1,773 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"typescript.disableAutomaticTypeAcquisition": true, | ||
"typescript.enablePromptUseWorkspaceTsdk": true, | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,70 @@ | ||
// @ts-check | ||
|
||
/** | ||
* Cache store. | ||
* @kind class | ||
* @name Cache | ||
* @param {object} [store={}] Initial [cache store]{@link Cache#store}. Useful for hydrating cache data from a server side render prior to the initial client side render. | ||
* @example <caption>How to import.</caption> | ||
* ```js | ||
* import Cache from "graphql-react/Cache.mjs"; | ||
* ``` | ||
* @example <caption>Construct a new instance.</caption> | ||
* ```js | ||
* const cache = new Cache(); | ||
* ``` | ||
* @see {@link CacheEventMap `CacheEventMap`} for a map of possible events. | ||
*/ | ||
export default class Cache extends EventTarget { | ||
/** | ||
* @param {CacheStore} [store] Initial {@link Cache.store cache store} record. | ||
* Defaults to `{}`. Useful for hydrating cache data from a server side | ||
* render prior to the initial client side render. | ||
*/ | ||
constructor(store = {}) { | ||
super(); | ||
|
||
if (typeof store !== "object" || !store || Array.isArray(store)) | ||
throw new TypeError("Constructor argument 1 `store` must be an object."); | ||
|
||
/** | ||
* Store of cache [keys]{@link CacheKey} and [values]{@link CacheValue}. | ||
* @kind member | ||
* @name Cache#store | ||
* @type {object} | ||
* Store of cache {@link CacheKey keys} and associated | ||
* {@link CacheValue values}. | ||
* @type {CacheStore} | ||
*/ | ||
this.store = store; | ||
} | ||
} | ||
|
||
/** | ||
* Signals that a [cache store]{@link Cache#store} entry was set. The event name | ||
* starts with the [cache key]{@link CacheKey} of the set entry, followed by | ||
* `/set`. | ||
* @kind event | ||
* @name Cache#event:set | ||
* @type {CustomEvent} | ||
* @prop {object} detail Event detail. | ||
* @prop {CacheValue} detail.cacheValue Cache value that was set. | ||
* Map of possible {@linkcode Cache} events. Note that the keys don’t match the | ||
* dispatched event names that dynamically contain the associated | ||
* {@link CacheKey cache key}. | ||
* @typedef {object} CacheEventMap | ||
* @prop {CustomEvent<CacheEventSetDetail>} set Signals that a | ||
* {@link Cache.store cache store} entry was set. The event name starts with | ||
* the {@link CacheKey cache key} of the set entry, followed by `/set`. | ||
* @prop {CustomEvent} stale Signals that a {@link Cache.store cache store} | ||
* entry is now stale (often due to a mutation) and should probably be | ||
* reloaded. The event name starts with the | ||
* {@link CacheKey cache key} of the stale entry, followed by `/stale`. | ||
* @prop {CustomEvent} prune Signals that a {@link Cache.store cache store} | ||
* entry will be deleted unless the event is canceled via | ||
* `event.preventDefault()`. The event name starts with the | ||
* {@link CacheKey cache key} of the entry being pruned, followed by `/prune`. | ||
* @prop {CustomEvent} delete Signals that a {@link Cache.store cache store} | ||
* entry was deleted. The event name starts with the | ||
* {@link CacheKey cache key} of the deleted entry, followed by `/delete`. | ||
*/ | ||
|
||
/** | ||
* @typedef {object} CacheEventSetDetail | ||
* @prop {CacheValue} cacheValue The set {@link CacheValue cache value}. | ||
*/ | ||
|
||
/** | ||
* Signals that a [cache store]{@link Cache#store} entry is now stale (often due | ||
* to a mutation) and should probably be reloaded. The event name starts with | ||
* the [cache key]{@link CacheKey} of the stale entry, followed by `/stale`. | ||
* @kind event | ||
* @name Cache#event:stale | ||
* @type {CustomEvent} | ||
* A unique key to access a {@link CacheValue cache value}. | ||
* @typedef {string} CacheKey | ||
*/ | ||
|
||
/** | ||
* Signals that a [cache store]{@link Cache#store} entry will be deleted unless | ||
* the event is canceled via `event.preventDefault()`. The event name starts | ||
* with the [cache key]{@link CacheKey} of the entry being pruned, followed by | ||
* `/prune`. | ||
* @kind event | ||
* @name Cache#event:prune | ||
* @type {CustomEvent} | ||
* A {@link Cache.store cache store} value. If server side rendering, it should | ||
* be JSON serializable for client hydration. It should contain information | ||
* about any errors that occurred during loading so they can be rendered, and if | ||
* server side rendering, be hydrated on the client. | ||
* @typedef {unknown} CacheValue | ||
*/ | ||
|
||
/** | ||
* Signals that a [cache store]{@link Cache#store} entry was deleted. The event | ||
* name starts with the [cache key]{@link CacheKey} of the deleted entry, | ||
* followed by `/delete`. | ||
* @kind event | ||
* @name Cache#event:delete | ||
* @type {CustomEvent} | ||
* Cache store record. | ||
* @typedef {Record<CacheKey, CacheValue>} CacheStore | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,40 @@ | ||
// @ts-check | ||
|
||
import { strictEqual } from "assert"; | ||
import React from "react"; | ||
import ReactTestRenderer from "react-test-renderer"; | ||
import ReactDOMServer from "react-dom/server.js"; | ||
import Cache from "./Cache.mjs"; | ||
import CacheContext from "./CacheContext.mjs"; | ||
import assertBundleSize from "./test/assertBundleSize.mjs"; | ||
|
||
/** | ||
* Adds `CacheContext` tests. | ||
* @param {import("test-director").default} tests Test director. | ||
*/ | ||
export default (tests) => { | ||
tests.add("`CacheContext` bundle size.", async () => { | ||
await assertBundleSize(new URL("./CacheContext.mjs", import.meta.url), 120); | ||
}); | ||
|
||
tests.add("`CacheContext` used as a React context.", () => { | ||
const TestComponent = () => React.useContext(CacheContext); | ||
const contextValue = "a"; | ||
const testRenderer = ReactTestRenderer.create( | ||
let contextValue; | ||
|
||
/** Test component. */ | ||
function TestComponent() { | ||
contextValue = React.useContext(CacheContext); | ||
return null; | ||
} | ||
|
||
const value = new Cache(); | ||
|
||
ReactDOMServer.renderToStaticMarkup( | ||
React.createElement( | ||
CacheContext.Provider, | ||
{ value: contextValue }, | ||
{ value }, | ||
React.createElement(TestComponent) | ||
) | ||
); | ||
|
||
strictEqual(testRenderer.toJSON(), contextValue); | ||
strictEqual(contextValue, value); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
// @ts-check | ||
|
||
/** @typedef {import("./useAutoLoad.mjs").default} useAutoLoad */ | ||
|
||
/** | ||
* Number of milliseconds after the first client render that’s considered the | ||
* hydration time; during which the | ||
* [`useAutoLoad`]{@link useAutoLoad} React hook won’t load if the | ||
* cache entry is already populated. | ||
* @kind constant | ||
* @name HYDRATION_TIME_MS | ||
* @type {number} | ||
* @example <caption>How to import.</caption> | ||
* ```js | ||
* import HYDRATION_TIME_MS from "graphql-react/HYDRATION_TIME_MS.mjs"; | ||
* ``` | ||
* hydration time; during which the {@linkcode useAutoLoad} React hook won’t | ||
* load if the cache entry is already populated. | ||
*/ | ||
export default 1000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.