Skip to content

Commit

Permalink
Added some missing bits and pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Dec 24, 2018
1 parent e40fedc commit e86dc6b
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 13 deletions.
17 changes: 14 additions & 3 deletions src/api/become-observed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import {
Lambda,
ObservableMap,
fail,
getAtom
getAtom,
ObservableSet
} from "../internal"

export function onBecomeObserved(
value: IObservable | IComputedValue<any> | IObservableArray<any> | ObservableMap<any, any>,
value:
| IObservable
| IComputedValue<any>
| IObservableArray<any>
| ObservableMap<any, any>
| ObservableSet<any>,
listener: Lambda
): Lambda
export function onBecomeObserved<K, V = any>(
Expand All @@ -22,7 +28,12 @@ export function onBecomeObserved(thing, arg2, arg3?): Lambda {
}

export function onBecomeUnobserved(
value: IObservable | IComputedValue<any> | IObservableArray<any> | ObservableMap<any, any>,
value:
| IObservable
| IComputedValue<any>
| IObservableArray<any>
| ObservableMap<any, any>
| ObservableSet<any>,
listener: Lambda
): Lambda
export function onBecomeUnobserved<K, V = any>(
Expand Down
7 changes: 6 additions & 1 deletion src/api/intercept-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
isObservableArray,
isObservableMap,
isObservableObject,
isObservableValue
isObservableValue,
ObservableSet
} from "../internal"

export type ReadInterceptor<T> = (value: any) => T
Expand All @@ -23,6 +24,10 @@ export function interceptReads<K, V>(
observableMap: ObservableMap<K, V>,
handler: ReadInterceptor<V>
): Lambda
export function interceptReads<V>(
observableSet: ObservableSet<V>,
handler: ReadInterceptor<V>
): Lambda
export function interceptReads(
object: Object,
property: string,
Expand Down
8 changes: 7 additions & 1 deletion src/api/intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
IValueWillChange,
Lambda,
ObservableMap,
getAdministration
getAdministration,
ObservableSet,
ISetWillChange
} from "../internal"

export function intercept<T>(
Expand All @@ -24,6 +26,10 @@ export function intercept<K, V>(
observableMap: ObservableMap<K, V>,
handler: IInterceptor<IMapWillChange<K, V>>
): Lambda
export function intercept<V>(
observableMap: ObservableSet<V>,
handler: IInterceptor<ISetWillChange<V>>
): Lambda
export function intercept<K, V>(
observableMap: ObservableMap<K, V>,
property: K,
Expand Down
2 changes: 1 addition & 1 deletion src/api/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getEnhancerFromOptions(options: CreateObservableOptions): IEnhancer<any
function createObservable(v: any, arg2?: any, arg3?: any) {
// @observable someProp;
if (typeof arguments[1] === "string") {
return deepDecorator.apply(null, arguments)
return deepDecorator.apply(null, arguments as any)
}

// it is an observable already, done
Expand Down
9 changes: 8 additions & 1 deletion src/api/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
IValueDidChange,
Lambda,
ObservableMap,
getAdministration
getAdministration,
ObservableSet,
ISetDidChange
} from "../internal"

export function observe<T>(
Expand All @@ -22,6 +24,11 @@ export function observe<T>(
listener: (change: IArrayChange<T> | IArraySplice<T>) => void,
fireImmediately?: boolean
): Lambda
export function observe<V>(
observableMap: ObservableSet<V>,
listener: (change: ISetDidChange<V>) => void,
fireImmediately?: boolean
): Lambda
export function observe<K, V>(
observableMap: ObservableMap<K, V>,
listener: (change: IMapDidChange<K, V>) => void,
Expand Down
21 changes: 19 additions & 2 deletions src/api/tojs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
isObservable,
isObservableArray,
isObservableValue,
isObservableMap
isObservableMap,
isObservableSet
} from "../internal"

export type ToJSOptions = {
Expand All @@ -27,7 +28,7 @@ function toJSHelper(source, options: ToJSOptions, __alreadySeen: Map<any, any>)
if (!options.recurseEverything && !isObservable(source)) return source

if (typeof source !== "object") return source

// Directly return null if source is null
if (source === null) return null

Expand All @@ -53,6 +54,22 @@ function toJSHelper(source, options: ToJSOptions, __alreadySeen: Map<any, any>)
return res
}

if (isObservableSet(source) || Object.getPrototypeOf(source) === Set.prototype) {
if (options.exportMapsAsObjects === false) {
const res = cache(__alreadySeen, source, new Set(), options)
source.forEach(value => {
res.add(toJSHelper(value, options!, __alreadySeen))
})
return res
} else {
const res = cache(__alreadySeen, source, [] as any[], options)
source.forEach(value => {
res.push(toJSHelper(value, options!, __alreadySeen))
})
return res
}
}

if (isObservableMap(source) || Object.getPrototypeOf(source) === Map.prototype) {
if (options.exportMapsAsObjects === false) {
const res = cache(__alreadySeen, source, new Map(), options)
Expand Down
11 changes: 8 additions & 3 deletions src/types/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
isObservableArray,
isObservableMap,
isObservableObject,
isReaction
isReaction,
isObservableSet
} from "../internal"

export function getAtom(thing: any, property?: string): IDepTreeNode {
Expand All @@ -21,6 +22,9 @@ export function getAtom(thing: any, property?: string): IDepTreeNode {
)
return (thing as any)[$mobx].atom
}
if (isObservableSet(thing)) {
return (thing as any)[$mobx]
}
if (isObservableMap(thing)) {
const anyThing = thing as any
if (property === undefined) return anyThing._keysAtom
Expand Down Expand Up @@ -66,7 +70,7 @@ export function getAdministration(thing: any, property?: string) {
if (!thing) fail("Expecting some object")
if (property !== undefined) return getAdministration(getAtom(thing, property))
if (isAtom(thing) || isComputedValue(thing) || isReaction(thing)) return thing
if (isObservableMap(thing)) return thing
if (isObservableMap(thing) || isObservableSet(thing)) return thing
// Initializers run lazily when transpiling to babel, so make sure they are run...
initializeInstance(thing)
if (thing[$mobx]) return thing[$mobx]
Expand All @@ -76,7 +80,8 @@ export function getAdministration(thing: any, property?: string) {
export function getDebugName(thing: any, property?: string): string {
let named
if (property !== undefined) named = getAtom(thing, property)
else if (isObservableObject(thing) || isObservableMap(thing)) named = getAdministration(thing)
else if (isObservableObject(thing) || isObservableMap(thing) || isObservableSet(thing))
named = getAdministration(thing)
else named = getAtom(thing) // valid for arrays as well
return named.name
}
2 changes: 1 addition & 1 deletion src/utils/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function createPropDecorator(
if (quacksLikeADecorator(arguments)) {
// @decorator
decoratorArguments = EMPTY_ARRAY
return decorator.apply(null, arguments)
return decorator.apply(null, arguments as any)
} else {
// @decorator(args)
decoratorArguments = Array.prototype.slice.call(arguments)
Expand Down
29 changes: 29 additions & 0 deletions test/base/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,32 @@ test("toStringTag", () => {
expect(x[Symbol.toStringTag]).toBe("Set")
expect(Object.prototype.toString.call(x)).toBe("[object Set]")
})

test("getAtom", () => {
var x = set([1])
expect(mobx.getAtom(x)).toBeTruthy()

expect(mobx.isObservableSet(x)).toBeTruthy()
expect(mobx.isObservable(x)).toBeTruthy()
})

test("observe", () => {
const vals = []
var x = set([1])
mobx.observe(x, v => {
vals.push(v)
})
x.add(2)
x.add(1)
expect(vals).toEqual([{ newValue: 2, object: x, type: "add" }])
})

test("toJS", () => {
const x = mobx.observable({ x: 1 })
const y = set([x, 1])

const z = mobx.toJS(y)
expect(z).toEqual([{ x: 1 }, 1])
expect(z.x).not.toBe(x)
expect(mobx.isObservable(z.x)).toBeFalsy()
})

0 comments on commit e86dc6b

Please sign in to comment.