@octanejs/mobx 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 +21 -0
- package/README.md +30 -0
- package/package.json +52 -0
- package/src/ObserverComponent.ts +21 -0
- package/src/index.ts +12 -0
- package/src/internal.ts +26 -0
- package/src/observer.ts +36 -0
- package/src/staticRendering.ts +12 -0
- package/src/useLocalObservable.ts +28 -0
- package/src/useObserver.ts +116 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dominic Gannaway
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @octanejs/mobx
|
|
2
|
+
|
|
3
|
+
MobX bindings for Octane. The package re-exports the framework-independent
|
|
4
|
+
`mobx` core and implements the `mobx-react-lite` function-component surface with
|
|
5
|
+
Octane hooks.
|
|
6
|
+
|
|
7
|
+
```tsrx
|
|
8
|
+
import { makeAutoObservable, observer } from '@octanejs/mobx';
|
|
9
|
+
|
|
10
|
+
const store = makeAutoObservable({ count: 0 });
|
|
11
|
+
|
|
12
|
+
const Counter = observer(function Counter(props: { store: typeof store }) @{
|
|
13
|
+
<button onClick={() => props.store.count++}>{props.store.count as string}</button>
|
|
14
|
+
});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Supported
|
|
18
|
+
|
|
19
|
+
- MobX core exports
|
|
20
|
+
- `observer`
|
|
21
|
+
- `useObserver`
|
|
22
|
+
- `Observer`
|
|
23
|
+
- `useLocalObservable`
|
|
24
|
+
- `enableStaticRendering` and `isUsingStaticRendering`
|
|
25
|
+
|
|
26
|
+
## Intentional v1 limits
|
|
27
|
+
|
|
28
|
+
This binding targets compiled Octane function components. React class
|
|
29
|
+
components, legacy `Provider`/`inject`, `forwardRef` compatibility options,
|
|
30
|
+
React-specific batching, and React DevTools integration are not included.
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@octanejs/mobx",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22"
|
|
8
|
+
},
|
|
9
|
+
"octane": {
|
|
10
|
+
"hookSlots": {
|
|
11
|
+
"manual": [
|
|
12
|
+
"src"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"description": "MobX bindings for Octane, reusing the MobX core with Octane-native observer hooks and components.",
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Dominic Gannaway",
|
|
19
|
+
"email": "dg@domgan.com"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/octanejs/octane.git",
|
|
27
|
+
"directory": "packages/mobx"
|
|
28
|
+
},
|
|
29
|
+
"main": "src/index.ts",
|
|
30
|
+
"module": "src/index.ts",
|
|
31
|
+
"types": "src/index.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"src",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"exports": {
|
|
37
|
+
".": "./src/index.ts"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"mobx": "6.16.1"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"octane": "0.1.19"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"vitest": "^4.1.10",
|
|
47
|
+
"octane": "0.1.19"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"test": "vitest run --root ../.. --project mobx --project mobx-ssr"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ComponentBody } from 'octane';
|
|
2
|
+
import { useObserverWithSlot } from './useObserver';
|
|
3
|
+
|
|
4
|
+
export type ObserverProps = {
|
|
5
|
+
children?: () => unknown;
|
|
6
|
+
render?: () => unknown;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const observerSlot = Symbol('@octanejs/mobx:Observer');
|
|
10
|
+
|
|
11
|
+
export const Observer: ComponentBody<ObserverProps> = ({ children, render }) => {
|
|
12
|
+
if (children && render) {
|
|
13
|
+
console.error('MobX Observer: Do not use children and render at the same time.');
|
|
14
|
+
}
|
|
15
|
+
const component = children || render;
|
|
16
|
+
return typeof component === 'function'
|
|
17
|
+
? useObserverWithSlot(component, 'Observer', observerSlot)
|
|
18
|
+
: null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
(Observer as ComponentBody<ObserverProps> & { displayName?: string }).displayName = 'Observer';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from 'mobx';
|
|
2
|
+
export { Observer } from './ObserverComponent';
|
|
3
|
+
export type { ObserverProps } from './ObserverComponent';
|
|
4
|
+
export { observer } from './observer';
|
|
5
|
+
export type { ObserverComponent } from './observer';
|
|
6
|
+
export {
|
|
7
|
+
enableStaticRendering,
|
|
8
|
+
isUsingStaticRendering,
|
|
9
|
+
useStaticRendering,
|
|
10
|
+
} from './staticRendering';
|
|
11
|
+
export { useLocalObservable } from './useLocalObservable';
|
|
12
|
+
export { useObserver } from './useObserver';
|
package/src/internal.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const subSlotCache = new Map<symbol, Map<string, symbol>>();
|
|
2
|
+
const bareTagCache = new Map<string, symbol>();
|
|
3
|
+
|
|
4
|
+
export function subSlot(slot: symbol | undefined, tag: string): symbol {
|
|
5
|
+
if (slot === undefined) {
|
|
6
|
+
let bare = bareTagCache.get(tag);
|
|
7
|
+
if (bare === undefined) {
|
|
8
|
+
bare = Symbol.for(`@octanejs/mobx:${tag}`);
|
|
9
|
+
bareTagCache.set(tag, bare);
|
|
10
|
+
}
|
|
11
|
+
return bare;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let byTag = subSlotCache.get(slot);
|
|
15
|
+
if (byTag === undefined) {
|
|
16
|
+
byTag = new Map();
|
|
17
|
+
subSlotCache.set(slot, byTag);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let child = byTag.get(tag);
|
|
21
|
+
if (child === undefined) {
|
|
22
|
+
child = Symbol(`${slot.description ?? '@octanejs/mobx'}:${tag}`);
|
|
23
|
+
byTag.set(tag, child);
|
|
24
|
+
}
|
|
25
|
+
return child;
|
|
26
|
+
}
|
package/src/observer.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { memo } from 'octane';
|
|
2
|
+
import type { ComponentBody } from 'octane';
|
|
3
|
+
import { isUsingStaticRendering } from './staticRendering';
|
|
4
|
+
import { useObserverWithSlot } from './useObserver';
|
|
5
|
+
|
|
6
|
+
const OBSERVED = Symbol.for('@octanejs/mobx/observed');
|
|
7
|
+
|
|
8
|
+
export type ObserverComponent<P> = ComponentBody<P> & {
|
|
9
|
+
displayName?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export function observer<P>(baseComponent: ComponentBody<P>): ObserverComponent<P> {
|
|
13
|
+
const observedBase = baseComponent as ObserverComponent<P>;
|
|
14
|
+
if ((observedBase as ObserverComponent<P> & { [OBSERVED]?: true })[OBSERVED]) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
'[@octanejs/mobx] You are trying to apply observer to an already observed component.',
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
if (isUsingStaticRendering()) return observedBase;
|
|
20
|
+
|
|
21
|
+
const baseComponentName = observedBase.displayName || observedBase.name || 'observed';
|
|
22
|
+
const hookSlot = Symbol(`@octanejs/mobx:observer:${baseComponentName}`);
|
|
23
|
+
const observerComponent: ObserverComponent<P> = (props, scope, extra) =>
|
|
24
|
+
useObserverWithSlot(() => baseComponent(props, scope, extra), baseComponentName, hookSlot);
|
|
25
|
+
|
|
26
|
+
observerComponent.displayName = observedBase.displayName;
|
|
27
|
+
if (Object.getOwnPropertyDescriptor(observerComponent, 'name')?.configurable) {
|
|
28
|
+
Object.defineProperty(observerComponent, 'name', {
|
|
29
|
+
value: observedBase.name,
|
|
30
|
+
configurable: true,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
const memoized = memo(observerComponent) as ObserverComponent<P>;
|
|
34
|
+
Object.defineProperty(memoized, OBSERVED, { value: true });
|
|
35
|
+
return memoized;
|
|
36
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
let staticRendering = false;
|
|
2
|
+
|
|
3
|
+
export function enableStaticRendering(enable: boolean): void {
|
|
4
|
+
staticRendering = enable;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** @deprecated Use enableStaticRendering instead. */
|
|
8
|
+
export const useStaticRendering = enableStaticRendering;
|
|
9
|
+
|
|
10
|
+
export function isUsingStaticRendering(): boolean {
|
|
11
|
+
return staticRendering;
|
|
12
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { observable } from 'mobx';
|
|
2
|
+
import type { AnnotationsMap } from 'mobx';
|
|
3
|
+
import { useState } from 'octane';
|
|
4
|
+
import { subSlot } from './internal';
|
|
5
|
+
|
|
6
|
+
export function useLocalObservable<TStore extends Record<string, unknown>>(
|
|
7
|
+
initializer: () => TStore,
|
|
8
|
+
annotations?: AnnotationsMap<TStore, never>,
|
|
9
|
+
): TStore;
|
|
10
|
+
export function useLocalObservable<TStore extends Record<string, unknown>>(
|
|
11
|
+
initializer: () => TStore,
|
|
12
|
+
annotations?: AnnotationsMap<TStore, never> | symbol,
|
|
13
|
+
...rest: [slot?: symbol]
|
|
14
|
+
): TStore {
|
|
15
|
+
const slot =
|
|
16
|
+
typeof rest[rest.length - 1] === 'symbol'
|
|
17
|
+
? rest[rest.length - 1]
|
|
18
|
+
: typeof annotations === 'symbol'
|
|
19
|
+
? annotations
|
|
20
|
+
: undefined;
|
|
21
|
+
return useState(
|
|
22
|
+
() =>
|
|
23
|
+
observable(initializer(), typeof annotations === 'symbol' ? undefined : annotations, {
|
|
24
|
+
autoBind: true,
|
|
25
|
+
}),
|
|
26
|
+
subSlot(slot, 'local-observable'),
|
|
27
|
+
)[0];
|
|
28
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Reaction } from 'mobx';
|
|
2
|
+
import { useRef, useSyncExternalStore } from 'octane';
|
|
3
|
+
import { subSlot } from './internal';
|
|
4
|
+
import { isUsingStaticRendering } from './staticRendering';
|
|
5
|
+
|
|
6
|
+
type ObserverAdministration = {
|
|
7
|
+
reaction: Reaction | null;
|
|
8
|
+
onStoreChange: (() => void) | null;
|
|
9
|
+
stateVersion: symbol;
|
|
10
|
+
name: string;
|
|
11
|
+
subscribe: (onStoreChange: () => void) => () => void;
|
|
12
|
+
getSnapshot: () => symbol;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const observerFinalizationRegistry = new FinalizationRegistry<ObserverAdministration>((adm) => {
|
|
16
|
+
adm.reaction?.dispose();
|
|
17
|
+
adm.reaction = null;
|
|
18
|
+
adm.onStoreChange = null;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
function createReaction(adm: ObserverAdministration): void {
|
|
22
|
+
adm.reaction = new Reaction(`observer${adm.name}`, () => {
|
|
23
|
+
adm.stateVersion = Symbol();
|
|
24
|
+
adm.onStoreChange?.();
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function runObserver<T>(render: () => T, baseComponentName: string, slot: symbol | undefined): T {
|
|
29
|
+
if (isUsingStaticRendering()) return render();
|
|
30
|
+
|
|
31
|
+
const admRef = useRef<ObserverAdministration | null>(null, subSlot(slot, 'administration'));
|
|
32
|
+
|
|
33
|
+
if (admRef.current === null) {
|
|
34
|
+
const adm: ObserverAdministration = {
|
|
35
|
+
reaction: null,
|
|
36
|
+
onStoreChange: null,
|
|
37
|
+
stateVersion: Symbol(),
|
|
38
|
+
name: baseComponentName,
|
|
39
|
+
subscribe(onStoreChange) {
|
|
40
|
+
observerFinalizationRegistry.unregister(adm);
|
|
41
|
+
adm.onStoreChange = onStoreChange;
|
|
42
|
+
if (adm.reaction === null) {
|
|
43
|
+
createReaction(adm);
|
|
44
|
+
adm.stateVersion = Symbol();
|
|
45
|
+
}
|
|
46
|
+
return () => {
|
|
47
|
+
adm.onStoreChange = null;
|
|
48
|
+
adm.reaction?.dispose();
|
|
49
|
+
adm.reaction = null;
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
getSnapshot() {
|
|
53
|
+
return adm.stateVersion;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
admRef.current = adm;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const adm = admRef.current;
|
|
60
|
+
if (adm.reaction === null) {
|
|
61
|
+
createReaction(adm);
|
|
62
|
+
observerFinalizationRegistry.register(admRef, adm, adm);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
useSyncExternalStore(
|
|
66
|
+
adm.subscribe,
|
|
67
|
+
adm.getSnapshot,
|
|
68
|
+
adm.getSnapshot,
|
|
69
|
+
subSlot(slot, 'external-store'),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
let result!: T;
|
|
73
|
+
let error: unknown;
|
|
74
|
+
let didThrow = false;
|
|
75
|
+
const reaction = adm.reaction;
|
|
76
|
+
if (reaction === null) {
|
|
77
|
+
throw new Error('[@octanejs/mobx] Observer reaction was disposed during render.');
|
|
78
|
+
}
|
|
79
|
+
reaction.track(() => {
|
|
80
|
+
try {
|
|
81
|
+
result = render();
|
|
82
|
+
} catch (caught) {
|
|
83
|
+
didThrow = true;
|
|
84
|
+
error = caught;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
if (didThrow) throw error;
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function useObserver<T>(render: () => T, baseComponentName?: string): T;
|
|
92
|
+
export function useObserver<T>(
|
|
93
|
+
render: () => T,
|
|
94
|
+
baseComponentName: string | symbol = 'observed',
|
|
95
|
+
...rest: [slot?: symbol]
|
|
96
|
+
): T {
|
|
97
|
+
const slot =
|
|
98
|
+
typeof rest[rest.length - 1] === 'symbol'
|
|
99
|
+
? rest[rest.length - 1]
|
|
100
|
+
: typeof baseComponentName === 'symbol'
|
|
101
|
+
? baseComponentName
|
|
102
|
+
: undefined;
|
|
103
|
+
return runObserver(
|
|
104
|
+
render,
|
|
105
|
+
typeof baseComponentName === 'string' ? baseComponentName : 'observed',
|
|
106
|
+
slot,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function useObserverWithSlot<T>(
|
|
111
|
+
render: () => T,
|
|
112
|
+
baseComponentName: string,
|
|
113
|
+
slot: symbol,
|
|
114
|
+
): T {
|
|
115
|
+
return runObserver(render, baseComponentName, slot);
|
|
116
|
+
}
|