Skip to content

Commit 66fdf7a

Browse files
committed
strict null
1 parent 7a19cef commit 66fdf7a

24 files changed

Lines changed: 88 additions & 73 deletions

‎scripts/publish.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/* Publish.js, publish a new version of the npm package as found in the current directory */
44
/* Run this file from the root of the repository */
55
module.exports = function(shell, npm, git) {
6+
// build
7+
npm("run", "small-build")
8+
69
var pkg = JSON.parse(shell.read('package.json'));
710

811
// Bump version number

‎src/api/action.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function runInAction<T>(arg1, arg2?, arg3?) {
6868
invariant(fn.length === 0, "`runInAction` expects a function without arguments");
6969
invariant(typeof actionName === "string" && actionName.length > 0, `actions should have valid names, got: '${actionName}'`);
7070

71-
return executeAction(actionName, fn, scope, undefined);
71+
return executeAction(actionName, fn, scope);
7272
}
7373

7474
export function isAction(thing: any) {

‎src/api/autorun.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ export function autorun(view: (r: IReactionPublic) => void, scope?: any);
2222
* @returns disposer function, which can be used to stop the view from being updated in the future.
2323
*/
2424
export function autorun(name: string, view: (r: IReactionPublic) => void, scope?: any);
25-
2625
export function autorun(arg1: any, arg2: any, arg3?: any) {
27-
let name: string, view: (r: IReactionPublic) => void, scope: any;
26+
let name: string,
27+
view: (r: IReactionPublic) => void,
28+
scope: any;
29+
2830
if (typeof arg1 === "string") {
2931
name = arg1;
3032
view = arg2;
3133
scope = arg3;
32-
} else if (typeof arg1 === "function") {
34+
} else {
3335
name = arg1.name || ("Autorun@" + getNextId());
3436
view = arg1;
3537
scope = arg2;
@@ -85,7 +87,7 @@ export function when(arg1: any, arg2: any, arg3?: any, arg4?: any) {
8587
predicate = arg2;
8688
effect = arg3;
8789
scope = arg4;
88-
} else if (typeof arg1 === "function") {
90+
} else {
8991
name = ("When@" + getNextId());
9092
predicate = arg1;
9193
effect = arg2;
@@ -114,7 +116,7 @@ export function autorunAsync(arg1: any, arg2: any, arg3?: any, arg4?: any) {
114116
func = arg2;
115117
delay = arg3;
116118
scope = arg4;
117-
} else if (typeof arg1 === "function") {
119+
} else {
118120
name = arg1.name || ("AutorunAsync@" + getNextId());
119121
func = arg1;
120122
delay = arg2;
@@ -199,7 +201,7 @@ export function reaction<T>(arg1: any, arg2: any, arg3: any, arg4?: any, arg5?:
199201

200202
let firstTime = true;
201203
let isScheduled = false;
202-
let nextValue = undefined;
204+
let nextValue: T;
203205

204206
const r = new Reaction(name, () => {
205207
if (delay < 1) {

‎src/api/computeddecorator.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const computedDecorator = createClassPropertyDecorator(
2020
if (decoratorArgs && decoratorArgs.length === 1 && decoratorArgs[0].asStructure === true)
2121
compareStructural = true;
2222

23-
const adm = asObservableObject(target, undefined, ValueMode.Recursive);
23+
const adm = asObservableObject(target, "", ValueMode.Recursive);
2424
defineObservableProperty(adm, name, compareStructural ? asStructure(baseValue) : baseValue, false, setter);
2525
},
2626
function (name) {

‎src/api/createtransformer.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {globalState} from "../core/globalstate";
44

55
export type ITransformer<A, B> = (object: A) => B;
66

7-
export function createTransformer<A, B>(transformer: ITransformer<A, B>, onCleanup?: (resultObject: B, sourceObject?: A) => void): ITransformer<A, B> {
7+
export function createTransformer<A, B>(transformer: ITransformer<A, B>, onCleanup?: (resultObject: B | undefined, sourceObject?: A) => void): ITransformer<A, B> {
88
invariant(typeof transformer === "function" && transformer.length === 1, "createTransformer expects a function that accepts one argument");
99

1010
// Memoizes: object id -> reactive view that applies transformer to the object
@@ -17,7 +17,7 @@ export function createTransformer<A, B>(transformer: ITransformer<A, B>, onClean
1717
// Local transformer class specifically for this transformer
1818
class Transformer extends ComputedValue<B> {
1919
constructor(private sourceIdentifier: string, private sourceObject: A) {
20-
super(() => transformer(sourceObject), null, false, `Transformer-${(<any>transformer).name}-${sourceIdentifier}`, undefined);
20+
super(() => transformer(sourceObject), undefined, false, `Transformer-${(<any>transformer).name}-${sourceIdentifier}`, undefined);
2121
}
2222
onBecomeUnobserved() {
2323
const lastValue = this.value;

‎src/api/extendobservable.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export function extendObservable<A extends Object, B extends Object>(target: A,
1717
properties.forEach(propSet => {
1818
invariant(typeof propSet === "object", "all arguments of extendObservable should be objects");
1919
invariant(!isObservable(propSet), "extending an object with another observable (object) is not supported. Please construct an explicit propertymap, using `toJS` if need. See issue #540");
20-
extendObservableHelper(target, propSet, ValueMode.Recursive, null);
20+
extendObservableHelper(target, propSet, ValueMode.Recursive, undefined);
2121
});
2222
return <A & B> target;
2323
}
2424

25-
export function extendObservableHelper(target, properties, mode: ValueMode, name: string): Object {
25+
export function extendObservableHelper(target, properties, mode: ValueMode, name?: string): Object {
2626
invariant(Object.isExtensible(target), "Cannot extend the designated object; it is not extensible");
2727
const adm = asObservableObject(target, name, mode);
2828
for (let key in properties) if (hasOwnProperty(properties, key)) {

‎src/api/tojs.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {deprecated, isArrayLike} from "../utils/utils";
1111
export function toJS<T>(source: T, detectCycles?: boolean): T;
1212
export function toJS(source: any, detectCycles?: boolean): any;
1313
export function toJS(source, detectCycles: boolean, __alreadySeen: [any, any][]); // internal overload
14-
export function toJS(source, detectCycles: boolean = true, __alreadySeen: [any, any][] = null) {
14+
export function toJS(source, detectCycles: boolean = true, __alreadySeen: [any, any][] = []) {
1515
// optimization: using ES6 map would be more efficient!
1616
// optimization: lift this function outside toJS, this makes recursion expensive
1717
function cache(value) {
@@ -58,7 +58,7 @@ export function toJS(source, detectCycles: boolean = true, __alreadySeen: [any,
5858
/**
5959
* Basically, a deep clone, so that no reactive property will exist anymore.
6060
*/
61-
export function toJSlegacy(source, detectCycles: boolean = true, __alreadySeen: [any, any][] = null) {
61+
export function toJSlegacy(source, detectCycles: boolean = true, __alreadySeen: [any, any][] = []) {
6262
deprecated("toJSlegacy is deprecated and will be removed in the next major. Use `toJS` instead. See #566");
6363
// optimization: using ES6 map would be more efficient!
6464
function cache(value) {

‎src/core/action.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ export function createAction(actionName: string, fn: Function): Function {
1515
return res;
1616
}
1717

18-
export function executeAction(actionName: string, fn: Function, scope: any, args: IArguments) {
18+
export function executeAction(actionName: string, fn: Function, scope: any, args?: IArguments) {
1919
// actions should not be called from computeds. check only works if the computed is actively observed, but that is fine enough as heuristic
2020
invariant(!isComputedValue(globalState.trackingDerivation), "Computed values or transformers should not invoke actions or trigger other side effects");
2121

2222
const notifySpy = isSpyEnabled();
23-
let startTime: number;
23+
let startTime: number = 0;
2424
if (notifySpy) {
2525
startTime = Date.now();
2626
const l = (args && args.length) || 0;
2727
const flattendArgs = new Array(l);
2828
if (l > 0) for (let i = 0; i < l; i++)
29-
flattendArgs[i] = args[i];
29+
flattendArgs[i] = args![i];
3030
spyReportStart({
3131
type: "action",
3232
name: actionName,
@@ -51,8 +51,7 @@ export function executeAction(actionName: string, fn: Function, scope: any, args
5151
}
5252
}
5353

54-
export function useStrict(strict: boolean);
55-
export function useStrict(strict?: boolean): any {
54+
export function useStrict(strict: boolean): any {
5655
invariant(globalState.trackingDerivation === null, "It is not allowed to set `useStrict` when a derivation is running");
5756
globalState.strictMode = strict;
5857
globalState.allowStateChanges = !strict;

‎src/core/atom.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class BaseAtom implements IAtom {
3737
* Invoke this method _after_ this method has changed to signal mobx that all its observers should invalidate.
3838
*/
3939
public reportChanged() {
40-
transactionStart("propagatingAtomChange", null, false);
40+
transactionStart("propagatingAtomChange", undefined, false);
4141
propagateChanged(this);
4242
transactionEnd(false);
4343
}

‎src/core/computedvalue.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
4343
lowestObserverState = IDerivationState.UP_TO_DATE;
4444
unboundDepsCount = 0;
4545
__mapid = "#" + getNextId();
46-
protected value: T = undefined;
46+
protected value: T | undefined = undefined;
4747
name: string;
4848
isComputing: boolean = false; // to check for cycles
4949
isRunningSetter: boolean = false; // TODO optimize, see: https://reaktor.com/blog/javascript-performance-fundamentals-make-bluebird-fast/
@@ -59,7 +59,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
5959
* However, enabling compareStructural can be convienent if you always produce an new aggregated object and don't want to notify observers if it is structurally the same.
6060
* This is useful for working with vectors, mouse coordinates etc.
6161
*/
62-
constructor(public derivation: () => T, public scope: Object, private compareStructural: boolean, name: string, setter: (v: T) => void) {
62+
constructor(public derivation: () => T, public scope: Object | undefined, private compareStructural: boolean, name: string, setter?: (v: T) => void) {
6363
this.name = name || "ComputedValue@" + getNextId();
6464
if (setter)
6565
this.setter = createAction(name + "-setter", setter) as any;
@@ -117,7 +117,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
117117
propagateChangeConfirmed(this);
118118

119119
}
120-
const result = this.value;
120+
const result = this.value!;
121121
endBatch();
122122

123123
return result;
@@ -157,9 +157,9 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
157157
return valueDidChange(this.compareStructural, newValue, oldValue);
158158
}
159159

160-
observe(listener: (newValue: T, oldValue: T) => void, fireImmediately?: boolean): Lambda {
160+
observe(listener: (newValue: T, oldValue: T | undefined) => void, fireImmediately?: boolean): Lambda {
161161
let firstTime = true;
162-
let prevValue = undefined;
162+
let prevValue: T | undefined = undefined;
163163
return autorun(() => {
164164
let newValue = this.get();
165165
if (!firstTime || fireImmediately) {
@@ -182,7 +182,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
182182

183183
whyRun() {
184184
const isTracking = Boolean(globalState.trackingDerivation);
185-
const observing = unique(this.isComputing ? this.newObserving : this.observing).map((dep: any) => dep.name);
185+
const observing = unique(this.isComputing ? this.newObserving! : this.observing).map((dep: any) => dep.name);
186186
const observers = unique(getObservers(this).map(dep => dep.name));
187187
// TODO: use issue
188188
// TOOD; expand wiht more states

0 commit comments

Comments
 (0)