Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Mobx4 #1321

Merged
merged 204 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
204 commits
Select commit Hold shift + click to select a range
6d71f48
Don't try/catch errors when globalstate.disableErrorBoundaries is on.
NaridaL Dec 4, 2017
2f32c15
Allow autorunAsync to take a debouncing function in addition to a delay
TrentHouliston Jan 15, 2018
7510b81
Add unit test for autorunAsync scheduling function
TrentHouliston Jan 17, 2018
787bf5a
Make types more explicit
TrentHouliston Jan 17, 2018
4077755
Increase time to execute the test for travis
TrentHouliston Jan 17, 2018
9be342e
Update changelog
TrentHouliston Jan 17, 2018
e805559
killed old modifiers
mweststrate Jan 24, 2018
31744bb
Dropped bower support. Fixes #1263
mweststrate Jan 24, 2018
49514b1
Implemented #1089: throw if there are multiple mobx instances
mweststrate Jan 24, 2018
4bcdb97
Fixed exports
mweststrate Jan 24, 2018
53edb5c
Dropped `.extras` namespace
mweststrate Jan 24, 2018
cef553b
Fixing tests
mweststrate Jan 24, 2018
cc5ccd4
Fixed tests :)
mweststrate Jan 24, 2018
df6d17c
`observable(value)` no longer accepts primitives. Use `observable.bo…
mweststrate Jan 24, 2018
b0ce8bc
introduced `isObservableProp` and `isComputedProp`
mweststrate Jan 25, 2018
dd21bc1
Jest cleanup: don't log errors as side effects
mweststrate Jan 25, 2018
15d6a1e
Removed all remaining leaking console logs
mweststrate Jan 25, 2018
dad19a2
Snapshotted whyrun test
mweststrate Jan 25, 2018
78df300
Fixed newline issues
mweststrate Jan 25, 2018
208390f
Whyrun will now print why a computed value / reaction is re-running
mweststrate Jan 25, 2018
b0f37df
fixed state sharing tests
mweststrate Jan 25, 2018
0d4e0a7
Merged in master
mweststrate Feb 6, 2018
3c55d85
Killed whyRun
mweststrate Feb 6, 2018
350efdc
Created a MobX4 todo list
mweststrate Feb 8, 2018
967c557
Improved error messages
mweststrate Feb 15, 2018
53a3d31
upgraded to TS 2.7
mweststrate Feb 15, 2018
6f1829e
cleaned up reaction api
mweststrate Feb 16, 2018
60507a4
Merge branch 'master' of https://github.com/TrentHouliston/mobx into …
mweststrate Feb 16, 2018
6abdb58
Merged #1305 into mobx4, added scheduler support for reactions as well
mweststrate Feb 16, 2018
8390609
Made decorators work with Babel 7 (loose mode only)
mweststrate Feb 16, 2018
9f5eda9
Disabled TS / Babel perf tests by default
mweststrate Feb 16, 2018
51ded3a
Fixed performance tests
mweststrate Feb 16, 2018
1e95ad4
fixed dependency issue with decorators
mweststrate Feb 16, 2018
f5cae00
extendObservable now extends objects in a single batch
mweststrate Feb 17, 2018
26a8b55
Introduced `keys`. Observable objects now have an observable keyset
mweststrate Feb 17, 2018
8b1ae86
Fixed a regression in toJS
mweststrate Feb 17, 2018
e510603
Fixed bug in `extendObservable`; values were not assigned to compute…
mweststrate Feb 17, 2018
aac6729
WIP `values` / `set` / `remove`
mweststrate Feb 17, 2018
5292254
Added tests for `set`, `remove`, `values` and `keys` with several da…
mweststrate Feb 21, 2018
8b4b650
Removed a todo
mweststrate Feb 23, 2018
bbc31c8
Introduced `decorate`
mweststrate Feb 23, 2018
d96e0e0
Removed debugger statement
mweststrate Feb 23, 2018
51d6f74
Added typings & type test for decorate
mweststrate Feb 23, 2018
0cb3568
Migrated spy tests to use snapshots
mweststrate Feb 23, 2018
7f04ff5
spy events now report the name of the observable they are invoked upon.
mweststrate Feb 23, 2018
2c88f33
decorate now supports plain objects as well
mweststrate Feb 23, 2018
c0d4e8f
fixed error handling tests, many were broken...
mweststrate Feb 23, 2018
5135530
WIP improving onbecome(un)observed
mweststrate Feb 23, 2018
701102c
onBecome(Un)Observed are now only triggered for observables that are …
mweststrate Feb 23, 2018
65ef22a
Introduced `createAtom`, little cleanup
mweststrate Feb 23, 2018
8f8cb22
Finished cleanup of createTransformer
mweststrate Feb 23, 2018
c79f843
Map / array api's now properly return iterator objects, instead of co…
mweststrate Feb 26, 2018
f758123
Fixed circular dependency problem with Atoms
mweststrate Feb 26, 2018
1b4863a
Fixed bug in hook disposal
mweststrate Feb 26, 2018
bf7cca8
Fixed incorrect test
mweststrate Feb 26, 2018
8e52f9f
Slightly faster object initialization
mweststrate Feb 26, 2018
a040981
Set up envify for conditional compilation
mweststrate Feb 26, 2018
fd8ab30
Introduced non-production only code for smaller / faster prod bundles
mweststrate Feb 26, 2018
3fcb72d
Cleaned up unused stuff
mweststrate Feb 26, 2018
1ab5ac5
Removed `expr`, now part of mobx-utils
mweststrate Feb 27, 2018
b55c7c0
Added prettier config file
mweststrate Feb 27, 2018
1a74a29
Moved `createTransformer` to mobx-utils package
mweststrate Feb 27, 2018
e79f794
Cleaned a bunch of TODO's
mweststrate Feb 27, 2018
65a0b98
More cleanup!
mweststrate Feb 27, 2018
856aa26
Replaces `useStrict` with `configure`
mweststrate Feb 27, 2018
db3ff5b
Introduced `mobx.configure({ warnOnUnsafeComputationReads: true })`, …
mweststrate Feb 27, 2018
1a4defd
isolateGlobalState has now become a configuration option
mweststrate Feb 27, 2018
8c2f648
Added test to reproduce #1122
mweststrate Feb 27, 2018
ea8f603
Added test to confirm #1148 is fixed
mweststrate Feb 27, 2018
1cde211
Merged changes
mweststrate Feb 27, 2018
8691169
Added unit tests to verify #1241
mweststrate Feb 27, 2018
56a1dcd
Little rename
mweststrate Feb 27, 2018
71f702f
Fixed unnecessary warning when reading map entries
mweststrate Feb 27, 2018
f4601a2
Fixed some typings in typescript tests
mweststrate Feb 27, 2018
1e7539e
Simplified the way computed values are constructed
mweststrate Feb 27, 2018
3fc851c
Added jsconfig file to supress warnings on decorators in babel
mweststrate Feb 27, 2018
54269bb
introduced `@computed(options)`. also introduced `requiresReaction`
mweststrate Feb 27, 2018
601f93d
removing some dead code
mweststrate Feb 27, 2018
a857dca
Implemented #1301: specify onError handler on reactions as option
mweststrate Feb 27, 2018
4e5c6cf
Merge remote-tracking branch 'origin/master' into mobx4
mweststrate Feb 27, 2018
0344e90
Fixed a potential bug in `when`
mweststrate Feb 27, 2018
f111eba
Merge branch 'master' into mobx4
mweststrate Feb 27, 2018
0fa557d
Fixed some compile errors
mweststrate Feb 27, 2018
1b2d947
Fixed regression tests after merging master
mweststrate Feb 27, 2018
b157f41
alpha 1
mweststrate Feb 28, 2018
7931d3b
extendObservable only accepts one arg
mweststrate Feb 28, 2018
e953595
extendObservable now takes an options object
mweststrate Feb 28, 2018
ef4bc3f
Fixed regression performance drop
mweststrate Feb 28, 2018
515e4fc
Fixed compilation error
mweststrate Feb 28, 2018
5833fe3
Merge remote-tracking branch 'origin/master' into mobx4
mweststrate Feb 28, 2018
9443b69
Stop exposing `deepEqual` to outside world. Use `comparer.structural`…
mweststrate Feb 28, 2018
9b70833
Merged #817
mweststrate Feb 28, 2018
04d3fe6
Merged #800
mweststrate Feb 28, 2018
e3e4907
Fixed tests
mweststrate Feb 28, 2018
32caff5
Fixed compilation error
mweststrate Feb 28, 2018
458f32e
More tests
mweststrate Feb 28, 2018
259e87c
Map definition is now based on es2015.collection
mweststrate Feb 28, 2018
be4ad7f
Enabled toJS to return Maps
mweststrate Feb 28, 2018
2dba343
Merge branch 'maps' into mobx4
mweststrate Feb 28, 2018
3bde5bc
fixed performance tests
mweststrate Feb 28, 2018
ac73be4
Simplified computed api
mweststrate Mar 1, 2018
1d5250e
naive extendObservable implementation
mweststrate Mar 1, 2018
0bf1c41
Made extendObservable stricter
mweststrate Mar 1, 2018
e4ff32d
observables test now passes
mweststrate Mar 1, 2018
6c41659
Added unit test for decorate
mweststrate Mar 1, 2018
5e31c81
Fixed some tests
mweststrate Mar 1, 2018
7695f97
`set` can now take a bag of arguments
mweststrate Mar 1, 2018
33023e8
extend now supports decorators
mweststrate Mar 1, 2018
f1b09ab
`observable.object` now accepts decorators
mweststrate Mar 1, 2018
4dabbee
Fixed most tests
mweststrate Mar 1, 2018
9fd1366
Structure etc works again
mweststrate Mar 1, 2018
bb5edeb
Killed modifiers :)
mweststrate Mar 1, 2018
ccfec38
test cleanup
mweststrate Mar 1, 2018
ea85186
Tiny improvements
mweststrate Mar 1, 2018
73bf899
Deprecated shallow api
mweststrate Mar 1, 2018
820286a
alpha 2
mweststrate Mar 1, 2018
f5a1892
todo's
mweststrate Mar 1, 2018
aaf71bd
WIP: optimizing initialization by skipping decorator
mweststrate Mar 1, 2018
21a1880
more notes
mweststrate Mar 1, 2018
be049dc
some cleanup
mweststrate Mar 1, 2018
d632313
Little restructuring
mweststrate Mar 1, 2018
b4dee43
some fancy tests
mweststrate Mar 2, 2018
42320c8
better decorators underway(?)
mweststrate Mar 2, 2018
7ab27b2
Improved typings of object api
mweststrate Mar 2, 2018
c62631e
Make inheritance work
mweststrate Mar 2, 2018
c8938eb
Fixed auto property initialization
mweststrate Mar 2, 2018
85f7c1c
Improve error on illegal property access
mweststrate Mar 2, 2018
2e9e709
Updated tests for illegal access
mweststrate Mar 2, 2018
bde7707
all observable decorators now use decorators2
mweststrate Mar 2, 2018
9e3877a
All observable decorators now use decorators2
mweststrate Mar 2, 2018
bc936bd
Rewriting computed
mweststrate Mar 2, 2018
36d81fa
Computed now goes through decorators2
mweststrate Mar 2, 2018
2c6c4a4
Cleaner property creation
mweststrate Mar 2, 2018
b7cf088
Support for non-enumerable props
mweststrate Mar 2, 2018
3408d99
Made sure computed props are declared on proto
mweststrate Mar 2, 2018
e5eda2d
Actions on new decorator system part 1
mweststrate Mar 2, 2018
75ecfa5
More efficient action instantiation
mweststrate Mar 2, 2018
4c90629
Added bound action decorator
mweststrate Mar 2, 2018
e73e1c7
Simplifications
mweststrate Mar 2, 2018
92d4e37
fixed compilation errors
mweststrate Mar 2, 2018
8b7e847
make mobx.observable also accept options
mweststrate Mar 3, 2018
78446a5
simplified extendObservable / property creation short-circuiting
mweststrate Mar 3, 2018
3bbfcb5
added support for action short-circuiting
mweststrate Mar 3, 2018
0613553
Cleanup
mweststrate Mar 3, 2018
f8008bb
cleanup
mweststrate Mar 3, 2018
390a852
Merge branch 'better-decorators' into improved-observable-api
mweststrate Mar 3, 2018
5014fbe
Merge branch 'improved-observable-api' into mobx4
mweststrate Mar 3, 2018
ec7a543
bugfix in bound actions
mweststrate Mar 3, 2018
eab222e
Cleanup in actions
mweststrate Mar 5, 2018
f80fd49
Processing TODO's
mweststrate Mar 5, 2018
c4a997a
Clean up decrators
mweststrate Mar 5, 2018
20d8dbf
cleanup in observable decorator typings
mweststrate Mar 5, 2018
5f87cdf
Setting new props now preserves existing enhancer
mweststrate Mar 5, 2018
66af4e9
object key additions and removals are now interceptable and observable
mweststrate Mar 5, 2018
cce1468
More todos' processed
mweststrate Mar 5, 2018
4c30db4
processing more todo's!
mweststrate Mar 5, 2018
8c63aad
Merged #1361
mweststrate Mar 5, 2018
9271180
Fixed compile errors
mweststrate Mar 5, 2018
afb3cc1
Added some environment checks
mweststrate Mar 5, 2018
b5f1155
Cleaned up api a bit further
mweststrate Mar 5, 2018
441a315
Throw when using old apis
mweststrate Mar 5, 2018
c6e3088
cleaned up public api
mweststrate Mar 5, 2018
e5f4cf3
Todo's, fixed issues with actions
mweststrate Mar 5, 2018
f539f60
All todo's processed now :)
mweststrate Mar 5, 2018
eaa7f33
Fixed typings issue
mweststrate Mar 5, 2018
6ec78b7
Added `timeout` option to `when`
mweststrate Mar 5, 2018
51a3b88
When will now return a promise if invoked without effect function
mweststrate Mar 5, 2018
168997e
Introduced `flow`
mweststrate Mar 5, 2018
4a522fd
Fixes in typings
mweststrate Mar 5, 2018
f17a983
Little cleanups
mweststrate Mar 5, 2018
ab8680b
Updated flow typings
mweststrate Mar 5, 2018
16b658d
Introduced `has`, `get`
mweststrate Mar 5, 2018
1e81a9f
Release notes stuff
mweststrate Mar 5, 2018
9644470
Updated changelog
mweststrate Mar 6, 2018
a301b71
Fix: computed properties are not considered part of the `keys` collec…
mweststrate Mar 6, 2018
0c59f95
Typing fixes
mweststrate Mar 6, 2018
95c20ec
Typing fix
mweststrate Mar 7, 2018
9125e61
Added vs code config to repo, so others can debug with vscode
mweststrate Mar 7, 2018
3e45406
Fixed module initialization issue
mweststrate Mar 7, 2018
b51c642
code cleanup
mweststrate Mar 7, 2018
bc677c2
Fixed module initialization
mweststrate Mar 7, 2018
8a94211
Beta 2
mweststrate Mar 7, 2018
4e6f084
Changelog fixes
mweststrate Mar 7, 2018
9ce985f
Reproduced and fixed #1376
mweststrate Mar 8, 2018
207c814
Removed unused function
mweststrate Mar 8, 2018
9747018
Removed `dom` stdlib. Fixed some typing issues
mweststrate Mar 8, 2018
c47a05a
Beta 3
mweststrate Mar 8, 2018
d899917
Fixed #1379, CRA refusing to compile mobx.module.js
mweststrate Mar 8, 2018
a65d5f5
Fixed additional compilation issues
mweststrate Mar 8, 2018
565be8a
introduced canceellable flows
mweststrate Mar 9, 2018
9984b12
Additional test
mweststrate Mar 9, 2018
a8354cd
Deprecated array.move and findIndex arguments
mweststrate Mar 10, 2018
ac3f948
Updated readme to MobX 4
mweststrate Mar 10, 2018
5525473
Better error handling setup for reactions
mweststrate Mar 12, 2018
de6c018
Cleaned up `ref.struct` / `deep.struct` decorators
mweststrate Mar 12, 2018
12c7c6f
Merge branch 'master' into mobx4
mweststrate Mar 12, 2018
ac5fe77
Modifiers cleanup
mweststrate Mar 12, 2018
36d690c
Iterator cleanup
mweststrate Mar 12, 2018
b0dbe24
Removed `@flow` decorator; as it's type signature is misleading
mweststrate Mar 12, 2018
9e805b5
Use `generator.return` rather than `throw` for cancellation
mweststrate Mar 12, 2018
fe8131c
Promises cancel recursively
mweststrate Mar 12, 2018
5ab68b1
Anything can now be yielded from flows
mweststrate Mar 12, 2018
cbfbbca
Added support for async iterators
mweststrate Mar 12, 2018
298ce84
Merge branch 'cancellable-flows' into mobx4
mweststrate Mar 12, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# 4.0.0

## Breaking changes

* The `extras.` namespace has been dropped to enable tree-shaking non-used MobX features. All methods that where there originally are now exported at top level. If they are part of the official public API (you are encouraged to use them) they are exported as is. If they are experimental or somehow internal (you are discouraged to use them), they are prefixed with `_`.
* If there are multiple mobx instances active in a single project, an exception will be thrown. Previously only a warning was printed. Fixes #1098. For details, see [#1082](https://github.com/mobxjs/mobx/issues/1082).
* Dropped the `shareGlobalState` feature. Instead, projects should be setup properly and it is up to the hosting package to make sure that there is only one MobX instance
* Dropped bower support. Fixes #1263
* The `spyReportStart`, `spyReportEnd`, `spyReport` and `isSpyEnabled` are no longer public. It is no longer possible to emit custom spy events as to avoid confusing in listeners what the possible set of events is.
* Dropped `isStrictModeEnabled`
* `observable(value)` will only succeed if it can turn the value into an observable data structure (a Map, Array or observable object). But it will no longer create an observable box for other values to avoid confusion. Call `observable.box(value)` explictly in such cases.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that @observable foo = 5 won't work anymore and we'd need to @observable.box foo = 5?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaik this is just for the function API

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, I think @observable.box foo = 5 would be tedious to do every time since it's a pretty common use case for me to have observable value properties.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjamingr correct, it is only for the function version, to make the result more predicatable (previously you would 'suddenly' get a box around your value if the value itself could not be made observable)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mweststrate, any update on using proxies for arrays? :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjamingr yes, after cleaning up everything in mobx 4, I will try to publish mobx 5, which is the same but with proxies in a foreseeable time

* `isComputed` and `isObservable` no longer accept a property as second argument. Instead use `isComputedProp` and `isObservableProp`.
* Removed `whyRun`, use `trace` instead

## Non breaking changes

* Dropped already deprecated and broken `default` export that made it impossible to tree-shake mobx. Make sure you always use `import { x } from "mobx"` and not `import x from "mobx"`
* Dropped already deprecated toplevel `map` function
* Killed the already deprecated modifiers `asFlat` etc. If you war still using this, see the MobX 2 -> 3 migration notes.

# 3.5.0/1

* Introduced `trace` for easier debugging of reactions / computed values. See the [docs](https://mobx.js.org/best/trace.html) for details.
Expand Down
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,21 +300,14 @@ And finally kudos for all the people that believed in, tried, validated and even
* Use `npm test` to run the basic test suite, `npm run coverage` for the test suite with coverage and `npm run perf` for the performance tests.

## Flow support
MobX ships with [flow typings](flow-typed/mobx.js). Flow will automatically include them when you import mobx modules. Although you **do not** need to import the types explicitly, you can still do it like this: `import type { ... } from 'mobx'`.
MobX ships with [flow typings](flow-typed/mobx.js). Flow will automatically include them when you import mobx modules. Although you **do not** need to import the types explicitly, you can still do it like this: `import type { ... } from 'mobx'`.

To use the [flow typings](flow-typed/mobx.js) shipped with MobX:

* In `.flowconfig`, you **cannot** ignore `node_modules`.
* In `.flowconfig`, you **cannot** import it explicitly in the `[libs]` section.
* You **do not** need to install library definition using [flow-typed](https://github.com/flowtype/flow-typed).

## Bower support

Bower support is available through the infamous unpkg.com:
`bower install https://unpkg.com/mobx/bower.zip`

