Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/emittor-middlewares/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,27 @@ page.use(
);
```

## Usage

### 4) Log state changes

A simple logger to trace state changes.

```ts
import { createEmittor } from "emittor";
import { middlewareLogger } from "emittor-middlewares";

export const search = createEmittor("");

search.use(middlewareLogger);
```

## API

### `middlewareLogger => Middleware`

Log state changes with timestamps.

### `middlewareDebounce(callback, options?) => Middleware`

Debounce a callback that receives the emittor, then calls `next()`.
Expand Down
3 changes: 2 additions & 1 deletion packages/emittor-middlewares/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsup"
"build": "tsup",
"check-types": "tsc --noEmit"
},
"keywords": [],
"author": "immi",
Expand Down
1 change: 1 addition & 0 deletions packages/emittor-middlewares/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./middlewares";
export * from "./logger";
21 changes: 21 additions & 0 deletions packages/emittor-middlewares/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Middleware } from "emittor";

const style = (color: string) => `color: ${color}; font-weight: bold;`;

export const middlewareLogger: Middleware<any, any> = (next, em) => {
const prevState = em.getState();
const time = new Date();

console.groupCollapsed(
`%cemittor @ ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}.${time.getMilliseconds()}`,
style("gray")
);

console.log("%cprev state", style("#9E9E9E"), prevState);

next();

const nextState = em.getState();
console.log("%cnext state", style("#4CAF50"), nextState);
console.groupEnd();
};
7 changes: 1 addition & 6 deletions packages/emittor-middlewares/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"noUncheckedIndexedAccess": true,
"target": "ES2022",
"outDir": "dist",
"rootDir": "src",
"module": "esnext",
"moduleResolution": "node",
"lib": ["dom", "dom.iterable", "esnext", "es2022"],
Expand All @@ -21,11 +20,7 @@
"resolveJsonModule": true,
"isolatedModules": true,
// "noEmit": true,
"jsx": "react-jsx",
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
"jsx": "react-jsx"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
Expand Down
3 changes: 2 additions & 1 deletion packages/emittor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsx ./preBuild.ts && tsup"
"build": "tsx ./preBuild.ts && tsup",
"check-types": "tsc --noEmit"
},
"keywords": [
"next",
Expand Down
Loading