forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservablevalue.ts
More file actions
159 lines (139 loc) · 4.3 KB
/
Copy pathobservablevalue.ts
File metadata and controls
159 lines (139 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { Atom, declareAtom } from "../core/atom"
import { checkIfStateModificationsAreAllowed } from "../core/derivation"
import {
Lambda,
getNextId,
createInstanceofPredicate,
primitiveSymbol,
toPrimitive
} from "../utils/utils"
import {
hasInterceptors,
IInterceptable,
IInterceptor,
registerInterceptor,
interceptChange
} from "./intercept-utils"
import { IListenable, registerListener, hasListeners, notifyListeners } from "./listen-utils"
import { isSpyEnabled, spyReportStart, spyReportEnd, spyReport } from "../core/spy"
import { IEnhancer } from "./modifiers"
export interface IValueWillChange<T> {
object: any
type: "update"
newValue: T
}
export interface IValueDidChange<T> extends IValueWillChange<T> {
oldValue: T | undefined
}
export type IUNCHANGED = {}
export const UNCHANGED: IUNCHANGED = {}
export interface IObservableValue<T> {
get(): T
set(value: T): void
intercept(handler: IInterceptor<IValueWillChange<T>>): Lambda
observe(listener: (change: IValueDidChange<T>) => void, fireImmediately?: boolean): Lambda
}
declareAtom()
export class ObservableValue<T> extends Atom
implements IObservableValue<T>, IInterceptable<IValueWillChange<T>>, IListenable {
hasUnreportedChange = false
interceptors
changeListeners
value
dehancer: any
constructor(
value: T,
public enhancer: IEnhancer<T>,
name = "ObservableValue@" + getNextId(),
notifySpy = true
) {
super(name)
this.value = enhancer(value, undefined, name)
if (notifySpy && isSpyEnabled()) {
// only notify spy if this is a stand-alone observable
spyReport({ type: "create", name: this.name, newValue: "" + this.value })
}
}
private dehanceValue(value: T): T {
if (this.dehancer !== undefined) return this.dehancer(value)
return value
}
public set(newValue: T) {
const oldValue = this.value
newValue = this.prepareNewValue(newValue) as any
if (newValue !== UNCHANGED) {
const notifySpy = isSpyEnabled()
if (notifySpy) {
spyReportStart({
type: "update",
name: this.name,
newValue,
oldValue
})
}
this.setNewValue(newValue)
if (notifySpy) spyReportEnd()
}
}
private prepareNewValue(newValue): T | IUNCHANGED {
checkIfStateModificationsAreAllowed(this)
if (hasInterceptors(this)) {
const change = interceptChange<IValueWillChange<T>>(this, {
object: this,
type: "update",
newValue
})
if (!change) return UNCHANGED
newValue = change.newValue
}
// apply modifier
newValue = this.enhancer(newValue, this.value, this.name)
return this.value !== newValue ? newValue : UNCHANGED
}
setNewValue(newValue: T) {
const oldValue = this.value
this.value = newValue
this.reportChanged()
if (hasListeners(this)) {
notifyListeners(this, {
type: "update",
object: this,
newValue,
oldValue
})
}
}
public get(): T {
this.reportObserved()
return this.dehanceValue(this.value)
}
public intercept(handler: IInterceptor<IValueWillChange<T>>): Lambda {
return registerInterceptor(this, handler)
}
public observe(
listener: (change: IValueDidChange<T>) => void,
fireImmediately?: boolean
): Lambda {
if (fireImmediately)
listener({
object: this,
type: "update",
newValue: this.value,
oldValue: undefined
})
return registerListener(this, listener)
}
toJSON() {
return this.get()
}
toString() {
return `${this.name}[${this.value}]`
}
valueOf(): T {
return toPrimitive(this.get())
}
}
ObservableValue.prototype[primitiveSymbol()] = ObservableValue.prototype.valueOf
export var isObservableValue = createInstanceofPredicate("ObservableValue", ObservableValue) as (
x: any
) => x is IObservableValue<any>