Skip to content

Commit

Permalink
feat: separate SWRDevToolsPanel as a separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Aug 30, 2021
1 parent 945e8b3 commit 4ddbfdb
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 30 deletions.
3 changes: 2 additions & 1 deletion examples/swr-devtools-demo/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "../styles/globals.css";
import { SWRConfig, useSWRConfig } from "swr";
import { SWRDevToolPanel, SWRDevTools } from "swr-devtools";
import { SWRDevTools } from "swr-devtools";
import { SWRDevToolPanel } from "swr-devtools-panel";

// The way to use SWR DevTools as a React Component
const DevToolsArea = () => {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
"build": "run-s build:devtools build:extensions",
"build:devtools": "yarn workspace swr-devtools build",
"build:extensions": "yarn workspace swr-devtools-extentions build",
"build:panel": "yarn workspace swr-devtools-panel build",
"start:demo": "yarn workspace swr-devtools-demo dev",
"start:dev": "yarn workspace swr-devtools start",
"start:extensions": "yarn workspace swr-devtools-extentions start",
"start:panel": "yarn workspace swr-devtools-panel start",
"start": "run-p -l start:*",
"lint:eslint": "eslint '**/pages/**/*' '**/src/**/*'",
"lint:tsc": "yarn workspaces run lint",
Expand Down
2 changes: 1 addition & 1 deletion packages/swr-devtools-extensions/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ReactDOM from "react-dom";
import { SWRDevToolPanel } from "swr-devtools";
import { SWRDevToolPanel } from "swr-devtools-panel";

const cache = new Map();

Expand Down
1 change: 1 addition & 0 deletions packages/swr-devtools-panel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
21 changes: 21 additions & 0 deletions packages/swr-devtools-panel/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Toru Kobayashi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions packages/swr-devtools-panel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# swr-devtools

**THIS IS NOT READY FOR THE USE IN YOUR PROJECT, JUST AN EXPERIMENT**

## How to Use

### Install dependencies

```shell
# This package hasn't been published yet.
npm install swr-devtools
# Install peerDependencies
npm install react swr styled-components
```

**This only supports SWR v1 or later versions.**

### Wrap your application in the SWRDevTools component

```jsx
// This package hasn't been published yet.
import { SWRDevTools } from "swr-devtools";

const App = () => (
<SWRDevTools>
<MainApp />
</SWRDevTools>
)
```

## LICENSE

[MIT](LICENSE.md)
33 changes: 33 additions & 0 deletions packages/swr-devtools-panel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "swr-devtools-panel",
"version": "0.1.0",
"private": true,
"description": "A React component for the SWR DevTools panel",
"main": "lib/index.js",
"license": "MIT",
"files": [
"lib"
],
"author": "koba04",
"scripts": {
"start": "tsc --watch",
"build": "tsc",
"lint": "tsc --noEmit"
},
"peerDependencies": {
"react": "^17.0.2",
"styled-components": "^5.3.0",
"swr": "^1.0.0"
},
"devDependencies": {
"@types/react": "^17.0.16",
"@types/styled-components": "^5.1.12",
"react": "^17.0.2",
"styled-components": "^5.3.0",
"swr": "^1.0.0",
"typescript": "^4.1.3"
},
"dependencies": {
"react-json-view": "^1.21.3"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { lazy, Suspense } from "react";
import styled from "styled-components";
import { SWRCacheData } from "../swr-cache";
import { SWRCacheData } from "swr-devtools/lib/swr-cache";
import { CacheKey } from "./CacheKey";

type Props = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React from "react";
import styled from "styled-components";

import { isInfiniteCache, getInfiniteCacheKey } from "../swr-cache";
import {
isInfiniteCache,
getInfiniteCacheKey,
} from "swr-devtools/lib/swr-cache";

export const CacheKey = ({ cacheKey }: { cacheKey: string }) => {
if (isInfiniteCache(cacheKey)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import styled from "styled-components";

import { SWRCacheData } from "../swr-cache";
import { SWRCacheData } from "swr-devtools/lib/swr-cache";
import { PanelType, ItemKey } from "./SWRDevToolPanel";
import { CacheData } from "./CacheData";
import { CacheKey } from "./CacheKey";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useState } from "react";
import styled, { createGlobalStyle } from "styled-components";
import { Cache } from "swr";

import { useDevToolsCache, SWRCache } from "../devtools-cache";
import { useDevToolsCache } from "../devtools-cache";
import { Panel } from "./Panel";
import { Tab } from "./Tab";

Expand All @@ -20,7 +21,7 @@ const panels: Panel[] = [
];

type Props = {
cache: SWRCache;
cache: Cache;
isFixedPosition?: boolean;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { useState, useEffect } from "react";
import { Cache } from "swr";

import { injectSWRCache, isMetaCache, SWRCacheData } from "./swr-cache";

// This is the Cache interface for SWR v1, but this only allows a string as its key
// https://github.com/vercel/swr/blob/6e25b3b123541db5e042e5d359683575d3631df1/src/types.ts#L183
export type SWRCache<Data = any> = {
get(key: string): Data | null | undefined;
set(key: string, value: Data): void;
delete(key: string): void;
};
import {
injectSWRCache,
isMetaCache,
SWRCacheData,
} from "swr-devtools/lib/swr-cache";

type DevToolsCache<Value = any> = {
get(key: string): Value;
Expand All @@ -18,7 +15,7 @@ type DevToolsCache<Value = any> = {
subscribe(fn: (key: string, value: Value) => void): () => void;
};

const createDevToolsCache = (cache: SWRCache): DevToolsCache => {
const createDevToolsCache = (cache: Cache): DevToolsCache => {
let listeners: Array<(key: string, value: any) => void> = [];
const store: DevToolsCache = {
get(key) {
Expand Down Expand Up @@ -88,7 +85,7 @@ const retrieveCache = (
};

export const useDevToolsCache = (
cache: SWRCache
cache: Cache
): [SWRCacheData[], SWRCacheData[]] => {
const [cacheData, setCacheData] = useState<[SWRCacheData[], SWRCacheData[]]>([
[],
Expand Down
1 change: 1 addition & 0 deletions packages/swr-devtools-panel/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SWRDevToolPanel } from "./components/SWRDevToolPanel";
70 changes: 70 additions & 0 deletions packages/swr-devtools-panel/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"lib": ["dom", "es2017"], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
"jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./lib", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
20 changes: 14 additions & 6 deletions packages/swr-devtools/package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
{
"name": "swr-devtools",
"version": "0.1.0",
"description": "A React component for SWR DevTools",
"main": "lib/index.js",
"license": "MIT",
"files": [
"lib"
],
"keywords": [
"swr"
],
"author": "koba04",
"repository": {
"type": "git",
"url": "https://github.com/koba04/swr-devtools.git"
},
"homepage": "https://github.com/koba04/swr-devtools",
"bugs": "https://github.com/koba04/swr-devtools/issues",
"scripts": {
"start": "tsc --watch",
"build": "tsc",
"lint": "tsc --noEmit"
},
"peerDependencies": {
"react": "^17.0.2",
"styled-components": "^5.3.0",
"swr": "^1.0.0"
},
"devDependencies": {
"@types/react": "^17.0.16",
"@types/styled-components": "^5.1.12",
"react": "^17.0.2",
"styled-components": "^5.3.0",
"swr": "^1.0.0",
"typescript": "^4.1.3"
},
"dependencies": {
"react-json-view": "^1.21.3"
}
}
5 changes: 2 additions & 3 deletions packages/swr-devtools/src/SWRDevTools.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from "react";
import { useSWRConfig, SWRConfig, Middleware } from "swr";
import { SWRCache } from "./devtools-cache";
import { useSWRConfig, SWRConfig, Middleware, Cache } from "swr";

import { injectSWRCache, isMetaCache } from "./swr-cache";

const injected = new WeakSet();

const inject = (cache: SWRCache) =>
const inject = (cache: Cache) =>
injectSWRCache(cache, (key: string, value: any) => {
if (isMetaCache(key)) {
return;
Expand Down
1 change: 0 additions & 1 deletion packages/swr-devtools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { SWRDevToolPanel } from "./components/SWRDevToolPanel";
export { SWRDevTools } from "./SWRDevTools";
4 changes: 2 additions & 2 deletions packages/swr-devtools/src/swr-cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SWRCache } from "./devtools-cache";
import { Cache } from "swr";

export type SWRCacheData = {
id: number;
Expand All @@ -11,7 +11,7 @@ export type SWRCacheData = {
};

export const injectSWRCache = (
cache: SWRCache,
cache: Cache,
watcher: (key: string, value: any) => void
): void => {
// intercept operations modifying the cache store
Expand Down

0 comments on commit 4ddbfdb

Please sign in to comment.