-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement testing and reactivity adapter for MobX
resolves #14
- Loading branch information
Showing
9 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
head: | ||
- - link | ||
- rel: canonical | ||
href: https://signaldb.js.org/reactivity/mobx/ | ||
--- | ||
# Reactivity adapter for [`MobX`](https://mobx.js.org/) | ||
|
||
Integrating signals (or observables) in MobX with signaldb has revolutionized the way state management operates in modern applications. MobX, renowned for its effortless state management capabilities, transparently implements functional reactive programming. Through the use of a MobX reactivity adapter for signaldb, developers can harness the powerful reactivity mechanisms of MobX within the database context of signaldb. When you marry the dynamic, auto-updating nature of MobX observables with signaldb collections, you cultivate an environment where state and data persistency harmoniously coexist. Especially in scenarios where real-time data updates are crucial, this combination ensures that all dependent parts of your codebase are notified and updated whenever underlying data changes. Furthermore, the adaptability of MobX means that it seamlessly meshes with various frameworks, including the versatile signaldb. As the landscape of web development shifts towards more reactive paradigms, integrating MobX with signaldb emerges as a compelling solution for developers seeking efficient, boilerplate-free state management synchronized with a dependable database. If your framework still lacks a reactivity adapter, consider the value addition of integrating MobX and its unparalleled reactivity system into your tech stack. | ||
|
||
## Adapter | ||
|
||
```js | ||
import { observable, runInAction, onBecomeUnobserved } from 'mobx' | ||
import { createReactivityAdapter } from 'signaldb' | ||
|
||
const reactivityAdapter = createReactivityAdapter({ | ||
create: () => { | ||
const dep = observable({ count: 0 }) | ||
return { | ||
depend: () => { | ||
dep.count | ||
}, | ||
notify: () => { | ||
runInAction(() => { | ||
dep.count += 1 | ||
}) | ||
}, | ||
raw: dep, | ||
} | ||
}, | ||
onDispose(callback, { raw: dep }) { | ||
onBecomeUnobserved(dep, 'count', callback) | ||
}, | ||
}) | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
import { Collection } from 'signaldb' | ||
import { autorun } from 'mobx' | ||
|
||
const posts = new Collection({ | ||
reactivity: reactivityAdapter, | ||
}) | ||
|
||
autorun(() => { | ||
const cursor = posts.find({ author: 'John' }) | ||
console.log(cursor.count()) | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,7 @@ | |
}, | ||
"dependencies": { | ||
"fast-sort": "^3.4.0", | ||
"mingo": "^6.4.4" | ||
"mingo": "^6.4.4", | ||
"mobx": "^6.10.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import type ReactivityAdapter from './types/ReactivityAdapter' | ||
import type { Signal } from './types/ReactivityAdapter' | ||
|
||
export default function createReactivityAdapter(definition: ReactivityAdapter) { | ||
export default function createReactivityAdapter<T extends Signal = Signal>( | ||
definition: ReactivityAdapter<T>, | ||
) { | ||
return definition | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
interface Signal { | ||
export interface Signal { | ||
depend(): void, | ||
notify(): void, | ||
} | ||
|
||
export default interface ReactivityAdapter { | ||
create(): Signal, | ||
onDispose?(callback: () => void): void, | ||
export default interface ReactivityAdapter<T extends Signal = Signal> { | ||
create(): T, | ||
onDispose?(callback: () => void, signal: T): void, | ||
isInScope?(): boolean, | ||
} |