-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
202 additions
and
127 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
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
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,14 @@ | ||
import { useViewModel } from "@frui.ts/views"; | ||
import { Observer } from "mobx-react-lite"; | ||
import React from "react"; | ||
import SettingsViewModel from "./settingsViewModel"; | ||
|
||
export function Settings() { | ||
const vm = useViewModel(() => new SettingsViewModel(), {}, []); | ||
|
||
return ( | ||
<p> | ||
<Observer>{() => <span>{vm.text}</span>}</Observer> | ||
</p> | ||
); | ||
} |
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,23 @@ | ||
import type { IViewModel } from "@frui.ts/views"; | ||
import { makeObservable, observable } from "mobx"; | ||
|
||
export default class SettingsViewModel implements IViewModel<unknown> { | ||
@observable | ||
text = crypto.randomUUID(); | ||
|
||
constructor() { | ||
makeObservable(this); | ||
} | ||
|
||
onInitialize(context: unknown) { | ||
console.log("settings on initialize", this.text, context); | ||
} | ||
|
||
onActivate(context: unknown) { | ||
console.log("settings on activate", this.text, context); | ||
} | ||
|
||
onDeactivate(context: unknown) { | ||
console.log("settings on deactivate", this.text, context); | ||
} | ||
} |
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,83 @@ | ||
import { Mutex } from "async-mutex"; | ||
import type { IViewModel } from "../types"; | ||
|
||
export class ViewModelLifecycleManager<TContext, TViewModel extends IViewModel<TContext>> { | ||
#instance: TViewModel | undefined; | ||
#lock = new Mutex(); | ||
|
||
_isInitialized = false; | ||
|
||
_isActive = false; | ||
|
||
get instance() { | ||
this.#instance ??= this.factory(); | ||
return this.#instance; | ||
} | ||
|
||
constructor(private factory: () => TViewModel) {} | ||
|
||
resetInstance() { | ||
this.#instance = undefined; | ||
this._isInitialized = false; | ||
this._isActive = false; | ||
} | ||
|
||
async initialize(context: TContext) { | ||
await this.#lock.runExclusive(async () => { | ||
await this.callInitialize(context); | ||
}); | ||
} | ||
|
||
private async callInitialize(context: TContext) { | ||
if (!this._isInitialized) { | ||
await this.instance.onInitialize?.(context); | ||
this._isInitialized = true; | ||
} | ||
} | ||
|
||
async activate(context: TContext) { | ||
await this.#lock.runExclusive(async () => { | ||
await this.callInitialize(context); | ||
await this.callActivate(context); | ||
}); | ||
} | ||
|
||
private async callActivate(context: TContext) { | ||
if (!this._isActive) { | ||
await this.instance.onActivate?.(context); | ||
this._isActive = true; | ||
} | ||
} | ||
|
||
async navigate(context: TContext) { | ||
await this.#lock.runExclusive(async () => { | ||
await this.callInitialize(context); | ||
await this.callActivate(context); | ||
await this.callNavigate(context); | ||
}); | ||
} | ||
|
||
private async callNavigate(context: TContext) { | ||
await this.instance.onNavigate?.(context); | ||
} | ||
|
||
async deactivate(context: TContext) { | ||
await this.#lock.runExclusive(async () => { | ||
await this.callDeactivate(context); | ||
}); | ||
} | ||
|
||
private async callDeactivate(context: TContext) { | ||
if (this._isActive) { | ||
await this.instance.onDeactivate?.(context); | ||
this._isActive = false; | ||
} | ||
} | ||
|
||
async close(context: TContext) { | ||
await this.#lock.runExclusive(async () => { | ||
await this.callDeactivate(context); | ||
this.resetInstance(); | ||
}); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/views/src/view/useDisposable.ts → packages/views/src/hooks/useDisposable.ts
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,29 @@ | ||
import type { DependencyList } from "react"; | ||
import { useEffect, useRef } from "react"; | ||
import { ViewModelLifecycleManager } from "../helpers/viewModelLifecycleManager"; | ||
import type { IViewModel } from "../types"; | ||
|
||
export function useViewModel<TContext, TViewModel extends IViewModel<TContext>>( | ||
factory: () => TViewModel, | ||
context: TContext, | ||
dependencies?: DependencyList | ||
) { | ||
const vmManager = useRef(new ViewModelLifecycleManager(factory)); | ||
|
||
const currentContext = useRef(context); | ||
currentContext.current = context; | ||
|
||
useEffect(() => { | ||
void vmManager.current.initialize(currentContext.current); | ||
|
||
return () => { | ||
void vmManager.current.close(currentContext.current); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
void vmManager.current.navigate(currentContext.current); | ||
}, dependencies ?? [context]); | ||
|
||
return vmManager.current.instance; | ||
} |
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
Oops, something went wrong.