simpler-state
Advanced tools
Comparing version 2.0.0-pre.5 to 2.0.0-pre.6
@@ -0,1 +1,2 @@ | ||
import _extends from "@babel/runtime/helpers/esm/extends"; | ||
import { getMutableMap, initRegistry, updateRegistry, watchRegistry } from './registry'; | ||
@@ -6,2 +7,4 @@ let devTools = null; | ||
let isDevToolsInitialized = false; | ||
let isDevToolsPaused = false; | ||
let initialRegistryValue = null; | ||
export function enableInspector(condition = true) { | ||
@@ -14,3 +17,7 @@ isInspectorEnabled = condition; | ||
devTools = ((_window$__REDUX_DEVTO = window.__REDUX_DEVTOOLS_EXTENSION__) == null ? void 0 : _window$__REDUX_DEVTO.connect({ | ||
name: document.title | ||
name: document.title, | ||
features: { | ||
dispatch: false, | ||
test: false | ||
} | ||
})) ?? null; | ||
@@ -20,9 +27,3 @@ | ||
initRegistry(); | ||
devTools.subscribe(event => { | ||
if (!isInspectorEnabled) return; | ||
if (event.type === 'DISPATCH') { | ||
updateRegistry(JSON.parse(event.state)); | ||
} | ||
}); | ||
devTools.subscribe(handleDevToolsEvent); | ||
} | ||
@@ -32,2 +33,55 @@ | ||
} | ||
function handleDevToolsEvent(event) { | ||
if (!isInspectorEnabled) return; | ||
if (event.type === 'DISPATCH') { | ||
switch (event.payload.type) { | ||
case 'JUMP_TO_STATE': | ||
case 'JUMP_TO_ACTION': | ||
applyStateToEntities(event.state); | ||
break; | ||
case 'COMMIT': | ||
devTools.init(getMutableMap()); | ||
break; | ||
case 'ROLLBACK': | ||
const snapshot = applyStateToEntities(event.state); | ||
if (snapshot) { | ||
devTools.init(snapshot); | ||
} | ||
break; | ||
case 'RESET': | ||
if (initialRegistryValue !== null) { | ||
updateRegistry(initialRegistryValue); | ||
devTools.init(initialRegistryValue); | ||
} | ||
break; | ||
case 'PAUSE_RECORDING': | ||
isDevToolsPaused = !isDevToolsPaused; | ||
break; | ||
} | ||
} | ||
} | ||
function applyStateToEntities(state) { | ||
let parsedState; | ||
try { | ||
parsedState = JSON.parse(state); | ||
} catch (err) { | ||
console.error('Ignoring invalid state received from DevTools.'); | ||
} | ||
if (parsedState === undefined) return; | ||
updateRegistry(parsedState); | ||
return parsedState; | ||
} | ||
export function onInit(entity) { | ||
@@ -48,3 +102,3 @@ if (entity.name.charAt(0) === '_') return; | ||
if (isDevToolsInitialized && isInspectorEnabled) { | ||
if (isDevToolsInitialized && isInspectorEnabled && !isDevToolsPaused) { | ||
devTools.send({ | ||
@@ -65,7 +119,10 @@ type: `${entity.name}:@@LAZY_INIT` | ||
isDevToolsInitialized = true; | ||
initialRegistryValue = _extends({}, mutableMap); | ||
} | ||
devTools.send({ | ||
type: `${entity.name}:${alias}` | ||
}, mutableMap); | ||
if (!isDevToolsPaused) { | ||
devTools.send({ | ||
type: `${entity.name}:${alias}` | ||
}, mutableMap); | ||
} | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
@@ -9,2 +11,4 @@ exports.enableInspector = enableInspector; | ||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); | ||
var _registry = require("./registry"); | ||
@@ -16,2 +20,4 @@ | ||
var isDevToolsInitialized = false; | ||
var isDevToolsPaused = false; | ||
var initialRegistryValue = null; | ||
@@ -30,3 +36,7 @@ function enableInspector(condition) { | ||
devTools = (_window$__REDUX_DEVTO = (_window$__REDUX_DEVTO2 = window.__REDUX_DEVTOOLS_EXTENSION__) == null ? void 0 : _window$__REDUX_DEVTO2.connect({ | ||
name: document.title | ||
name: document.title, | ||
features: { | ||
dispatch: false, | ||
test: false | ||
} | ||
})) != null ? _window$__REDUX_DEVTO : null; | ||
@@ -36,9 +46,3 @@ | ||
(0, _registry.initRegistry)(); | ||
devTools.subscribe(function (event) { | ||
if (!isInspectorEnabled) return; | ||
if (event.type === 'DISPATCH') { | ||
(0, _registry.updateRegistry)(JSON.parse(event.state)); | ||
} | ||
}); | ||
devTools.subscribe(handleDevToolsEvent); | ||
} | ||
@@ -49,2 +53,54 @@ | ||
function handleDevToolsEvent(event) { | ||
if (!isInspectorEnabled) return; | ||
if (event.type === 'DISPATCH') { | ||
switch (event.payload.type) { | ||
case 'JUMP_TO_STATE': | ||
case 'JUMP_TO_ACTION': | ||
applyStateToEntities(event.state); | ||
break; | ||
case 'COMMIT': | ||
devTools.init((0, _registry.getMutableMap)()); | ||
break; | ||
case 'ROLLBACK': | ||
var snapshot = applyStateToEntities(event.state); | ||
if (snapshot) { | ||
devTools.init(snapshot); | ||
} | ||
break; | ||
case 'RESET': | ||
if (initialRegistryValue !== null) { | ||
(0, _registry.updateRegistry)(initialRegistryValue); | ||
devTools.init(initialRegistryValue); | ||
} | ||
break; | ||
case 'PAUSE_RECORDING': | ||
isDevToolsPaused = !isDevToolsPaused; | ||
break; | ||
} | ||
} | ||
} | ||
function applyStateToEntities(state) { | ||
var parsedState; | ||
try { | ||
parsedState = JSON.parse(state); | ||
} catch (err) { | ||
console.error('Ignoring invalid state received from DevTools.'); | ||
} | ||
if (parsedState === undefined) return; | ||
(0, _registry.updateRegistry)(parsedState); | ||
return parsedState; | ||
} | ||
function onInit(entity) { | ||
@@ -65,3 +121,3 @@ if (entity.name.charAt(0) === '_') return; | ||
if (isDevToolsInitialized && isInspectorEnabled) { | ||
if (isDevToolsInitialized && isInspectorEnabled && !isDevToolsPaused) { | ||
devTools.send({ | ||
@@ -83,7 +139,10 @@ type: entity.name + ":@@LAZY_INIT" | ||
isDevToolsInitialized = true; | ||
initialRegistryValue = (0, _extends2.default)({}, mutableMap); | ||
} | ||
devTools.send({ | ||
type: entity.name + ":" + alias | ||
}, mutableMap); | ||
if (!isDevToolsPaused) { | ||
devTools.send({ | ||
type: entity.name + ":" + alias | ||
}, mutableMap); | ||
} | ||
} |
{ | ||
"name": "simpler-state", | ||
"version": "2.0.0-pre.5", | ||
"version": "2.0.0-pre.6", | ||
"description": "The simplest app state management for React", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
import type { MutableMap } from './registry'; | ||
import type { Entity } from '../core/entity'; | ||
interface DevToolsEvent { | ||
type: string; | ||
declare type DevToolsEvent = { | ||
type: 'DISPATCH'; | ||
payload: { | ||
type: string; | ||
[P: string]: any; | ||
}; | ||
state: string; | ||
} | ||
} | { | ||
type: 'ACTION'; | ||
payload: string; | ||
}; | ||
interface DevToolsConnection { | ||
@@ -19,2 +26,3 @@ init(state: MutableMap): void; | ||
name?: string; | ||
features?: Record<string, any>; | ||
}): DevToolsConnection; | ||
@@ -21,0 +29,0 @@ }; |
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
38117
884