[Init]Init commit

This commit is contained in:
kenyeh
2023-05-19 17:13:53 +08:00
parent f670fe1a11
commit 10690ac902
135 changed files with 59831 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
import { IDerivation } from "../internal";
export declare function createAction(actionName: string, fn: Function, autoAction?: boolean, ref?: Object): Function;
export declare function executeAction(actionName: string, canRunAsDerivation: boolean, fn: Function, scope?: any, args?: IArguments): any;
export interface IActionRunInfo {
prevDerivation_: IDerivation | null;
prevAllowStateChanges_: boolean;
prevAllowStateReads_: boolean;
notifySpy_: boolean;
startTime_: number;
error_?: any;
parentActionId_: number;
actionId_: number;
runAsAction_?: boolean;
}
export declare function _startAction(actionName: string, canRunAsDerivation: boolean, // true for autoAction
scope: any, args?: IArguments): IActionRunInfo;
export declare function _endAction(runInfo: IActionRunInfo): void;
export declare function allowStateChanges<T>(allowStateChanges: boolean, func: () => T): T;
export declare function allowStateChangesStart(allowStateChanges: boolean): boolean;
export declare function allowStateChangesEnd(prev: boolean): void;
@@ -0,0 +1,5 @@
{
"ver": "2.0.0",
"uuid": "1f7b61f1-19cd-47f0-a89f-15fa99caa5be",
"subMetas": {}
}
+36
View File
@@ -0,0 +1,36 @@
import { IDerivationState_, IObservable, IDerivation, Lambda } from "../internal";
export declare const $mobx: unique symbol;
export interface IAtom extends IObservable {
reportObserved(): any;
reportChanged(): any;
}
export declare class Atom implements IAtom {
name_: string;
isPendingUnobservation_: boolean;
isBeingObserved_: boolean;
observers_: Set<IDerivation>;
diffValue_: number;
lastAccessedBy_: number;
lowestObserverState_: IDerivationState_;
/**
* Create a new atom. For debugging purposes it is recommended to give it a name.
* The onBecomeObserved and onBecomeUnobserved callbacks can be used for resource management.
*/
constructor(name_?: string);
onBOL: Set<Lambda> | undefined;
onBUOL: Set<Lambda> | undefined;
onBO(): void;
onBUO(): void;
/**
* Invoke this method to notify mobx that your atom has been used somehow.
* Returns true if there is currently a reactive context.
*/
reportObserved(): boolean;
/**
* Invoke this method _after_ this method has changed to signal mobx that all its observers should invalidate.
*/
reportChanged(): void;
toString(): string;
}
export declare const isAtom: (x: any) => x is Atom;
export declare function createAtom(name: string, onBecomeObservedHandler?: () => void, onBecomeUnobservedHandler?: () => void): IAtom;
@@ -0,0 +1,5 @@
{
"ver": "2.0.0",
"uuid": "6c21e329-0ffd-43d8-9ac8-fe6a7c7b5c2f",
"subMetas": {}
}
+100
View File
@@ -0,0 +1,100 @@
import { CaughtException, IDerivation, IDerivationState_, IEqualsComparer, IObservable, Lambda, TraceMode } from "../internal";
export interface IComputedValue<T> {
get(): T;
set(value: T): void;
observe_(listener: (change: IComputedDidChange<T>) => void, fireImmediately?: boolean): Lambda;
}
export interface IComputedValueOptions<T> {
get?: () => T;
set?: (value: T) => void;
name?: string;
equals?: IEqualsComparer<T>;
context?: any;
requiresReaction?: boolean;
keepAlive?: boolean;
}
export declare type IComputedDidChange<T = any> = {
type: "update";
observableKind: "computed";
object: unknown;
debugObjectName: string;
newValue: T;
oldValue: T | undefined;
};
/**
* A node in the state dependency root that observes other nodes, and can be observed itself.
*
* ComputedValue will remember the result of the computation for the duration of the batch, or
* while being observed.
*
* During this time it will recompute only when one of its direct dependencies changed,
* but only when it is being accessed with `ComputedValue.get()`.
*
* Implementation description:
* 1. First time it's being accessed it will compute and remember result
* give back remembered result until 2. happens
* 2. First time any deep dependency change, propagate POSSIBLY_STALE to all observers, wait for 3.
* 3. When it's being accessed, recompute if any shallow dependency changed.
* if result changed: propagate STALE to all observers, that were POSSIBLY_STALE from the last step.
* go to step 2. either way
*
* If at any point it's outside batch and it isn't observed: reset everything and go to 1.
*/
export declare class ComputedValue<T> implements IObservable, IComputedValue<T>, IDerivation {
dependenciesState_: IDerivationState_;
observing_: IObservable[];
newObserving_: null;
isBeingObserved_: boolean;
isPendingUnobservation_: boolean;
observers_: Set<IDerivation>;
diffValue_: number;
runId_: number;
lastAccessedBy_: number;
lowestObserverState_: IDerivationState_;
unboundDepsCount_: number;
protected value_: T | undefined | CaughtException;
name_: string;
triggeredBy_?: string;
isComputing_: boolean;
isRunningSetter_: boolean;
derivation: () => T;
setter_?: (value: T) => void;
isTracing_: TraceMode;
scope_: Object | undefined;
private equals_;
private requiresReaction_;
keepAlive_: boolean;
/**
* Create a new computed value based on a function expression.
*
* The `name` property is for debug purposes only.
*
* The `equals` property specifies the comparer function to use to determine if a newly produced
* value differs from the previous value. Two comparers are provided in the library; `defaultComparer`
* compares based on identity comparison (===), and `structuralComparer` deeply compares the structure.
* Structural comparison can be convenient if you always produce a new aggregated object and
* don't want to notify observers if it is structurally the same.
* This is useful for working with vectors, mouse coordinates etc.
*/
constructor(options: IComputedValueOptions<T>);
onBecomeStale_(): void;
onBOL: Set<Lambda> | undefined;
onBUOL: Set<Lambda> | undefined;
onBO(): void;
onBUO(): void;
/**
* Returns the current value of this computed value.
* Will evaluate its computation first if needed.
*/
get(): T;
set(value: T): void;
trackAndCompute(): boolean;
computeValue_(track: boolean): T | CaughtException;
suspend_(): void;
observe_(listener: (change: IComputedDidChange<T>) => void, fireImmediately?: boolean): Lambda;
warnAboutUntrackedRead_(): void;
toString(): string;
valueOf(): T;
[Symbol.toPrimitive](): T;
}
export declare const isComputedValue: (x: any) => x is ComputedValue<unknown>;
@@ -0,0 +1,5 @@
{
"ver": "2.0.0",
"uuid": "bc86d0fa-d8e2-4505-a93c-b03e92666c14",
"subMetas": {}
}
+73
View File
@@ -0,0 +1,73 @@
import { IAtom, IDepTreeNode, IObservable } from "../internal";
export declare enum IDerivationState_ {
NOT_TRACKING_ = -1,
UP_TO_DATE_ = 0,
POSSIBLY_STALE_ = 1,
STALE_ = 2
}
export declare enum TraceMode {
NONE = 0,
LOG = 1,
BREAK = 2
}
/**
* A derivation is everything that can be derived from the state (all the atoms) in a pure manner.
* See https://medium.com/@mweststrate/becoming-fully-reactive-an-in-depth-explanation-of-mobservable-55995262a254#.xvbh6qd74
*/
export interface IDerivation extends IDepTreeNode {
observing_: IObservable[];
newObserving_: null | IObservable[];
dependenciesState_: IDerivationState_;
/**
* Id of the current run of a derivation. Each time the derivation is tracked
* this number is increased by one. This number is globally unique
*/
runId_: number;
/**
* amount of dependencies used by the derivation in this run, which has not been bound yet.
*/
unboundDepsCount_: number;
onBecomeStale_(): void;
isTracing_: TraceMode;
/**
* warn if the derivation has no dependencies after creation/update
*/
requiresObservable_?: boolean;
}
export declare class CaughtException {
cause: any;
constructor(cause: any);
}
export declare function isCaughtException(e: any): e is CaughtException;
/**
* Finds out whether any dependency of the derivation has actually changed.
* If dependenciesState is 1 then it will recalculate dependencies,
* if any dependency changed it will propagate it by changing dependenciesState to 2.
*
* By iterating over the dependencies in the same order that they were reported and
* stopping on the first change, all the recalculations are only called for ComputedValues
* that will be tracked by derivation. That is because we assume that if the first x
* dependencies of the derivation doesn't change then the derivation should run the same way
* up until accessing x-th dependency.
*/
export declare function shouldCompute(derivation: IDerivation): boolean;
export declare function isComputingDerivation(): boolean;
export declare function checkIfStateModificationsAreAllowed(atom: IAtom): void;
export declare function checkIfStateReadsAreAllowed(observable: IObservable): void;
/**
* Executes the provided function `f` and tracks which observables are being accessed.
* The tracking information is stored on the `derivation` object and the derivation is registered
* as observer of any of the accessed observables.
*/
export declare function trackDerivedFunction<T>(derivation: IDerivation, f: () => T, context: any): any;
export declare function clearObserving(derivation: IDerivation): void;
export declare function untracked<T>(action: () => T): T;
export declare function untrackedStart(): IDerivation | null;
export declare function untrackedEnd(prev: IDerivation | null): void;
export declare function allowStateReadsStart(allowStateReads: boolean): boolean;
export declare function allowStateReadsEnd(prev: boolean): void;
/**
* needed to keep `lowestObserverState` correct. when changing from (2 or 1) to 0
*
*/
export declare function changeDependenciesStateTo0(derivation: IDerivation): void;
@@ -0,0 +1,5 @@
{
"ver": "2.0.0",
"uuid": "62978fe5-ca25-43a9-b3d4-3e5d55fdf89d",
"subMetas": {}
}
+112
View File
@@ -0,0 +1,112 @@
import { IDerivation, IObservable, Reaction } from "../internal";
import { ComputedValue } from "./computedvalue";
export declare type IUNCHANGED = {};
export declare class MobXGlobals {
/**
* MobXGlobals version.
* MobX compatiblity with other versions loaded in memory as long as this version matches.
* It indicates that the global state still stores similar information
*
* N.B: this version is unrelated to the package version of MobX, and is only the version of the
* internal state storage of MobX, and can be the same across many different package versions
*/
version: number;
/**
* globally unique token to signal unchanged
*/
UNCHANGED: IUNCHANGED;
/**
* Currently running derivation
*/
trackingDerivation: IDerivation | null;
/**
* Currently running reaction. This determines if we currently have a reactive context.
* (Tracking derivation is also set for temporal tracking of computed values inside actions,
* but trackingReaction can only be set by a form of Reaction)
*/
trackingContext: Reaction | ComputedValue<any> | null;
/**
* Each time a derivation is tracked, it is assigned a unique run-id
*/
runId: number;
/**
* 'guid' for general purpose. Will be persisted amongst resets.
*/
mobxGuid: number;
/**
* Are we in a batch block? (and how many of them)
*/
inBatch: number;
/**
* Observables that don't have observers anymore, and are about to be
* suspended, unless somebody else accesses it in the same batch
*
* @type {IObservable[]}
*/
pendingUnobservations: IObservable[];
/**
* List of scheduled, not yet executed, reactions.
*/
pendingReactions: Reaction[];
/**
* Are we currently processing reactions?
*/
isRunningReactions: boolean;
/**
* Is it allowed to change observables at this point?
* In general, MobX doesn't allow that when running computations and React.render.
* To ensure that those functions stay pure.
*/
allowStateChanges: boolean;
/**
* Is it allowed to read observables at this point?
* Used to hold the state needed for `observableRequiresReaction`
*/
allowStateReads: boolean;
/**
* If strict mode is enabled, state changes are by default not allowed
*/
enforceActions: boolean | "always";
/**
* Spy callbacks
*/
spyListeners: {
(change: any): void;
}[];
/**
* Globally attached error handlers that react specifically to errors in reactions
*/
globalReactionErrorHandlers: ((error: any, derivation: IDerivation) => void)[];
/**
* Warn if computed values are accessed outside a reactive context
*/
computedRequiresReaction: boolean;
/**
* (Experimental)
* Warn if you try to create to derivation / reactive context without accessing any observable.
*/
reactionRequiresObservable: boolean;
/**
* (Experimental)
* Warn if observables are accessed outside a reactive context
*/
observableRequiresReaction: boolean;
disableErrorBoundaries: boolean;
suppressReactionErrors: boolean;
useProxies: boolean;
verifyProxies: boolean;
/**
* False forces all object's descriptors to
* writable: true
* configurable: true
*/
safeDescriptors: boolean;
}
export declare let globalState: MobXGlobals;
export declare function isolateGlobalState(): void;
export declare function getGlobalState(): any;
/**
* For testing purposes only; this will break the internal state of existing observables,
* but can be used to get back at a stable state after throwing errors
*/
export declare function resetGlobalState(): void;
@@ -0,0 +1,5 @@
{
"ver": "2.0.0",
"uuid": "c635e75d-4b5d-40c0-b877-ed547746a404",
"subMetas": {}
}
+45
View File
@@ -0,0 +1,45 @@
import { Lambda, IDerivation, IDerivationState_ } from "../internal";
export interface IDepTreeNode {
name_: string;
observing_?: IObservable[];
}
export interface IObservable extends IDepTreeNode {
diffValue_: number;
/**
* Id of the derivation *run* that last accessed this observable.
* If this id equals the *run* id of the current derivation,
* the dependency is already established
*/
lastAccessedBy_: number;
isBeingObserved_: boolean;
lowestObserverState_: IDerivationState_;
isPendingUnobservation_: boolean;
observers_: Set<IDerivation>;
onBUO(): void;
onBO(): void;
onBUOL: Set<Lambda> | undefined;
onBOL: Set<Lambda> | undefined;
}
export declare function hasObservers(observable: IObservable): boolean;
export declare function getObservers(observable: IObservable): Set<IDerivation>;
export declare function addObserver(observable: IObservable, node: IDerivation): void;
export declare function removeObserver(observable: IObservable, node: IDerivation): void;
export declare function queueForUnobservation(observable: IObservable): void;
/**
* Batch starts a transaction, at least for purposes of memoizing ComputedValues when nothing else does.
* During a batch `onBecomeUnobserved` will be called at most once per observable.
* Avoids unnecessary recalculations.
*/
export declare function startBatch(): void;
export declare function endBatch(): void;
export declare function reportObserved(observable: IObservable): boolean;
/**
* NOTE: current propagation mechanism will in case of self reruning autoruns behave unexpectedly
* It will propagate changes to observers from previous run
* It's hard or maybe impossible (with reasonable perf) to get it right with current approach
* Hopefully self reruning autoruns aren't a feature people should depend on
* Also most basic use cases should be ok
*/
export declare function propagateChanged(observable: IObservable): void;
export declare function propagateChangeConfirmed(observable: IObservable): void;
export declare function propagateMaybeChanged(observable: IObservable): void;
@@ -0,0 +1,5 @@
{
"ver": "2.0.0",
"uuid": "259a7031-1e0d-4e2e-9049-f10290d5e3ac",
"subMetas": {}
}
+62
View File
@@ -0,0 +1,62 @@
import { IDerivation, IDerivationState_, IObservable, Lambda, TraceMode } from "../internal";
/**
* Reactions are a special kind of derivations. Several things distinguishes them from normal reactive computations
*
* 1) They will always run, whether they are used by other computations or not.
* This means that they are very suitable for triggering side effects like logging, updating the DOM and making network requests.
* 2) They are not observable themselves
* 3) They will always run after any 'normal' derivations
* 4) They are allowed to change the state and thereby triggering themselves again, as long as they make sure the state propagates to a stable state in a reasonable amount of iterations.
*
* The state machine of a Reaction is as follows:
*
* 1) after creating, the reaction should be started by calling `runReaction` or by scheduling it (see also `autorun`)
* 2) the `onInvalidate` handler should somehow result in a call to `this.track(someFunction)`
* 3) all observables accessed in `someFunction` will be observed by this reaction.
* 4) as soon as some of the dependencies has changed the Reaction will be rescheduled for another run (after the current mutation or transaction). `isScheduled` will yield true once a dependency is stale and during this period
* 5) `onInvalidate` will be called, and we are back at step 1.
*
*/
export interface IReactionPublic {
dispose(): void;
trace(enterBreakPoint?: boolean): void;
}
export interface IReactionDisposer {
(): void;
$mobx: Reaction;
}
export declare class Reaction implements IDerivation, IReactionPublic {
name_: string;
private onInvalidate_;
private errorHandler_?;
requiresObservable_: boolean;
observing_: IObservable[];
newObserving_: IObservable[];
dependenciesState_: IDerivationState_;
diffValue_: number;
runId_: number;
unboundDepsCount_: number;
isDisposed_: boolean;
isScheduled_: boolean;
isTrackPending_: boolean;
isRunning_: boolean;
isTracing_: TraceMode;
constructor(name_: string, onInvalidate_: () => void, errorHandler_?: ((error: any, derivation: IDerivation) => void) | undefined, requiresObservable_?: boolean);
onBecomeStale_(): void;
schedule_(): void;
isScheduled(): boolean;
/**
* internal, use schedule() if you intend to kick off a reaction
*/
runReaction_(): void;
track(fn: () => void): void;
reportExceptionInDerivation_(error: any): void;
dispose(): void;
getDisposer_(): IReactionDisposer;
toString(): string;
trace(enterBreakPoint?: boolean): void;
}
export declare function onReactionError(handler: (error: any, derivation: IDerivation) => void): Lambda;
export declare function runReactions(): void;
export declare const isReaction: (x: any) => x is Reaction;
export declare function setReactionScheduler(fn: (f: () => void) => void): void;
@@ -0,0 +1,5 @@
{
"ver": "2.0.0",
"uuid": "639733f7-5764-4d38-aa3a-aea86a3d2a83",
"subMetas": {}
}
+37
View File
@@ -0,0 +1,37 @@
import { IComputedDidChange } from "./computedvalue";
import { IValueDidChange, IBoxDidChange } from "../types/observablevalue";
import { IObjectDidChange } from "../types/observableobject";
import { IArrayDidChange } from "../types/observablearray";
import { Lambda, ISetDidChange, IMapDidChange } from "../internal";
export declare function isSpyEnabled(): boolean;
export declare type PureSpyEvent = {
type: "action";
name: string;
object: unknown;
arguments: unknown[];
} | {
type: "scheduled-reaction";
name: string;
} | {
type: "reaction";
name: string;
} | {
type: "error";
name: string;
message: string;
error: string;
} | IComputedDidChange<unknown> | IObjectDidChange<unknown> | IArrayDidChange<unknown> | IMapDidChange<unknown, unknown> | ISetDidChange<unknown> | IValueDidChange<unknown> | IBoxDidChange<unknown> | {
type: "report-end";
spyReportEnd: true;
time?: number;
};
declare type SpyEvent = PureSpyEvent & {
spyReportStart?: true;
};
export declare function spyReport(event: SpyEvent): void;
export declare function spyReportStart(event: PureSpyEvent): void;
export declare function spyReportEnd(change?: {
time?: number;
}): void;
export declare function spy(listener: (change: SpyEvent) => void): Lambda;
export {};
@@ -0,0 +1,5 @@
{
"ver": "2.0.0",
"uuid": "86e2f566-eaf3-4d09-a093-2a2cc5bda60c",
"subMetas": {}
}