vapor-chamber 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +504 -0
- package/README.md +301 -0
- package/dist/chamber.d.ts +49 -0
- package/dist/chamber.d.ts.map +1 -0
- package/dist/chamber.js +149 -0
- package/dist/command-bus.d.ts +38 -0
- package/dist/command-bus.d.ts.map +1 -0
- package/dist/command-bus.js +119 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/plugins.d.ts +46 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +130 -0
- package/package.json +65 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vapor-chamber - Built-in plugins
|
|
3
|
+
*/
|
|
4
|
+
import type { Command, Plugin } from './command-bus';
|
|
5
|
+
/**
|
|
6
|
+
* Logger plugin - logs all commands and results
|
|
7
|
+
*/
|
|
8
|
+
export declare function logger(options?: {
|
|
9
|
+
collapsed?: boolean;
|
|
10
|
+
filter?: (cmd: Command) => boolean;
|
|
11
|
+
}): Plugin;
|
|
12
|
+
/**
|
|
13
|
+
* Validator plugin - validate commands before execution
|
|
14
|
+
*/
|
|
15
|
+
export declare function validator(rules: {
|
|
16
|
+
[action: string]: (cmd: Command) => string | null;
|
|
17
|
+
}): Plugin;
|
|
18
|
+
/**
|
|
19
|
+
* History plugin - tracks command history for undo/redo
|
|
20
|
+
*/
|
|
21
|
+
export interface HistoryState {
|
|
22
|
+
past: Command[];
|
|
23
|
+
future: Command[];
|
|
24
|
+
canUndo: boolean;
|
|
25
|
+
canRedo: boolean;
|
|
26
|
+
}
|
|
27
|
+
export declare function history(options?: {
|
|
28
|
+
maxSize?: number;
|
|
29
|
+
filter?: (cmd: Command) => boolean;
|
|
30
|
+
}): Plugin & {
|
|
31
|
+
getState: () => HistoryState;
|
|
32
|
+
undo: () => Command | undefined;
|
|
33
|
+
redo: () => Command | undefined;
|
|
34
|
+
clear: () => void;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Debounce plugin - debounce specific actions
|
|
38
|
+
* Returns a pending result immediately, executes after wait period
|
|
39
|
+
*/
|
|
40
|
+
export declare function debounce(actions: string[], wait: number): Plugin;
|
|
41
|
+
/**
|
|
42
|
+
* Throttle plugin - execute immediately, then block for wait period
|
|
43
|
+
* Unlike debounce, throttle guarantees execution on first call
|
|
44
|
+
*/
|
|
45
|
+
export declare function throttle(actions: string[], wait: number): Plugin;
|
|
46
|
+
//# sourceMappingURL=plugins.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugins.d.ts","sourceRoot":"","sources":["../src/plugins.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAiB,MAAM,EAAE,MAAM,eAAe,CAAC;AAEpE;;GAEG;AACH,wBAAgB,MAAM,CAAC,OAAO,GAAE;IAC9B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC;CAC/B,GAAG,MAAM,CAwBd;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE;IAC/B,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,GAAG,EAAE,OAAO,KAAK,MAAM,GAAG,IAAI,CAAC;CACnD,GAAG,MAAM,CAWT;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,OAAO,EAAE,CAAC;IAChB,MAAM,EAAE,OAAO,EAAE,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,wBAAgB,OAAO,CAAC,OAAO,GAAE;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC;CAC/B,GAAG,MAAM,GAAG;IAChB,QAAQ,EAAE,MAAM,YAAY,CAAC;IAC7B,IAAI,EAAE,MAAM,OAAO,GAAG,SAAS,CAAC;IAChC,IAAI,EAAE,MAAM,OAAO,GAAG,SAAS,CAAC;IAChC,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB,CAwCA;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,MAAM,EAAE,EACjB,IAAI,EAAE,MAAM,GACX,MAAM,CAwBR;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,MAAM,EAAE,EACjB,IAAI,EAAE,MAAM,GACX,MAAM,CAmBR"}
|
package/dist/plugins.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* vapor-chamber - Built-in plugins
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Logger plugin - logs all commands and results
|
|
6
|
+
*/
|
|
7
|
+
export function logger(options = {}) {
|
|
8
|
+
const { collapsed = true, filter } = options;
|
|
9
|
+
return (cmd, next) => {
|
|
10
|
+
if (filter && !filter(cmd))
|
|
11
|
+
return next();
|
|
12
|
+
const label = `⚡ ${cmd.action}`;
|
|
13
|
+
const log = collapsed ? console.groupCollapsed : console.group;
|
|
14
|
+
log(label);
|
|
15
|
+
console.log('target:', cmd.target);
|
|
16
|
+
if (cmd.payload !== undefined)
|
|
17
|
+
console.log('payload:', cmd.payload);
|
|
18
|
+
const result = next();
|
|
19
|
+
if (result.ok) {
|
|
20
|
+
console.log('result:', result.value);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
console.error('error:', result.error);
|
|
24
|
+
}
|
|
25
|
+
console.groupEnd();
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Validator plugin - validate commands before execution
|
|
31
|
+
*/
|
|
32
|
+
export function validator(rules) {
|
|
33
|
+
return (cmd, next) => {
|
|
34
|
+
const rule = rules[cmd.action];
|
|
35
|
+
if (rule) {
|
|
36
|
+
const error = rule(cmd);
|
|
37
|
+
if (error) {
|
|
38
|
+
return { ok: false, error: new Error(error) };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return next();
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export function history(options = {}) {
|
|
45
|
+
const { maxSize = 50, filter } = options;
|
|
46
|
+
const past = [];
|
|
47
|
+
const future = [];
|
|
48
|
+
const plugin = (cmd, next) => {
|
|
49
|
+
const result = next();
|
|
50
|
+
// Only track successful commands that pass filter
|
|
51
|
+
if (result.ok && (!filter || filter(cmd))) {
|
|
52
|
+
past.push(cmd);
|
|
53
|
+
if (past.length > maxSize)
|
|
54
|
+
past.shift();
|
|
55
|
+
future.length = 0; // Clear redo stack on new command
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
59
|
+
return Object.assign(plugin, {
|
|
60
|
+
getState: () => ({
|
|
61
|
+
past: [...past],
|
|
62
|
+
future: [...future],
|
|
63
|
+
canUndo: past.length > 0,
|
|
64
|
+
canRedo: future.length > 0,
|
|
65
|
+
}),
|
|
66
|
+
undo: () => {
|
|
67
|
+
const cmd = past.pop();
|
|
68
|
+
if (cmd)
|
|
69
|
+
future.push(cmd);
|
|
70
|
+
return cmd;
|
|
71
|
+
},
|
|
72
|
+
redo: () => {
|
|
73
|
+
const cmd = future.pop();
|
|
74
|
+
if (cmd)
|
|
75
|
+
past.push(cmd);
|
|
76
|
+
return cmd;
|
|
77
|
+
},
|
|
78
|
+
clear: () => {
|
|
79
|
+
past.length = 0;
|
|
80
|
+
future.length = 0;
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Debounce plugin - debounce specific actions
|
|
86
|
+
* Returns a pending result immediately, executes after wait period
|
|
87
|
+
*/
|
|
88
|
+
export function debounce(actions, wait) {
|
|
89
|
+
const actionSet = new Set(actions);
|
|
90
|
+
const timers = new Map();
|
|
91
|
+
const results = new Map();
|
|
92
|
+
return (cmd, next) => {
|
|
93
|
+
if (!actionSet.has(cmd.action))
|
|
94
|
+
return next();
|
|
95
|
+
const key = `${cmd.action}:${JSON.stringify(cmd.target)}`;
|
|
96
|
+
// Clear existing timer
|
|
97
|
+
const existing = timers.get(key);
|
|
98
|
+
if (existing)
|
|
99
|
+
clearTimeout(existing);
|
|
100
|
+
// Schedule execution
|
|
101
|
+
timers.set(key, setTimeout(() => {
|
|
102
|
+
const result = next();
|
|
103
|
+
results.set(key, result);
|
|
104
|
+
timers.delete(key);
|
|
105
|
+
}, wait));
|
|
106
|
+
// Return pending status synchronously (check results map for actual result)
|
|
107
|
+
return results.get(key) ?? { ok: true, value: { pending: true, key } };
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Throttle plugin - execute immediately, then block for wait period
|
|
112
|
+
* Unlike debounce, throttle guarantees execution on first call
|
|
113
|
+
*/
|
|
114
|
+
export function throttle(actions, wait) {
|
|
115
|
+
const actionSet = new Set(actions);
|
|
116
|
+
const lastRun = new Map();
|
|
117
|
+
return (cmd, next) => {
|
|
118
|
+
if (!actionSet.has(cmd.action))
|
|
119
|
+
return next();
|
|
120
|
+
const key = `${cmd.action}:${JSON.stringify(cmd.target)}`;
|
|
121
|
+
const now = Date.now();
|
|
122
|
+
const last = lastRun.get(key) ?? 0;
|
|
123
|
+
if (now - last >= wait) {
|
|
124
|
+
lastRun.set(key, now);
|
|
125
|
+
return next();
|
|
126
|
+
}
|
|
127
|
+
// Throttled - return skipped status
|
|
128
|
+
return { ok: true, value: { throttled: true, key, retryIn: wait - (now - last) } };
|
|
129
|
+
};
|
|
130
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vapor-chamber",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Lightweight command bus for Vue Vapor - plugins, hooks, undo/redo",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"dev": "tsc --watch",
|
|
21
|
+
"test": "vitest",
|
|
22
|
+
"test:run": "vitest run",
|
|
23
|
+
"test:coverage": "vitest run --coverage",
|
|
24
|
+
"prepublishOnly": "npm run build && npm run test:run"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"vue",
|
|
28
|
+
"vapor",
|
|
29
|
+
"vue-vapor",
|
|
30
|
+
"command-bus",
|
|
31
|
+
"commands",
|
|
32
|
+
"plugins",
|
|
33
|
+
"middleware",
|
|
34
|
+
"undo-redo",
|
|
35
|
+
"state-management",
|
|
36
|
+
"signals",
|
|
37
|
+
"reactive"
|
|
38
|
+
],
|
|
39
|
+
"author": "Luciano Federico Pereira",
|
|
40
|
+
"license": "LGPL-2.1",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/lucianofedericopereira/vapor-chamber.git"
|
|
44
|
+
},
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/lucianofedericopereira/vapor-chamber/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/lucianofedericopereira/vapor-chamber#readme",
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"vue": ">=3.5.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"vue": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
59
|
+
"typescript": "^5.0.0",
|
|
60
|
+
"vitest": "^4.0.18"
|
|
61
|
+
},
|
|
62
|
+
"engines": {
|
|
63
|
+
"node": ">=18.0.0"
|
|
64
|
+
}
|
|
65
|
+
}
|