Skip to content

Commit

Permalink
Add stack.meta support (#265)
Browse files Browse the repository at this point in the history
* Add stack.meta support

* Add note in the docs
  • Loading branch information
AlexandrHoroshih authored Jan 7, 2023
1 parent 284f9c9 commit ee370ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/debug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type LogContext = {
line: number;
column: number;
};
stackMeta: Record<string, unknown>;
trace: {
node: Node;
name: string | null;
Expand All @@ -37,6 +38,7 @@ type LogContext = {
line: number;
column: number;
};
stackMeta: Record<string, unknown>;
}[];
};

Expand Down Expand Up @@ -156,6 +158,7 @@ function watch(unit: Unit<any>, config: Config) {
value,
name: getName(unit),
loc: getLoc(unit),
stackMeta: getStackMeta(stack),
trace: config.trace ? collectTrace(stack) : [],
};

Expand Down Expand Up @@ -189,6 +192,7 @@ function collectTrace(stack: Stack): Trace {
name: getName(node),
loc: getLoc(node),
kind: getType(node),
stackMeta: getStackMeta(parent),
};

trace.push(entry);
Expand Down Expand Up @@ -218,6 +222,8 @@ function watchStoreInit(store: Store<any>, config: Config) {
loc: getLoc(store),
// nothing to trace for store.getState() - it is one-step call
trace: [],
// no stackMeta for initial state
stackMeta: {},
};

config.handler(context);
Expand Down Expand Up @@ -247,6 +253,8 @@ function watchStoreInitInScope(store: Store<any>, config: Config, scope: Scope)
loc: getLoc(store),
// nothing to trace for scope.getState(store) - it is one-step call
trace: [],
// no stackMeta for initial state
stackMeta: {},
};

config.handler(context);
Expand Down Expand Up @@ -511,3 +519,9 @@ function getNode(node: Node | { graphite: Node } | Unit<any>): Node {

return actualNode;
}

