swr-devtools
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -1,2 +0,2 @@ | ||
import { Middleware } from "swr"; | ||
import type { Middleware } from "swr"; | ||
declare type EventListener = (...args: any[]) => void; | ||
@@ -3,0 +3,0 @@ export declare class EventEmitter { |
@@ -15,6 +15,4 @@ "use strict"; | ||
exports.createSWRDevtools = exports.EventEmitter = void 0; | ||
// We cannot import React nad use hooks that SWR provides because this runs on the application side, | ||
// we have to use the same React instance with the application | ||
var swr_1 = require("swr"); | ||
var swr_cache_1 = require("./swr-cache"); | ||
var serialize_1 = require("./swr/serialize"); | ||
var EventEmitter = /** @class */ (function () { | ||
@@ -42,12 +40,22 @@ function EventEmitter() { | ||
exports.EventEmitter = EventEmitter; | ||
var debug = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
// console.log(...args); | ||
}; | ||
var injected = new WeakSet(); | ||
var devToolsPanelIsOpen = false; | ||
var inject = function (cache) { | ||
return (0, swr_cache_1.injectSWRCache)(cache, function (key, value) { | ||
window.postMessage({ | ||
type: "updated_swr_cache", | ||
payload: (0, swr_cache_1.serializePayload)({ | ||
key: key, | ||
value: value, | ||
}), | ||
}, "*"); | ||
if (devToolsPanelIsOpen) { | ||
window.postMessage({ | ||
type: "updated_swr_cache", | ||
payload: (0, swr_cache_1.serializePayload)({ | ||
key: key, | ||
value: value, | ||
}), | ||
}, "*"); | ||
} | ||
}); | ||
@@ -67,2 +75,16 @@ }; | ||
var events = new EventEmitter(); | ||
if (typeof window !== "undefined") { | ||
window.addEventListener("message", function (e) { | ||
var _a, _b, _c, _d, _e; | ||
if (((_a = e.data) === null || _a === void 0 ? void 0 : _a.type) === "panelshow") { | ||
devToolsPanelIsOpen = true; | ||
} | ||
else if (((_b = e.data) === null || _b === void 0 ? void 0 : _b.type) === "panelhide") { | ||
devToolsPanelIsOpen = false; | ||
} | ||
else if (((_c = e.data) === null || _c === void 0 ? void 0 : _c.type) === "load") { | ||
devToolsPanelIsOpen = (_e = (_d = e.data) === null || _d === void 0 ? void 0 : _d.payload) === null || _e === void 0 ? void 0 : _e.panelIsOpen; | ||
} | ||
}); | ||
} | ||
var swrdevtools = function (useSWRNext) { return function (key, fn, config) { | ||
@@ -87,5 +109,7 @@ // use the same React instance with the application | ||
var requestIdRef = useRef(0); | ||
var serializedKey = (0, swr_1.unstable_serialize)(key); | ||
var serializedKey = (0, serialize_1.serialize)(key); | ||
useEffect(function () { | ||
return function () { | ||
if (!devToolsPanelIsOpen) | ||
return; | ||
// When the key changes or unmounts, ongoing requests should be discarded. | ||
@@ -99,9 +123,17 @@ // This only affects React 17. | ||
}); | ||
events.emit("request_discarded", { | ||
key: serializedKey, | ||
id: requestIdRef.current, | ||
}); | ||
window.postMessage({ | ||
type: "request_discarded", | ||
payload: (0, swr_cache_1.serializePayload)({ | ||
key: serializedKey, | ||
id: requestIdRef.current, | ||
}), | ||
}, "*"); | ||
} | ||
}; | ||
}, [serializedKey]); | ||
debug({ devToolsPanelIsOpen: devToolsPanelIsOpen }); | ||
// If DevToolsPanel is not opened, we don't do anything. | ||
if (!devToolsPanelIsOpen) { | ||
return useSWRNext(key, fn, config); | ||
} | ||
var wrappedFn = fn | ||
@@ -115,3 +147,3 @@ ? function () { | ||
events.emit("request_start", { | ||
key: (0, swr_1.unstable_serialize)(args[0]), | ||
key: (0, serialize_1.serialize)(args[0]), | ||
id: id, | ||
@@ -122,3 +154,3 @@ }); | ||
payload: (0, swr_cache_1.serializePayload)({ | ||
key: (0, swr_1.unstable_serialize)(args[0]), | ||
key: (0, serialize_1.serialize)(args[0]), | ||
id: id, | ||
@@ -134,3 +166,3 @@ }), | ||
var payload = { | ||
key: (0, swr_1.unstable_serialize)(args[0]), | ||
key: (0, serialize_1.serialize)(args[0]), | ||
id: id, | ||
@@ -149,3 +181,3 @@ data: r, | ||
var payload = { | ||
key: (0, swr_1.unstable_serialize)(args[0]), | ||
key: (0, serialize_1.serialize)(args[0]), | ||
id: id, | ||
@@ -178,3 +210,3 @@ error: e, | ||
events.emit("request_discarded", { | ||
key: (0, swr_1.unstable_serialize)(args[0]), | ||
key: (0, serialize_1.serialize)(args[0]), | ||
id: requestIdRef.current, | ||
@@ -185,3 +217,3 @@ }); | ||
payload: (0, swr_cache_1.serializePayload)({ | ||
key: (0, swr_1.unstable_serialize)(args[0]), | ||
key: (0, serialize_1.serialize)(args[0]), | ||
id: requestIdRef.current, | ||
@@ -188,0 +220,0 @@ }), |
@@ -1,2 +0,2 @@ | ||
import { Cache } from "swr"; | ||
import type { Cache } from "swr"; | ||
export declare type DevToolsCacheData = { | ||
@@ -3,0 +3,0 @@ key: string; |
@@ -1,2 +0,2 @@ | ||
import { Middleware } from "swr"; | ||
import type { Middleware } from "swr"; | ||
declare type EventListener = (...args: any[]) => void; | ||
@@ -3,0 +3,0 @@ export declare class EventEmitter { |
@@ -12,6 +12,4 @@ var __assign = (this && this.__assign) || function () { | ||
}; | ||
// We cannot import React nad use hooks that SWR provides because this runs on the application side, | ||
// we have to use the same React instance with the application | ||
import { unstable_serialize } from "swr"; | ||
import { injectSWRCache, serializePayload } from "./swr-cache"; | ||
import { serialize as unstable_serialize } from "./swr/serialize"; | ||
var EventEmitter = /** @class */ (function () { | ||
@@ -39,12 +37,22 @@ function EventEmitter() { | ||
export { EventEmitter }; | ||
var debug = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
// console.log(...args); | ||
}; | ||
var injected = new WeakSet(); | ||
var devToolsPanelIsOpen = false; | ||
var inject = function (cache) { | ||
return injectSWRCache(cache, function (key, value) { | ||
window.postMessage({ | ||
type: "updated_swr_cache", | ||
payload: serializePayload({ | ||
key: key, | ||
value: value, | ||
}), | ||
}, "*"); | ||
if (devToolsPanelIsOpen) { | ||
window.postMessage({ | ||
type: "updated_swr_cache", | ||
payload: serializePayload({ | ||
key: key, | ||
value: value, | ||
}), | ||
}, "*"); | ||
} | ||
}); | ||
@@ -64,2 +72,16 @@ }; | ||
var events = new EventEmitter(); | ||
if (typeof window !== "undefined") { | ||
window.addEventListener("message", function (e) { | ||
var _a, _b, _c, _d, _e; | ||
if (((_a = e.data) === null || _a === void 0 ? void 0 : _a.type) === "panelshow") { | ||
devToolsPanelIsOpen = true; | ||
} | ||
else if (((_b = e.data) === null || _b === void 0 ? void 0 : _b.type) === "panelhide") { | ||
devToolsPanelIsOpen = false; | ||
} | ||
else if (((_c = e.data) === null || _c === void 0 ? void 0 : _c.type) === "load") { | ||
devToolsPanelIsOpen = (_e = (_d = e.data) === null || _d === void 0 ? void 0 : _d.payload) === null || _e === void 0 ? void 0 : _e.panelIsOpen; | ||
} | ||
}); | ||
} | ||
var swrdevtools = function (useSWRNext) { return function (key, fn, config) { | ||
@@ -87,2 +109,4 @@ // use the same React instance with the application | ||
return function () { | ||
if (!devToolsPanelIsOpen) | ||
return; | ||
// When the key changes or unmounts, ongoing requests should be discarded. | ||
@@ -96,9 +120,17 @@ // This only affects React 17. | ||
}); | ||
events.emit("request_discarded", { | ||
key: serializedKey, | ||
id: requestIdRef.current, | ||
}); | ||
window.postMessage({ | ||
type: "request_discarded", | ||
payload: serializePayload({ | ||
key: serializedKey, | ||
id: requestIdRef.current, | ||
}), | ||
}, "*"); | ||
} | ||
}; | ||
}, [serializedKey]); | ||
debug({ devToolsPanelIsOpen: devToolsPanelIsOpen }); | ||
// If DevToolsPanel is not opened, we don't do anything. | ||
if (!devToolsPanelIsOpen) { | ||
return useSWRNext(key, fn, config); | ||
} | ||
var wrappedFn = fn | ||
@@ -105,0 +137,0 @@ ? function () { |
@@ -1,2 +0,2 @@ | ||
import { Cache } from "swr"; | ||
import type { Cache } from "swr"; | ||
export declare type DevToolsCacheData = { | ||
@@ -3,0 +3,0 @@ key: string; |
{ | ||
"name": "swr-devtools", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "A React component for SWR DevTools", | ||
@@ -18,2 +18,7 @@ "main": "cjs/index.js", | ||
"types": "./lib/swr-cache.d.ts" | ||
}, | ||
"./lib/createSWRDevTools": { | ||
"import": "./lib/createSWRDevTools.js", | ||
"require": "./cjs/createSWRDevTools.js", | ||
"types": "./lib/createSWRDevTools.d.ts" | ||
} | ||
@@ -61,3 +66,3 @@ }, | ||
}, | ||
"gitHead": "c14a5fe0e5b3b9bcd1526071caf14f8d9fae3b3f" | ||
"gitHead": "596e4c215758bc7496849ed4e9f6d11802eaa9e3" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67937
39
1060