Skip to content

Commit d216baf

Browse files
committed
feat: add new utils to mobx kit
1 parent 8704e54 commit d216baf

File tree

15 files changed

+347
-107
lines changed

15 files changed

+347
-107
lines changed

packages/shared/widget-plugin-mobx-kit/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
},
3333
"dependencies": {
3434
"@types/minimatch": "^3.0.5",
35+
"mitt": "^3.0.1",
3536
"mobx": "6.12.3"
3637
},
3738
"devDependencies": {

packages/shared/widget-plugin-mobx-kit/src/SetupHost.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { disposeBatch } from "./disposeBatch";
21
import { SetupComponent } from "./interfaces/SetupComponent";
32
import { SetupComponentHost } from "./interfaces/SetupComponentHost";
3+
import { disposeBatch } from "./lib/disposeBatch";
44

55
export abstract class SetupHost implements SetupComponentHost {
66
private components: Set<SetupComponent> = new Set();
Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1-
type MaybeFn = (() => void) | void;
1+
import { disposeBatch } from "./lib/disposeBatch";
22

3-
export function disposeBatch(): [add: (fn: MaybeFn) => void, disposeAll: () => void] {
4-
const disposers = new Set<() => void>();
5-
6-
const add = (fn: MaybeFn): void => {
7-
if (fn) {
8-
disposers.add(fn);
9-
}
10-
};
11-
12-
const disposeAll = (): void => {
13-
for (const fn of disposers) {
14-
fn();
15-
}
16-
disposers.clear();
17-
};
18-
return [add, disposeAll];
19-
}
3+
export {
4+
/** @deprecated import `disposeBatch` from `@mendix/widget-plugin-mobx-kit/main` instead */
5+
disposeBatch
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface ComputedAtom<T> {
2+
get(): T;
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { computed } from "mobx";
2+
import { ComputedAtom } from "../interfaces/ComputedAtom";
3+
4+
/** Creates a computed atom factory by composing a map function with a computation function. */
5+
export function atomFactory<P extends readonly any[], A extends readonly any[], B>(
6+
map: (...args: P) => A,
7+
fn: (...args: A) => B
8+
): (...args: P) => ComputedAtom<B> {
9+
return (...args: P) => computed(() => fn(...map(...args)));
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import mitt, { Emitter as MittEmitter } from "mitt";
2+
3+
export type Handler<T = unknown> = (event: T) => void;
4+
5+
export type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
6+
7+
export interface Emitter<Events extends Record<string | symbol, unknown>> extends MittEmitter<Events> {
8+
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): () => void;
9+
on(type: "*", handler: WildcardHandler<Events>): () => void;
10+
}
11+
12+
export function createEmitter<Events extends Record<string | symbol, unknown>>(): Emitter<Events> {
13+
const emitter = mitt<Events>();
14+
15+
return {
16+
...emitter,
17+
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]> | WildcardHandler<Events>): () => void {
18+
if (type === "*") {
19+
const fn = handler as WildcardHandler<Events>;
20+
emitter.on(type, fn);
21+
return () => emitter.off(type, fn);
22+
}
23+
const fn = handler as Handler<Events[Key]>;
24+
emitter.on(type, fn);
25+
return () => emitter.off(type, fn);
26+
}
27+
};
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function disposeBatch(): [add: (fn: void | (() => void)) => void, disposeAll: () => void] {
2+
const disposers = new Set<() => void>();
3+
4+
const add = (fn: void | (() => void)): void => {
5+
if (fn) {
6+
disposers.add(fn);
7+
}
8+
};
9+
10+
const disposeAll = (): void => {
11+
for (const fn of disposers) {
12+
fn();
13+
}
14+
disposers.clear();
15+
};
16+
return [add, disposeAll];
17+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
export { autoEffect } from "./autoEffect";
21
export { DerivedGate } from "./DerivedGate";
3-
export { disposeBatch } from "./disposeBatch";
42
export { GateProvider } from "./GateProvider";
3+
export type { ComputedAtom } from "./interfaces/ComputedAtom";
54
export * from "./interfaces/DerivedPropsGate";
65
export * from "./interfaces/DerivedPropsGateProvider";
76
export * from "./interfaces/SetupComponent";
87
export * from "./interfaces/SetupComponentHost";
8+
export { atomFactory } from "./lib/atomFactory";
9+
export { autoEffect } from "./lib/autoEffect";
10+
export { createEmitter } from "./lib/createEmitter";
11+
export type { Emitter } from "./lib/createEmitter";
12+
export { disposeBatch } from "./lib/disposeBatch";
913
export { SetupHost } from "./SetupHost";

packages/shared/widget-plugin-mobx-kit/src/react/useSubscribe.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)