Then use `lib/mobx.umd.js` or `lib/mobx.umd.min.js`

## MobX was formerly known as Mobservable.

See the [changelog](https://github.com/mobxjs/mobx/blob/master/CHANGELOG.md#200) for all the details about `mobservable` to `mobx`.
Expand Down
36 changes: 0 additions & 36 deletions bower.json

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
},
"files": [
"lib",
"LICENSE",
"bower.json"
"LICENSE"
],
"homepage": "https://mobx.js.org/",
"devDependencies": {
Expand Down
17 changes: 16 additions & 1 deletion src/api/iscomputed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { isObservableObject } from "../types/observableobject"
import { getAtom } from "../types/type-utils"
import { isComputedValue } from "../core/computedvalue"
import { fail } from "../utils/utils"

export function isComputed(value, property?: string): boolean {
export function _isComputed(value, property?: string): boolean {
if (value === null || value === undefined) return false
if (property !== undefined) {
if (isObservableObject(value) === false) return false
Expand All @@ -12,3 +13,17 @@ export function isComputed(value, property?: string): boolean {
}
return isComputedValue(value)
}

export function isComputed(value: any): boolean {
if (arguments.length > 1)
return fail(
`isComputed expects only 1 argument. Use isObsevableProp to inspect the observability of a property`
)
return _isComputed(value)
}

export function isComputedProp(value: any, propName: string): boolean {
if (typeof propName !== "string")
return fail(`isComputed expected a property name as second argument`)
return _isComputed(value, propName)
}
21 changes: 15 additions & 6 deletions src/api/isobservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import { isAtom } from "../core/atom"
import { isComputedValue } from "../core/computedvalue"
import { isReaction } from "../core/reaction"
import { getMessage } from "../utils/messages"
import { fail } from "../utils/utils"

/**
* Returns true if the provided value is reactive.
* @param value object, function or array
* @param property if property is specified, checks whether value.property is reactive.
*/
export function isObservable(value, property?: string): boolean {
function _isObservable(value, property?: string): boolean {
if (value === null || value === undefined) return false
if (property !== undefined) {
if (isObservableArray(value) || isObservableMap(value)) throw new Error(getMessage("m019"))
Expand All @@ -30,3 +26,16 @@ export function isObservable(value, property?: string): boolean {
isComputedValue(value)
)
}

export function isObservable(value: any): boolean {
if (arguments.length > 1)
return fail(
`isObservable expects only 1 argument. Use isObsevableProp to inspect the observability of a property`
)
return _isObservable(value)
}

export function isObservableProp(value: any, propName: string): boolean {
if (typeof propName !== "string") return fail(`expected a property name as second argument`)
return _isObservable(value, propName)
}
17 changes: 4 additions & 13 deletions src/api/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,18 @@ function createObservable(v: any = undefined) {
if (res !== v) return res

// otherwise, just box it
return observable.box(v)
fail(
`The provided value could not be converted into an observable. If you want just create an observable reference to the object use 'observable.box(value)'`
)
}

export interface IObservableFactory {
// observable overloads
<T>(): IObservableValue<T>
<T>(wrapped: IModifierDescriptor<T>): T
(target: Object, key: string, baseDescriptor?: PropertyDescriptor): any
<T>(value: T[]): IObservableArray<T>
(value: string): IObservableValue<string>
(value: boolean): IObservableValue<boolean>
(value: number): IObservableValue<number>
(value: Date): IObservableValue<Date>
(value: RegExp): IObservableValue<RegExp>
(value: Function): IObservableValue<Function>
<T>(value: null | undefined): IObservableValue<T>
(value: null | undefined): IObservableValue<any>
(): IObservableValue<any>
<T>(value: IMap<string | number | boolean, T>): ObservableMap<T>
<T extends Object>(value: T): T & IObservableObject
<T>(value: T): IObservableValue<T>
}

export interface IObservableFactories {
Expand All @@ -82,7 +73,7 @@ export interface IObservableFactories {
* Decorator that creates an observable that only observes the references, but doesn't try to turn the assigned value into an observable.ts.
*/
ref(target: Object, property: string, descriptor?: PropertyDescriptor): any
ref<T>(initialValue: T): T;
ref<T>(initialValue: T): T

/**
* Decorator that creates an observable converts its value (objects, maps or arrays) into a shallow observable structure
Expand Down
18 changes: 1 addition & 17 deletions src/api/whyrun.ts → src/api/trace.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import { globalState } from "../core/globalstate"
import { isComputedValue } from "../core/computedvalue"
import { isReaction } from "../core/reaction"
import { getAtom } from "../types/type-utils"
import { fail, deprecated } from "../utils/utils"
import { getMessage } from "../utils/messages"
import { fail } from "../utils/utils"
import { TraceMode } from "../core/derivation"

function log(msg: string): string {
console.log(msg)
return msg
}

export function whyRun(thing?: any, prop?: string) {
deprecated("`whyRun` is deprecated in favor of `trace`")
thing = getAtomFromArgs(arguments)
if (!thing) return log(getMessage("m024"))
if (isComputedValue(thing) || isReaction(thing)) return log(thing.whyRun())
return fail(getMessage("m025"))
}

export function trace(thing?: any, prop?: string, enterBreakPoint?: boolean): void
export function trace(thing?: any, enterBreakPoint?: boolean): void
export function trace(enterBreakPoint?: boolean): void
Expand Down
32 changes: 1 addition & 31 deletions src/core/computedvalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
invariant,
Lambda,
unique,
joinStrings,
primitiveSymbol,
toPrimitive
} from "../utils/utils"
Expand Down Expand Up @@ -78,6 +77,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
__mapid = "#" + getNextId()
protected value: T | undefined | CaughtException = new CaughtException(null)
name: string
triggeredBy: string
isComputing: boolean = false // to check for cycles
isRunningSetter: boolean = false
setter: (value: T) => void
Expand Down Expand Up @@ -242,36 +242,6 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
valueOf(): T {
return toPrimitive(this.get())
}

whyRun() {
const isTracking = Boolean(globalState.trackingDerivation)
const observing = unique(this.isComputing ? this.newObserving! : this.observing).map(
(dep: any) => dep.name
)
const observers = unique(getObservers(this).map(dep => dep.name))
return (
`
WhyRun? computation '${this.name}':
* Running because: ${isTracking
? "[active] the value of this computation is needed by a reaction"
: this.isComputing
? "[get] The value of this computed was requested outside a reaction"
: "[idle] not running at the moment"}
` +
(this.dependenciesState === IDerivationState.NOT_TRACKING
? getMessage("m032")
: ` * This computation will re-run if any of the following observables changes:
${joinStrings(observing)}
${this.isComputing && isTracking
? " (... or any observable accessed during the remainder of the current run)"
: ""}
${getMessage("m038")}

* If the outcome of this computation changes, the following observers will be re-run:
${joinStrings(observers)}
`)
)
}
}

ComputedValue.prototype[primitiveSymbol()] = ComputedValue.prototype.valueOf
Expand Down
8 changes: 4 additions & 4 deletions src/core/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface IDerivation extends IDepTreeNode {
*/
unboundDepsCount: number
__mapid: string
onBecomeStale()
onBecomeStale(): void
isTracing: TraceMode
}

Expand All @@ -59,7 +59,7 @@ export class CaughtException {
}
}

export function isCaughtException(e): e is CaughtException {
export function isCaughtException(e: any): e is CaughtException {
return e instanceof CaughtException
}

Expand Down Expand Up @@ -128,7 +128,7 @@ export function checkIfStateModificationsAreAllowed(atom: IAtom) {
* The tracking information is stored on the `derivation` object and the derivation is registered
* as observer of any of the accessed observables.
*/
export function trackDerivedFunction<T>(derivation: IDerivation, f: () => T, context) {
export function trackDerivedFunction<T>(derivation: IDerivation, f: () => T, context: any) {
// pre allocate array allocation + room for variation in deps
// array will be trimmed by bindDependencies
changeDependenciesStateTo0(derivation)
Expand All @@ -155,7 +155,7 @@ export function trackDerivedFunction<T>(derivation: IDerivation, f: () => T, con
*/
function bindDependencies(derivation: IDerivation) {
// invariant(derivation.dependenciesState !== IDerivationState.NOT_TRACKING, "INTERNAL ERROR bindDependencies expects derivation.dependenciesState !== -1");

derivation.triggeredBy = ""
const prevObserving = derivation.observing
const observing = (derivation.observing = derivation.newObserving!)
let lowestNewObservingDerivationState = IDerivationState.UP_TO_DATE
Expand Down
35 changes: 4 additions & 31 deletions src/core/globalstate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getGlobal, deprecated } from "../utils/utils"
import { getGlobal, deprecated, fail } from "../utils/utils"
import { IDerivation } from "./derivation"
import { Reaction } from "./reaction"
import { IObservable } from "./observable"
Expand Down Expand Up @@ -88,9 +88,7 @@ export class MobXGlobals {

export let globalState: MobXGlobals = new MobXGlobals()

let shareGlobalStateCalled = false
let runInIsolationCalled = false
let warnedAboutMultipleInstances = false

{
const global = getGlobal()
Expand All @@ -99,10 +97,9 @@ let warnedAboutMultipleInstances = false
} else {
global.__mobxInstanceCount++
setTimeout(() => {
if (!shareGlobalStateCalled && !runInIsolationCalled && !warnedAboutMultipleInstances) {
warnedAboutMultipleInstances = true
console.warn(
"[mobx] Warning: there are multiple mobx instances active. This might lead to unexpected results. See https://github.com/mobxjs/mobx/issues/1082 for details."
if (!runInIsolationCalled) {
fail(
"There are multiple mobx instances active. This might lead to unexpected results. See https://github.com/mobxjs/mobx/issues/1082 for details."
)
}
})
Expand All @@ -114,34 +111,10 @@ export function isolateGlobalState() {
getGlobal().__mobxInstanceCount--
}

export function shareGlobalState() {
// TODO: remove in 4.0; just use peer dependencies instead.
deprecated(
"Using `shareGlobalState` is not recommended, use peer dependencies instead. See https://github.com/mobxjs/mobx/issues/1082 for details."
)
shareGlobalStateCalled = true
const global = getGlobal()
const ownState = globalState

/**
* Backward compatibility check
*/
if (global.__mobservableTrackingStack || global.__mobservableViewStack)
throw new Error("[mobx] An incompatible version of mobservable is already loaded.")
if (global.__mobxGlobal && global.__mobxGlobal.version !== ownState.version)
throw new Error("[mobx] An incompatible version of mobx is already loaded.")
if (global.__mobxGlobal) globalState = global.__mobxGlobal
else global.__mobxGlobal = ownState
}

export function getGlobalState(): any {
return globalState
}

export function registerGlobals() {
// no-op to make explicit why this file is loaded
}

/**
* 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
Expand Down
4 changes: 2 additions & 2 deletions src/core/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface IObservable extends IDepTreeNode {
observers: IDerivation[] // maintain _observers in raw array for for way faster iterating in propagation.
observersIndexes: {} // map derivation.__mapid to _observers.indexOf(derivation) (see removeObserver)

onBecomeUnobserved()
onBecomeUnobserved(): void
}

export function hasObservers(observable: IObservable): boolean {
Expand Down Expand Up @@ -157,7 +157,7 @@ export function reportObserved(observable: IObservable) {
}
}

function invariantLOS(observable: IObservable, msg) {
function invariantLOS(observable: IObservable, msg: string) {
// it's expensive so better not run it in produciton. but temporarily helpful for testing
const min = getObservers(observable).reduce((a, b) => Math.min(a, b.dependenciesState), 2)
if (min >= observable.lowestObserverState) return // <- the only assumption about `lowestObserverState`
Expand Down
Loading