function getStackMeta(stack: Stack) {
const meta = (stack as any).meta || {};

return meta as Record<string, unknown>;
}
11 changes: 8 additions & 3 deletions src/debug/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,13 @@ debug($count);
// "[store] (scope: scope_1337) $count 1337",
```

### Custom log handler
## Customization

You can provide custom log handler in the config. It can be useful, if `console.log` somehow doesn't fit your use-case: e.g. you want advanced info from `patronum/debug` to built your own dev-tools, debug especially hard case, etc.
The `patronum/debug` package extracts quite a lot of low-level effector data in a universal format that may be useful for other dev tools or monitoring tools, so there is a special API to add your own way of handling this data.

### Custom handler

You can provide custom log handler in the config. It can be useful, if `console.info` somehow doesn't fit your use-case: e.g. you want advanced info from `patronum/debug` to built your own dev-tools, debug especially hard case, etc.

Handler is called for each update with context object like this:

Expand All @@ -196,10 +200,11 @@ Common fields:
- **kind** - `string` - node's kind for convenience. It can be unit's kind (e.g. `store` or `event`) or operation kind (e.g. `sample`, `split`, etc).
- **value** - `unknown` - value of the update.
- **loc** - `{ file?: string; line: number; column: number; }` - location in the source code, if known
- **stackMeta** - `{ Record<string, unknown> }` - effector's internal stack metadata, available if provided. For example, effect calls provide an `fxID` that is stable between `fx` and `fx.finally` updates - this allows you to associate a `fx.finally` update with a particular `fx` call (available since **[email protected]**)

Special field if `trace: true` provided:

- **trace** - `Array<{ node: Node; name: string | null; kind: string; value: unknown; loc?: Loc }>` - trace of updates.
- **trace** - `Array<{ node: Node; name: string | null; kind: string; value: unknown; loc?: Loc; stackMeta?: Record<string, unknown> }>` - trace of updates.

The `trace` array is always empty (i.e. trace is not collected), if `debug`'s config does not have `trace: true`.

Expand Down

1 comment on commit ee370ab

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚛 size-compare report

Comparing 284f9c98...ee370abd

File +/- Base Current +/- gzip Base gzip Current gzip
dist/and/index.cjs = 397 B 397 B = 251 B 251 B
dist/and/index.js = 413 B 413 B = 245 B 245 B
dist/babel-preset.cjs = 434 B 434 B = 249 B 249 B
dist/combine-events/index.cjs = 2.46 kB 2.46 kB = 840 B 840 B
dist/combine-events/index.js = 3.95 kB 3.95 kB = 1.35 kB 1.35 kB
dist/condition/index.cjs = 1.37 kB 1.37 kB = 505 B 505 B
dist/condition/index.js = 1.29 kB 1.29 kB = 461 B 461 B
dist/debounce/index.cjs = 3.96 kB 3.96 kB = 1.29 kB 1.29 kB
dist/debounce/index.js = 3.76 kB 3.76 kB = 1.26 kB 1.26 kB
dist/debug/index.cjs +2.57% 10.7 kB 11 kB +1.83% 3.01 kB 3.06 kB
dist/debug/index.js +2.14% 12.8 kB 13 kB +1.46% 3.64 kB 3.69 kB
dist/delay/index.cjs = 1.76 kB 1.76 kB = 708 B 708 B
dist/delay/index.js = 1.81 kB 1.81 kB = 702 B 702 B
dist/either/index.cjs = 600 B 600 B = 334 B 334 B
dist/either/index.js = 477 B 477 B = 270 B 270 B
dist/empty/index.cjs = 175 B 175 B = 151 B 151 B
dist/empty/index.js = 79 B 79 B = 89 B 89 B
dist/equals/index.cjs = 336 B 336 B = 249 B 249 B
dist/equals/index.js = 221 B 221 B = 179 B 179 B
dist/every/index.cjs = 1.22 kB 1.22 kB = 513 B 513 B
dist/every/index.js = 1.08 kB 1.08 kB = 442 B 442 B
dist/format/index.cjs = 642 B 642 B = 366 B 366 B
dist/format/index.js = 687 B 687 B = 368 B 368 B
dist/in-flight/index.cjs = 641 B 641 B = 357 B 357 B
dist/in-flight/index.js = 546 B 546 B = 305 B 305 B
dist/index.cjs = 1.57 kB 1.57 kB = 359 B 359 B
dist/index.js = 1.09 kB 1.09 kB = 279 B 279 B
dist/interval/index.cjs = 3.88 kB 3.88 kB = 1.14 kB 1.14 kB
dist/interval/index.js = 3.74 kB 3.74 kB = 1.12 kB 1.12 kB
dist/macro.cjs = 1.91 kB 1.91 kB = 808 B 808 B
dist/not/index.cjs = 161 B 161 B = 148 B 148 B
dist/not/index.js = 69 B 69 B = 81 B 81 B
dist/or/index.cjs = 393 B 393 B = 249 B 249 B
dist/or/index.js = 411 B 411 B = 245 B 245 B
dist/patronum.cjs +0.47% 18.6 kB 18.7 kB +0.51% 5.86 kB 5.89 kB
dist/patronum.js +0.51% 17.6 kB 17.7 kB +0.42% 5.94 kB 5.97 kB
dist/patronum.umd.js +0.44% 19.6 kB 19.7 kB +0.52% 5.96 kB 6 kB
dist/pending/index.cjs = 909 B 909 B = 495 B 495 B
dist/pending/index.js = 828 B 828 B = 444 B 444 B
dist/reset/index.cjs = 324 B 324 B = 208 B 208 B
dist/reset/index.js = 249 B 249 B = 157 B 157 B
dist/reshape/index.cjs = 419 B 419 B = 242 B 242 B
dist/reshape/index.js = 379 B 379 B = 201 B 201 B
dist/snapshot/index.cjs = 756 B 756 B = 346 B 346 B
dist/snapshot/index.js = 641 B 641 B = 288 B 288 B
dist/some/index.cjs = 1.16 kB 1.16 kB = 474 B 474 B
dist/some/index.js = 1.02 kB 1.02 kB = 407 B 407 B
dist/split-map/index.cjs = 628 B 628 B = 359 B 359 B
dist/split-map/index.js = 575 B 575 B = 318 B 318 B
dist/spread/index.cjs = 1.26 kB 1.26 kB = 534 B 534 B
dist/spread/index.js = 1.28 kB 1.28 kB = 516 B 516 B
dist/status/index.cjs = 426 B 426 B = 265 B 265 B
dist/status/index.js = 339 B 339 B = 208 B 208 B
dist/throttle/index.cjs = 2.1 kB 2.1 kB = 814 B 814 B
dist/throttle/index.js = 1.99 kB 1.99 kB = 775 B 775 B
dist/time/index.cjs = 719 B 719 B = 375 B 375 B
dist/time/index.js = 621 B 621 B = 321 B 321 B

Please sign in to comment.