-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented view functions, improved typings
- Loading branch information
1 parent
ef334d3
commit f121207
Showing
5 changed files
with
100 additions
and
13 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 |
---|---|---|
@@ -1,21 +1,34 @@ | ||
import { addHiddenFinalProp } from "../../utils" | ||
import { createActionInvoker } from "../../core" | ||
import { extras } from "mobx" | ||
import { addHiddenFinalProp, createNamedFunction } from "../../utils" | ||
import { IMSTNode, getMSTAdministration } from "../../core" | ||
import { Property } from "./property" | ||
|
||
export class ViewProperty extends Property { | ||
invokeAction: Function | ||
invokeView: Function | ||
|
||
constructor(name: string, fn: Function) { | ||
super(name) | ||
this.invokeAction = createActionInvoker(name, fn) | ||
throw new Error("oops") | ||
this.invokeView = createViewInvoker(name, fn) | ||
} | ||
|
||
initialize(target: any) { | ||
addHiddenFinalProp(target, this.name, this.invokeAction.bind(target)) | ||
addHiddenFinalProp(target, this.name, this.invokeView.bind(target)) | ||
} | ||
|
||
isValidSnapshot(snapshot: any) { | ||
return !(this.name in snapshot) | ||
} | ||
} | ||
|
||
export function createViewInvoker(name: string, fn: Function) { | ||
const viewInvoker = function (this: IMSTNode) { | ||
const args = arguments | ||
const adm = getMSTAdministration(this) | ||
adm.assertAlive() | ||
return extras.allowStateChanges(false, () => fn.apply(this, args)) | ||
} | ||
|
||
// This construction helps producing a better function name in the stack trace, but could be optimized | ||
// away in prod builds, and `actionInvoker` be returned directly | ||
return createNamedFunction(name, viewInvoker) | ||
} |
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