Comparing version 2.5.1 to 2.5.2
@@ -5,3 +5,2 @@ "use strict"; | ||
var global_state_1 = require("../global-state"); | ||
var utils_1 = require("../utils"); | ||
var native = Map.prototype; | ||
@@ -56,9 +55,7 @@ var masterKey = Symbol("Map master key"); | ||
target[getter] = function (key) { | ||
var result = native[getter].apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.registerParentInfo(target, key, result); | ||
} | ||
result = proxyValue(this, key, result); | ||
var value = native[getter].apply(this, arguments); | ||
global_state_1.globalState.event.emit("get", { target: target, key: key, value: value }); | ||
value = proxyValue(this, key, value); | ||
bindCurrentReaction(this, key); | ||
return result; | ||
return value; | ||
}; | ||
@@ -122,5 +119,3 @@ }; | ||
var result = native.set.apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.printDiff(target, key, oldValue, value); | ||
} | ||
global_state_1.globalState.event.emit("set", { target: target, key: key, oldValue: oldValue, value: value }); | ||
if (oldValue !== value) { | ||
@@ -136,5 +131,3 @@ queueRunReactions(this, key); | ||
var result = native.delete.apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.printDelete(target, key); | ||
} | ||
global_state_1.globalState.event.emit("deleteProperty", { target: target, key: key }); | ||
if (has) { | ||
@@ -141,0 +134,0 @@ queueRunReactions(this, key); |
@@ -5,3 +5,2 @@ "use strict"; | ||
var global_state_1 = require("../global-state"); | ||
var utils_1 = require("../utils"); | ||
var native = Set.prototype; | ||
@@ -57,5 +56,3 @@ var masterValue = Symbol("Set master value"); | ||
var result = native[getter].apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.registerParentInfo(target, null, result); | ||
} | ||
global_state_1.globalState.event.emit("get", { target: target, key: null, value: result }); | ||
result = proxyValue(this, value, result); | ||
@@ -122,5 +119,3 @@ bindCurrentReaction(this, value); | ||
var result = native.add.apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.printCustom(target, "add", value); | ||
} | ||
global_state_1.globalState.event.emit("set", { target: target, key: null, value: value, oldValue: null }); | ||
if (!has) { | ||
@@ -136,5 +131,3 @@ queueRunReactions(this, value); | ||
var result = native.delete.apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.printCustom(target, "delete", value); | ||
} | ||
global_state_1.globalState.event.emit("deleteProperty", { target: target, key: null }); | ||
if (has) { | ||
@@ -141,0 +134,0 @@ queueRunReactions(this, value); |
@@ -5,3 +5,2 @@ "use strict"; | ||
var global_state_1 = require("../global-state"); | ||
var utils_1 = require("../utils"); | ||
var native = WeakMap.prototype; | ||
@@ -54,9 +53,7 @@ var getters = ["has", "get"]; | ||
target[getter] = function (key) { | ||
var result = native[getter].apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.registerParentInfo(target, key, result); | ||
} | ||
result = proxyValue(this, key, result); | ||
var value = native[getter].apply(this, arguments); | ||
global_state_1.globalState.event.emit("get", { target: target, key: null, value: value }); | ||
value = proxyValue(this, key, value); | ||
bindCurrentReaction(this, key); | ||
return result; | ||
return value; | ||
}; | ||
@@ -87,5 +84,3 @@ }; | ||
var result = native.set.apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.printDiff(target, key, oldValue, value); | ||
} | ||
global_state_1.globalState.event.emit("set", { target: target, key: key, oldValue: oldValue, value: value }); | ||
if (oldValue !== value) { | ||
@@ -100,5 +95,3 @@ queueRunReactions(this, key); | ||
var result = native.delete.apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.printDelete(target, key); | ||
} | ||
global_state_1.globalState.event.emit("deleteProperty", { target: target, key: key }); | ||
if (has) { | ||
@@ -105,0 +98,0 @@ queueRunReactions(this, key); |
@@ -5,3 +5,2 @@ "use strict"; | ||
var global_state_1 = require("../global-state"); | ||
var utils_1 = require("../utils"); | ||
var native = WeakSet.prototype; | ||
@@ -55,5 +54,3 @@ var getters = ["has"]; | ||
var result = native[getter].apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.registerParentInfo(target, null, result); | ||
} | ||
global_state_1.globalState.event.emit("get", { target: target, key: null, value: result }); | ||
result = proxyValue(this, value, result); | ||
@@ -87,5 +84,3 @@ bindCurrentReaction(this, value); | ||
var result = native.add.apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.printCustom(target, "add", value); | ||
} | ||
global_state_1.globalState.event.emit("set", { target: target, key: null, value: value, oldValue: null }); | ||
if (!has) { | ||
@@ -100,5 +95,3 @@ queueRunReactions(this, value); | ||
var result = native.delete.apply(this, arguments); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.printCustom(target, "delete", value); | ||
} | ||
global_state_1.globalState.event.emit("deleteProperty", { target: target, key: null }); | ||
if (has) { | ||
@@ -105,0 +98,0 @@ queueRunReactions(this, value); |
@@ -9,2 +9,3 @@ "use strict"; | ||
exports.Atom = atom_1.Atom; | ||
require("./debug"); | ||
var global_state_1 = require("./global-state"); | ||
@@ -11,0 +12,0 @@ exports.globalState = global_state_1.globalState; |
@@ -61,5 +61,2 @@ "use strict"; | ||
var value = Reflect.get(target, key, receiver); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.registerParentInfo(target, key, value); | ||
} | ||
// 如果 key 是 $raw,或者在 Action 中,直接返回原始对象 | ||
@@ -87,5 +84,2 @@ if (key === "$raw") { | ||
if (key === "length" || value !== oldValue) { | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.printDiff(target, key, oldValue, value); | ||
} | ||
queueRunReactions(target, key); | ||
@@ -99,5 +93,2 @@ } | ||
var result = Reflect.deleteProperty(target, key); | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.printDelete(target, key); | ||
} | ||
if (hasKey) { | ||
@@ -246,5 +237,3 @@ queueRunReactions(target, key); | ||
global_state_1.globalState.batchDeep++; | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.debugInAction(global_state_1.globalState.currentDebugName); | ||
} | ||
global_state_1.globalState.event.emit("startBatch", null); | ||
} | ||
@@ -259,5 +248,3 @@ exports.startBatch = startBatch; | ||
} | ||
if (global_state_1.globalState.useDebug) { | ||
utils_1.debugOutAction(); | ||
} | ||
global_state_1.globalState.event.emit("endBatch", null); | ||
} | ||
@@ -300,5 +287,3 @@ exports.endBatch = endBatch; | ||
function runInAction(fn, debugName) { | ||
if (global_state_1.globalState.useDebug) { | ||
global_state_1.globalState.currentDebugName = debugName || null; | ||
} | ||
global_state_1.globalState.event.emit("runInAction", { debugName: debugName }); | ||
startBatch(); | ||
@@ -305,0 +290,0 @@ try { |
"use strict"; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var cloneDeep = require("lodash.clonedeep"); | ||
var global_state_1 = require("./global-state"); | ||
@@ -22,17 +19,2 @@ /** | ||
/** | ||
* redux thunk | ||
*/ | ||
exports.createThunkMiddleware = function (_ref) { | ||
var dispatch = _ref.dispatch, | ||
getState = _ref.getState; | ||
return function (next) { | ||
return function (action) { | ||
if (typeof action === "function") { | ||
return action(dispatch, getState); | ||
} | ||
return next(action); | ||
}; | ||
}; | ||
}; | ||
/** | ||
* 空函数 | ||
@@ -91,145 +73,2 @@ */ | ||
/** | ||
* 注册 parentInfo | ||
*/ | ||
function registerParentInfo(target, key, value) { | ||
if (value !== null && (typeof value === "undefined" ? "undefined" : _typeof(value)) === "object") { | ||
global_state_1.globalState.parentInfo.set(value, { | ||
parent: target, | ||
key: key | ||
}); | ||
} | ||
} | ||
exports.registerParentInfo = registerParentInfo; | ||
/** | ||
* debug 入栈 action | ||
*/ | ||
function debugInAction(actionName) { | ||
var debugOutputBundleAction = { | ||
name: actionName, | ||
changeList: [], | ||
type: "action" | ||
}; | ||
global_state_1.globalState.debugOutputActionMapBatchDeep.set(global_state_1.globalState.batchDeep, debugOutputBundleAction); | ||
global_state_1.globalState.currentDebugOutputAction = debugOutputBundleAction; | ||
// 如果深度等于 1,生成唯一 id 给这个 action | ||
if (global_state_1.globalState.batchDeep === 1) { | ||
debugOutputBundleAction.id = getUniqueId(); | ||
global_state_1.globalState.currentDebugId = debugOutputBundleAction.id; | ||
} | ||
// 如果当前深度大于 1,就作为加到父级的 changeList | ||
if (global_state_1.globalState.batchDeep > 1) { | ||
global_state_1.globalState.debugOutputActionMapBatchDeep.get(global_state_1.globalState.batchDeep - 1).changeList.push({ | ||
type: "action", | ||
action: debugOutputBundleAction | ||
}); | ||
} | ||
} | ||
exports.debugInAction = debugInAction; | ||
/** | ||
* debug 出栈 action | ||
*/ | ||
function debugOutAction() { | ||
// 每次出栈,都要更新 currentDebugOutputAction | ||
global_state_1.globalState.currentDebugOutputAction = global_state_1.globalState.debugOutputActionMapBatchDeep.get(global_state_1.globalState.batchDeep); | ||
if (!inAction()) { | ||
var cloneDebugInfo = null; | ||
try { | ||
cloneDebugInfo = JSON.parse(JSON.stringify(global_state_1.globalState.debugOutputActionMapBatchDeep.get(1))); | ||
} catch (error) { | ||
return; | ||
} | ||
// 如果完全出队列了,把存储的 debug 信息输出给 debug 事件,并清空 debug 信息 | ||
global_state_1.globalState.event.emit("debug", cloneDebugInfo); | ||
global_state_1.globalState.currentDebugOutputAction = null; | ||
global_state_1.globalState.debugOutputActionMapBatchDeep.clear(); | ||
// 此时不能清空 globalState.currentDebugId = null,因为后续要传给 callback | ||
// action 与 debug 调用顺序:startBatch -> debugInAction -> ...multiple nested startBatch and endBatch -> debugOutAction -> reaction -> observe | ||
} | ||
} | ||
exports.debugOutAction = debugOutAction; | ||
/** | ||
* 获取对象路径 | ||
*/ | ||
function getCallStack(target) { | ||
var callStack = []; | ||
if (!global_state_1.globalState.parentInfo.has(target)) { | ||
callStack.unshift(target.constructor.name); | ||
} else { | ||
var currentTarget = target; | ||
var runCount = 0; | ||
while (global_state_1.globalState.parentInfo.has(currentTarget)) { | ||
var parentInfo = global_state_1.globalState.parentInfo.get(currentTarget); | ||
// 添加调用队列 | ||
callStack.unshift(parentInfo.key); | ||
// 如果父级没有父级了,给调用队列添加父级名称 | ||
if (!global_state_1.globalState.parentInfo.has(parentInfo.parent)) { | ||
callStack.unshift(parentInfo.parent.constructor.name); | ||
} | ||
currentTarget = parentInfo.parent; | ||
runCount++; | ||
if (runCount >= 50) { | ||
break; | ||
} | ||
} | ||
} | ||
return callStack; | ||
} | ||
/** | ||
* 打印 diff 路径 | ||
*/ | ||
function printDiff(target, key, oldValue, value) { | ||
var callStack = getCallStack(target); | ||
reportChange({ | ||
type: "change", | ||
callStack: callStack, | ||
oldValue: cloneDeep(oldValue), | ||
key: key, | ||
value: cloneDeep(value) | ||
}); | ||
} | ||
exports.printDiff = printDiff; | ||
/** | ||
* 打印删除路径 | ||
*/ | ||
function printDelete(target, key) { | ||
var callStack = getCallStack(target); | ||
reportChange({ | ||
type: "delete", | ||
callStack: callStack, | ||
key: key | ||
}); | ||
} | ||
exports.printDelete = printDelete; | ||
/** | ||
* 自定义打印 | ||
*/ | ||
function printCustom(target) { | ||
for (var _len = arguments.length, customMessage = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
customMessage[_key - 1] = arguments[_key]; | ||
} | ||
var callStack = getCallStack(target); | ||
reportChange({ | ||
type: "custom", | ||
callStack: callStack, | ||
customMessage: customMessage | ||
}); | ||
} | ||
exports.printCustom = printCustom; | ||
/** | ||
* 通知修改 | ||
*/ | ||
function reportChange(change) { | ||
if (global_state_1.globalState.currentDebugOutputAction) { | ||
global_state_1.globalState.currentDebugOutputAction.changeList.push(change); | ||
} else { | ||
global_state_1.globalState.event.emit("debug", { | ||
id: getUniqueId(), | ||
name: null, | ||
changeList: [change], | ||
type: "isolated" | ||
}); | ||
} | ||
} | ||
/** | ||
* 设置处于严格模式 | ||
@@ -236,0 +75,0 @@ */ |
{ | ||
"name": "dob", | ||
"version": "2.5.1", | ||
"version": "2.5.2", | ||
"description": "", | ||
@@ -57,4 +57,4 @@ "main": "built/src/index.js", | ||
"dependencies": { | ||
"lodash.clonedeep": "^4.5.0", | ||
"dependency-inject": "^1.1.0" | ||
"dependency-inject": "^1.1.1", | ||
"lodash.clonedeep": "^4.5.0" | ||
}, | ||
@@ -66,3 +66,3 @@ "pre-commit": [ | ||
"entrys": [ | ||
"demos/action/index.tsx" | ||
"demos/debug/index" | ||
], | ||
@@ -83,2 +83,2 @@ "rules": [ | ||
} | ||
} | ||
} |
@@ -77,3 +77,3 @@ # Dob · [](https://travis-ci.org/dobjs/dob) [](https://www.npmjs.com/package/dob) [](https://codecov.io/github/dobjs/dob) | ||
Dob is available as the `dob` package on [npm](https://www.npmjs.com/package/dob). It is also available on a [CDN](https://unpkg.com/dob@2.2.5/built/bundle.js). | ||
Dob is available as the `dob` package on [npm](https://www.npmjs.com/package/dob). It is also available on a [CDN](https://unpkg.com/dob/built/bundle.js). | ||
@@ -96,19 +96,10 @@ ```bash | ||
## Combination | ||
## Ecosystem | ||
### Combining [dob-react](https://github.com/ascoders/dob-react) | ||
- [dob-react](https://github.com/dobjs/dob-react) - Connect dob to react! Here is a basic [demo](https://jsfiddle.net/yp90Lep9/21/), and here is a [demo](https://jsfiddle.net/g19ehhgu/11/) with fractal. [Quick start](./docs/dob-react.md). | ||
- [dob-react-devtools](https://github.com/dobjs/dob-react-devtools) - Devtools for dob-react, with action and ui two way binding. | ||
- [dob-redux](https://github.com/dobjs/dob-redux) - You can use both dob and Redux by using it! Enjoy the type and convenience of dob, and the ecology of Redux. | ||
- [dob-refect](https://github.com/dobjs/dob-refetch) - Auto fetch, away from the trouble of `componentDidUpdate`. | ||
[Quick start](./docs/dob-react.md) | ||
Here is a basic [demo](https://jsfiddle.net/yp90Lep9/21/), and here is a [demo](https://jsfiddle.net/g19ehhgu/11/) with fractal. | ||
### Combining [react-redux](https://github.com/reactjs/react-redux) | ||
Here is a basic [demo](https://jsfiddle.net/56saqqvw/8/) | ||
- [createReduxStore](./docs/createReduxStore.md) | ||
- [task](./docs/task.md) | ||
You can clone this project, and run `npm start` to see [redux todoMVC demo](./src/demos/todo-mvc). | ||
## Debug in console | ||
@@ -115,0 +106,0 @@ |
import { globalState } from "../global-state" | ||
import { printDelete, printDiff, registerParentInfo } from "../utils" | ||
@@ -31,13 +30,11 @@ const native: Map<any, any> & { | ||
target[getter] = function (key: string) { | ||
let result = native[getter].apply(this, arguments) | ||
let value = native[getter].apply(this, arguments) | ||
if (globalState.useDebug) { | ||
registerParentInfo(target, key, result) | ||
} | ||
globalState.event.emit("get", { target, key, value }) | ||
result = proxyValue(this, key, result) | ||
value = proxyValue(this, key, value) | ||
bindCurrentReaction(this, key) | ||
return result | ||
return value | ||
} | ||
@@ -59,5 +56,3 @@ } | ||
if (globalState.useDebug) { | ||
printDiff(target, key, oldValue, value) | ||
} | ||
globalState.event.emit("set", { target, key, oldValue, value }) | ||
@@ -76,5 +71,3 @@ if (oldValue !== value) { | ||
if (globalState.useDebug) { | ||
printDelete(target, key) | ||
} | ||
globalState.event.emit("deleteProperty", { target, key }) | ||
@@ -81,0 +74,0 @@ if (has) { |
import { globalState } from "../global-state" | ||
import { printCustom, printDelete, printDiff, registerParentInfo } from "../utils" | ||
@@ -33,5 +32,3 @@ const native: Set<any> & { | ||
if (globalState.useDebug) { | ||
registerParentInfo(target, null, result) | ||
} | ||
globalState.event.emit("get", { target, key: null, value: result }) | ||
@@ -59,5 +56,3 @@ result = proxyValue(this, value, result) | ||
if (globalState.useDebug) { | ||
printCustom(target, "add", value) | ||
} | ||
globalState.event.emit("set", { target, key: null, value, oldValue: null }) | ||
@@ -76,5 +71,3 @@ if (!has) { | ||
if (globalState.useDebug) { | ||
printCustom(target, "delete", value) | ||
} | ||
globalState.event.emit("deleteProperty", { target, key: null }) | ||
@@ -81,0 +74,0 @@ if (has) { |
import { globalState } from "../global-state" | ||
import { printDelete, printDiff, registerParentInfo } from "../utils" | ||
@@ -29,13 +28,11 @@ const native: WeakMap<any, any> & { | ||
target[getter] = function (key: string) { | ||
let result = native[getter].apply(this, arguments) | ||
let value = native[getter].apply(this, arguments) | ||
if (globalState.useDebug) { | ||
registerParentInfo(target, key, result) | ||
} | ||
globalState.event.emit("get", { target, key: null, value }) | ||
result = proxyValue(this, key, result) | ||
value = proxyValue(this, key, value) | ||
bindCurrentReaction(this, key) | ||
return result | ||
return value | ||
} | ||
@@ -49,5 +46,3 @@ } | ||
if (globalState.useDebug) { | ||
printDiff(target, key, oldValue, value) | ||
} | ||
globalState.event.emit("set", { target, key, oldValue, value }) | ||
@@ -65,5 +60,3 @@ if (oldValue !== value) { | ||
if (globalState.useDebug) { | ||
printDelete(target, key) | ||
} | ||
globalState.event.emit("deleteProperty", { target, key }) | ||
@@ -70,0 +63,0 @@ if (has) { |
import { globalState } from "../global-state" | ||
import { printCustom, printDelete, printDiff, registerParentInfo } from "../utils" | ||
@@ -31,5 +30,3 @@ const native: WeakSet<any> & { | ||
if (globalState.useDebug) { | ||
registerParentInfo(target, null, result) | ||
} | ||
globalState.event.emit("get", { target, key: null, value: result }) | ||
@@ -49,5 +46,3 @@ result = proxyValue(this, value, result) | ||
if (globalState.useDebug) { | ||
printCustom(target, "add", value) | ||
} | ||
globalState.event.emit("set", { target, key: null, value, oldValue: null }) | ||
@@ -65,5 +60,3 @@ if (!has) { | ||
if (globalState.useDebug) { | ||
printCustom(target, "delete", value) | ||
} | ||
globalState.event.emit("deleteProperty", { target, key: null }) | ||
@@ -70,0 +63,0 @@ if (has) { |
@@ -36,2 +36,4 @@ import { IDebugInfo } from "../global-state" | ||
}) => void): void | ||
public on(eventType: "startBatch" | "endBatch", callback: () => void): void | ||
public on(eventType: "runInAction", callback: (debugName?: string) => void): void | ||
public on(eventType: EventType, callback: ICallback): void { | ||
@@ -38,0 +40,0 @@ const event: IEvent = { |
import { inject, injectFactory as combineStores } from "dependency-inject" | ||
import { Atom } from "./atom" | ||
import "./debug" | ||
import { globalState, IDebugInfo } from "./global-state" | ||
@@ -4,0 +5,0 @@ import { Action, isObservable, observable, observe, Static } from "./observer" |
import builtIns from "./built-ins" | ||
import { globalState } from "./global-state" | ||
import { Reaction } from "./reaction" | ||
import { debugInAction, debugOutAction, Func, getBinder, inAction, isPrimitive, printDelete, printDiff, registerParentInfo } from "./utils" | ||
import { Func, getBinder, inAction, isPrimitive } from "./utils" | ||
@@ -60,6 +60,2 @@ const MAX_RUN_COUNT = 1000 | ||
if (globalState.useDebug) { | ||
registerParentInfo(target, key, value) | ||
} | ||
// 如果 key 是 $raw,或者在 Action 中,直接返回原始对象 | ||
@@ -96,6 +92,2 @@ if (key === "$raw") { | ||
if (key === "length" || value !== oldValue) { | ||
if (globalState.useDebug) { | ||
printDiff(target, key, oldValue, value) | ||
} | ||
queueRunReactions<T>(target, key) | ||
@@ -114,6 +106,2 @@ } | ||
if (globalState.useDebug) { | ||
printDelete(target, key) | ||
} | ||
if (hasKey) { | ||
@@ -279,5 +267,3 @@ queueRunReactions(target, key) | ||
if (globalState.useDebug) { | ||
debugInAction(globalState.currentDebugName) | ||
} | ||
globalState.event.emit("startBatch", null) | ||
} | ||
@@ -293,5 +279,3 @@ | ||
if (globalState.useDebug) { | ||
debugOutAction() | ||
} | ||
globalState.event.emit("endBatch", null) | ||
} | ||
@@ -338,5 +322,3 @@ | ||
function runInAction(fn: () => any | Promise<any>, debugName?: string) { | ||
if (globalState.useDebug) { | ||
globalState.currentDebugName = debugName || null | ||
} | ||
globalState.event.emit("runInAction", { debugName }) | ||
@@ -343,0 +325,0 @@ startBatch() |
176
src/utils.ts
@@ -7,10 +7,2 @@ import * as cloneDeep from "lodash.clonedeep" | ||
export declare interface IObjectType<T> { | ||
new(): T | ||
} | ||
export declare type ICombineActions<T> = { | ||
[P in keyof T]?: IObjectType<T[P]> | ||
} | ||
/** | ||
@@ -38,13 +30,2 @@ * 某个对象的所有绑定 | ||
/** | ||
* redux thunk | ||
*/ | ||
export const createThunkMiddleware = ({ dispatch, getState }: any) => (next: any) => (action: any) => { | ||
if (typeof action === "function") { | ||
return action(dispatch, getState) | ||
} | ||
return next(action) | ||
} | ||
/** | ||
* 空函数 | ||
@@ -108,159 +89,2 @@ */ | ||
/** | ||
* 注册 parentInfo | ||
*/ | ||
export function registerParentInfo(target: object, key: PropertyKey, value: any) { | ||
if (value !== null && typeof value === "object") { | ||
globalState.parentInfo.set(value, { | ||
parent: target, | ||
key | ||
}) | ||
} | ||
} | ||
/** | ||
* debug 入栈 action | ||
*/ | ||
export function debugInAction(actionName: string) { | ||
const debugOutputBundleAction: IDebugInfo = { | ||
name: actionName, | ||
changeList: [], | ||
type: "action" | ||
} | ||
globalState.debugOutputActionMapBatchDeep.set(globalState.batchDeep, debugOutputBundleAction) | ||
globalState.currentDebugOutputAction = debugOutputBundleAction | ||
// 如果深度等于 1,生成唯一 id 给这个 action | ||
if (globalState.batchDeep === 1) { | ||
debugOutputBundleAction.id = getUniqueId() | ||
globalState.currentDebugId = debugOutputBundleAction.id | ||
} | ||
// 如果当前深度大于 1,就作为加到父级的 changeList | ||
if (globalState.batchDeep > 1) { | ||
globalState.debugOutputActionMapBatchDeep.get(globalState.batchDeep - 1).changeList.push({ | ||
type: "action", | ||
action: debugOutputBundleAction | ||
}) | ||
} | ||
} | ||
/** | ||
* debug 出栈 action | ||
*/ | ||
export function debugOutAction() { | ||
// 每次出栈,都要更新 currentDebugOutputAction | ||
globalState.currentDebugOutputAction = globalState.debugOutputActionMapBatchDeep.get(globalState.batchDeep) | ||
if (!inAction()) { | ||
let cloneDebugInfo: IDebugInfo = null | ||
try { | ||
cloneDebugInfo = JSON.parse(JSON.stringify(globalState.debugOutputActionMapBatchDeep.get(1))) | ||
} catch (error) { | ||
return | ||
} | ||
// 如果完全出队列了,把存储的 debug 信息输出给 debug 事件,并清空 debug 信息 | ||
globalState.event.emit("debug", cloneDebugInfo) | ||
globalState.currentDebugOutputAction = null | ||
globalState.debugOutputActionMapBatchDeep.clear() | ||
// 此时不能清空 globalState.currentDebugId = null,因为后续要传给 callback | ||
// action 与 debug 调用顺序:startBatch -> debugInAction -> ...multiple nested startBatch and endBatch -> debugOutAction -> reaction -> observe | ||
} | ||
} | ||
/** | ||
* 获取对象路径 | ||
*/ | ||
function getCallStack(target: object) { | ||
const callStack: PropertyKey[] = [] | ||
if (!globalState.parentInfo.has(target)) { // 当前访问的对象就是顶层 | ||
callStack.unshift(target.constructor.name) | ||
} else { | ||
let currentTarget: object = target | ||
let runCount = 0 | ||
while (globalState.parentInfo.has(currentTarget)) { | ||
const parentInfo = globalState.parentInfo.get(currentTarget) | ||
// 添加调用队列 | ||
callStack.unshift(parentInfo.key) | ||
// 如果父级没有父级了,给调用队列添加父级名称 | ||
if (!globalState.parentInfo.has(parentInfo.parent)) { | ||
callStack.unshift(parentInfo.parent.constructor.name) | ||
} | ||
currentTarget = parentInfo.parent | ||
runCount++ | ||
if (runCount >= 50) { | ||
break | ||
} | ||
} | ||
} | ||
return callStack | ||
} | ||
/** | ||
* 打印 diff 路径 | ||
*/ | ||
export function printDiff(target: object, key?: PropertyKey, oldValue?: any, value?: any) { | ||
const callStack = getCallStack(target) | ||
reportChange({ | ||
type: "change", | ||
callStack, | ||
oldValue: cloneDeep(oldValue), | ||
key, | ||
value: cloneDeep(value) | ||
}) | ||
} | ||
/** | ||
* 打印删除路径 | ||
*/ | ||
export function printDelete(target: object, key?: PropertyKey) { | ||
const callStack = getCallStack(target) | ||
reportChange({ | ||
type: "delete", | ||
callStack, | ||
key, | ||
}) | ||
} | ||
/** | ||
* 自定义打印 | ||
*/ | ||
export function printCustom(target: object, ...customMessage: any[]) { | ||
const callStack = getCallStack(target) | ||
reportChange({ | ||
type: "custom", | ||
callStack, | ||
customMessage, | ||
}) | ||
} | ||
/** | ||
* 通知修改 | ||
*/ | ||
function reportChange(change: IDebugChange) { | ||
if (globalState.currentDebugOutputAction) { | ||
globalState.currentDebugOutputAction.changeList.push(change) | ||
} else { // 脱离了 action 事件循环的孤立改动 | ||
globalState.event.emit("debug", { | ||
id: getUniqueId(), | ||
name: null, | ||
changeList: [change], | ||
type: "isolated" | ||
}) | ||
} | ||
} | ||
/** | ||
* 设置处于严格模式 | ||
@@ -267,0 +91,0 @@ */ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
321260
85
4974
129
Updateddependency-inject@^1.1.1