diff --git a/package.json b/package.json index d15c3b0..fafdcf0 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "lodash.clonedeepwith": "*", "lodash.intersection": "*", "mobx": "^5.14.0", - "prettier": "^1.7.2", + "prettier": "^2.0.4", "rollup": "^0.50.0", "rxjs": "^6.0.0", "shelljs": "^0.8.3", diff --git a/publish.js b/publish.js index ccc6700..5f6ae3f 100755 --- a/publish.js +++ b/publish.js @@ -8,7 +8,7 @@ const readline = require("readline") const rl = readline.createInterface({ input: process.stdin, - output: process.stdout + output: process.stdout, }) function run(command, options) { @@ -26,8 +26,8 @@ function exit(code, msg) { } async function prompt(question, defaultValue) { - return new Promise(resolve => { - rl.question(`${question} [${defaultValue}]: `, answer => { + return new Promise((resolve) => { + rl.question(`${question} [${defaultValue}]: `, (answer) => { answer = answer && answer.trim() resolve(answer ? answer : defaultValue) }) @@ -51,7 +51,7 @@ async function main() { // Check registry data const npmInfoRet = run(`npm info ${pkg.name} --json`, { continueOnErrors: true, - silent: true + silent: true, }) if (npmInfoRet.code === 0) { //package is registered in npm? @@ -81,6 +81,6 @@ async function main() { } } -main().catch(e => { +main().catch((e) => { throw e }) diff --git a/src/action-async.ts b/src/action-async.ts index b9c6083..cb627b8 100644 --- a/src/action-async.ts +++ b/src/action-async.ts @@ -39,7 +39,7 @@ let inOrderExecution: () => Promise } const idle = () => - new Promise(r => { + new Promise((r) => { queueMicrotaskPolyfill(r) }) @@ -93,7 +93,7 @@ export async function task(value: R | PromiseLike): Promise { actionRunInfo, actionName, args, - scope + scope, }) currentlyActiveIds.add(runId) } @@ -205,7 +205,7 @@ function actionAsyncFn(actionName: string, fn: Function): Function { if (typeof actionName !== "string" || !actionName) fail(`actions should have valid names, got: '${actionName}'`) - return async function(this: any, ...args: any) { + return async function (this: any, ...args: any) { const nextRunId = runId++ unfinishedIds.add(nextRunId) @@ -217,7 +217,7 @@ function actionAsyncFn(actionName: string, fn: Function): Function { actionRunInfo, actionName, args, - scope: this + scope: this, }) currentlyActiveIds.add(nextRunId) diff --git a/src/array.ts b/src/array.ts index 8f6a576..ea2f56c 100644 --- a/src/array.ts +++ b/src/array.ts @@ -27,7 +27,7 @@ export function moveItem(target: IObservableArray, fromIndex: number, toIn ...oldItems.slice(0, fromIndex), ...oldItems.slice(fromIndex + 1, toIndex + 1), oldItems[fromIndex], - ...oldItems.slice(toIndex + 1) + ...oldItems.slice(toIndex + 1), ] } else { // toIndex < fromIndex @@ -35,7 +35,7 @@ export function moveItem(target: IObservableArray, fromIndex: number, toIn ...oldItems.slice(0, toIndex), oldItems[fromIndex], ...oldItems.slice(toIndex, fromIndex), - ...oldItems.slice(fromIndex + 1) + ...oldItems.slice(fromIndex + 1), ] } target.replace(newItems) diff --git a/src/async-action.ts b/src/async-action.ts index 07ebab8..eb5830a 100644 --- a/src/async-action.ts +++ b/src/async-action.ts @@ -175,16 +175,16 @@ export function asyncAction(arg1: any, arg2?: any): any { const descriptor: PropertyDescriptor = arguments[2] if (descriptor && descriptor.value) { return Object.assign({}, descriptor, { - value: flow(descriptor.value) + value: flow(descriptor.value), }) } else { return Object.assign({}, descriptor, { set(v: any) { Object.defineProperty(this, name, { ...descriptor, - value: flow(v) + value: flow(v), }) - } + }, }) } } diff --git a/src/computedFn.ts b/src/computedFn.ts index 2f205c2..787bfcd 100644 --- a/src/computedFn.ts +++ b/src/computedFn.ts @@ -5,7 +5,7 @@ import { computed, onBecomeUnobserved, _isComputingDerivation, - isAction + isAction, } from "mobx" /** @@ -56,7 +56,7 @@ export function computedFn any>( : keepAliveOrOptions const d = new DeepMap>() - return function(...args: Parameters): ReturnType { + return function (...args: Parameters): ReturnType { const self = this const entry = d.entry(args) // cache hit, return @@ -78,7 +78,7 @@ export function computedFn any>( }, { ...opts, - name: `computedFn(${fn.name}#${++i})` + name: `computedFn(${fn.name}#${++i})`, } ) entry.set(c) diff --git a/src/create-transformer.ts b/src/create-transformer.ts index c53288a..714e904 100644 --- a/src/create-transformer.ts +++ b/src/create-transformer.ts @@ -3,7 +3,7 @@ import { onBecomeUnobserved, IComputedValue, _isComputingDerivation, - IComputedValueOptions + IComputedValueOptions, } from "mobx" import { invariant, addHiddenProp } from "./utils" @@ -75,7 +75,7 @@ export function createTransformer( }, { ...computedValueOptions, - name: prettifiedName + name: prettifiedName, } ) if (!keepAlive) { diff --git a/src/create-view-model.ts b/src/create-view-model.ts index 8fc2b5a..a01e43a 100644 --- a/src/create-view-model.ts +++ b/src/create-view-model.ts @@ -11,7 +11,7 @@ import { computed, keys, _getAdministration, - $mobx + $mobx, } from "mobx" import { invariant, getAllMethodsAndProperties } from "./utils" @@ -75,7 +75,7 @@ export class ViewModel implements IViewModel { } else { this.localValues.delete(key) } - }) + }), }) }) } diff --git a/src/decorator-utils.ts b/src/decorator-utils.ts index 9377cc8..9a4f732 100644 --- a/src/decorator-utils.ts +++ b/src/decorator-utils.ts @@ -34,7 +34,7 @@ export function decorateMethod( value: decorateFn(prop, descriptor.value), enumerable: false, configurable: true, // See #1477 - writable: true // for typescript, this must be writable, otherwise it cannot inherit :/ (see inheritable actions test) + writable: true, // for typescript, this must be writable, otherwise it cannot inherit :/ (see inheritable actions test) } } @@ -47,7 +47,7 @@ export function decorateMethod( initializer() { // N.B: we can't immediately invoke initializer; this would be wrong return decorateFn(prop, initializer!.call(this)) - } + }, } } @@ -65,6 +65,6 @@ export function decorateField( }, set(value) { addHiddenProp(this, prop, decorateFn(prop, value)) - } + }, }) } diff --git a/src/deepObserve.ts b/src/deepObserve.ts index 41352fb..2b7129b 100644 --- a/src/deepObserve.ts +++ b/src/deepObserve.ts @@ -8,7 +8,7 @@ import { IArraySplice, IMapDidChange, values, - entries + entries, } from "mobx" import { IDisposer } from "./utils" @@ -117,7 +117,7 @@ export function deepObserve( const entry = { parent, path, - dispose: observe(thing, genericListener) + dispose: observe(thing, genericListener), } entrySet.set(thing, entry) entries(thing).forEach(([key, value]) => observeRecursively(value, entry, key)) diff --git a/src/from-promise.ts b/src/from-promise.ts index bb5bfe5..84a00f6 100644 --- a/src/from-promise.ts +++ b/src/from-promise.ts @@ -82,7 +82,7 @@ function createObservablePromise(origPromise: any, oldPromise?: any) { promise, { value: oldData, - state: PENDING + state: PENDING, }, {}, { deep: false } @@ -195,14 +195,14 @@ export const fromPromise: { resolve(value?: T): IFulfilledPromise & IBasePromiseBasedObservable } = createObservablePromise as any -fromPromise.reject = action("fromPromise.reject", function(reason: any) { +fromPromise.reject = action("fromPromise.reject", function (reason: any) { const p: any = fromPromise(Promise.reject(reason)) p.state = REJECTED p.value = reason return p }) as any -fromPromise.resolve = action("fromPromise.resolve", function(value: any = undefined) { +fromPromise.resolve = action("fromPromise.resolve", function (value: any = undefined) { const p: any = fromPromise(Promise.resolve(value)) p.state = FULFILLED p.value = value diff --git a/src/from-resource.ts b/src/from-resource.ts index a5f01ff..6cfff7f 100644 --- a/src/from-resource.ts +++ b/src/from-resource.ts @@ -116,6 +116,6 @@ export function fromResource( isDisposed = true suspender() }, - isAlive: () => isActive + isAlive: () => isActive, } } diff --git a/src/guarded-when.ts b/src/guarded-when.ts index 8295aeb..dc1b8de 100644 --- a/src/guarded-when.ts +++ b/src/guarded-when.ts @@ -39,6 +39,6 @@ export function whenWithTimeout( deprecated("whenWithTimeout is deprecated, use mobx.when with timeout option instead") return when(expr, action, { timeout, - onError: onTimeout + onError: onTimeout, }) } diff --git a/src/lazy-observable.ts b/src/lazy-observable.ts index 122d739..4c1e0fa 100644 --- a/src/lazy-observable.ts +++ b/src/lazy-observable.ts @@ -83,6 +83,6 @@ export function lazyObservable( }, get pending() { return pending.get() - } + }, } } diff --git a/src/now.ts b/src/now.ts index 3b1baa9..c939f26 100644 --- a/src/now.ts +++ b/src/now.ts @@ -46,7 +46,7 @@ export function now(interval: number | "frame" = 1000) { function createIntervalTicker(interval: number): IResource { let subscriptionHandle: any return fromResource( - sink => { + (sink) => { subscriptionHandle = setInterval(() => sink(Date.now()), interval) }, () => { @@ -59,7 +59,7 @@ function createIntervalTicker(interval: number): IResource { function createAnimationFrameTicker(): IResource { let subscriptionHandle: number const frameBasedTicker = fromResource( - sink => { + (sink) => { function scheduleTick() { window.requestAnimationFrame(() => { sink(Date.now()) diff --git a/src/observable-stream.ts b/src/observable-stream.ts index 3b52097..e34ecc9 100644 --- a/src/observable-stream.ts +++ b/src/observable-stream.ts @@ -62,10 +62,10 @@ export function toStream( ? ({ newValue }: { newValue: T }) => observer(newValue) : ({ newValue }: { newValue: T }) => observer.next(newValue), fireImmediately - ) + ), } }, - [observableSymbol()]: self + [observableSymbol()]: self, } } diff --git a/src/utils.ts b/src/utils.ts index 408d117..1e3f3b4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -24,7 +24,7 @@ export function addHiddenProp(object: any, propName: string, value: any) { enumerable: false, writable: true, configurable: true, - value + value, }) } diff --git a/src/when-async.ts b/src/when-async.ts index 29b79f7..0f01413 100644 --- a/src/when-async.ts +++ b/src/when-async.ts @@ -17,6 +17,6 @@ import { deprecated } from "./utils" export function whenAsync(fn: () => boolean, timeout: number = 0): Promise { deprecated("whenAsync is deprecated, use mobx.when without effect instead") return when(fn, { - timeout + timeout, }) } diff --git a/test/action-async.ts b/test/action-async.ts index 4041475..55ce1f2 100644 --- a/test/action-async.ts +++ b/test/action-async.ts @@ -2,7 +2,7 @@ import * as mobx from "mobx" import { actionAsync, task } from "../src/mobx-utils" function delay(time: number, value: T) { - return new Promise(resolve => { + return new Promise((resolve) => { setTimeout(() => { resolve(value) }, time) @@ -18,7 +18,7 @@ function delayThrow(time: number, value: T) { } function delayFn(time: number, fn: () => void) { - return new Promise(resolve => { + return new Promise((resolve) => { setTimeout(() => { fn() resolve() @@ -28,7 +28,10 @@ function delayFn(time: number, fn: () => void) { function expectNoActionsRunning() { const obs = mobx.observable.box(1) - const d = mobx.reaction(() => obs.get(), () => {}) + const d = mobx.reaction( + () => obs.get(), + () => {} + ) expect(() => obs.set(2)).toThrow( "changing observed observable values outside actions is not allowed" ) @@ -39,9 +42,13 @@ test("it should support async actions", async () => { mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) - const f = actionAsync(async function(initial) { + const f = actionAsync(async function (initial) { x.a = initial // this runs in action x.a = await task(delay(100, 3)) await task(delay(100, 0)) @@ -61,9 +68,13 @@ test("it should support try catch in async", async () => { mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) - const f = actionAsync(async function(initial) { + const f = actionAsync(async function (initial) { x.a = initial // this runs in action try { x.a = await task(delayThrow(100, 5)) @@ -116,7 +127,7 @@ test("it should support async action in classes", async () => { class X { a = 1 - f = actionAsync(async function(initial) { + f = actionAsync(async function (initial) { this.a = initial // this runs in action try { this.a = await task(delayThrow(100, 5)) @@ -129,11 +140,15 @@ test("it should support async action in classes", async () => { }) } mobx.decorate(X, { - a: mobx.observable + a: mobx.observable, }) const x = new X() - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) const v = await x.f(2) expect(v).toBe(5) @@ -165,7 +180,11 @@ test("it should support async action in classes with a method decorator", async } const x = new X() - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) const v = await x.f(2) expect(v).toBe(5) @@ -183,7 +202,7 @@ test("it should support async action in classes with a field decorator", async ( @mobx.observable a = 1 @actionAsync - f = async initial => { + f = async (initial) => { this.a = initial // this runs in action try { this.a = await task(delayThrow(100, 5)) @@ -197,7 +216,11 @@ test("it should support async action in classes with a field decorator", async ( } const x = new X() - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) const v = await x.f(2) expect(v).toBe(5) @@ -211,21 +234,21 @@ test("it should support logging", async () => { const events = [] const x = mobx.observable({ a: 1 }) - const innerF = actionAsync("innerF", async initial => { + const innerF = actionAsync("innerF", async (initial) => { x.a = initial // this runs in action x.a = await task(delay(100, 3)) x.a = 4 return x.a }) - const f = actionAsync("f", async initial => { + const f = actionAsync("f", async (initial) => { x.a = initial x.a = await task(innerF(2)) x.a = 5 x.a = await task(delay(100, 3)) return x.a }) - const d = mobx.spy(ev => events.push(ev)) + const d = mobx.spy((ev) => events.push(ev)) await f(1) expect(stripEvents(events)).toMatchSnapshot() @@ -234,7 +257,7 @@ test("it should support logging", async () => { }) function stripEvents(events) { - return events.map(e => { + return events.map((e) => { delete e.object delete e.fn delete e.time @@ -246,9 +269,13 @@ test("it should support async actions within async actions", async () => { mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) - const innerF = actionAsync(async initial => { + const innerF = actionAsync(async (initial) => { x.a = initial // this runs in action x.a = await task(delay(100, 3)) await task(delay(100, 0)) @@ -256,7 +283,7 @@ test("it should support async actions within async actions", async () => { return x.a }) - const f1 = actionAsync(async initial => { + const f1 = actionAsync(async (initial) => { x.a = await task(innerF(initial)) x.a = await task(delay(100, 5)) await task(delay(100, 0)) @@ -274,9 +301,13 @@ test("it should support async actions within async actions that are awaited late mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) - const innerF = actionAsync(async initial => { + const innerF = actionAsync(async (initial) => { x.a = initial // this runs in action x.a = await task(delay(10, 3)) await task(delay(30, 0)) @@ -284,7 +315,7 @@ test("it should support async actions within async actions that are awaited late return 7 }) - const f1 = actionAsync(async initial => { + const f1 = actionAsync(async (initial) => { const futureInnerF = innerF(initial) x.a = await task(delay(20, 4)) await task(delay(10, 0)) @@ -303,9 +334,13 @@ test("it should support async actions within async actions that throw", async () mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) - const innerF = actionAsync(async function(initial) { + const innerF = actionAsync(async function (initial) { x.a = initial // this runs in action x.a = await task(delay(100, 3)) await task(delay(100, 0)) @@ -313,7 +348,7 @@ test("it should support async actions within async actions that throw", async () throw "err" }) - const f = actionAsync(async function(initial) { + const f = actionAsync(async function (initial) { x.a = await task(innerF(initial)) x.a = await task(delay(100, 5)) await task(delay(100, 0)) @@ -350,7 +385,7 @@ test("dangling promises created indirectly inside the action should be ok", asyn const f1 = actionAsync(async () => { await task( - new Promise(resolve => { + new Promise((resolve) => { setTimeout(() => { danglingP = delay(100, 1) // indirect dangling promise resolve() @@ -393,7 +428,11 @@ test("dangling promises created directly inside the action without using task sh mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) let danglingP @@ -416,7 +455,11 @@ test("it should support recursive async", async () => { mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 10 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) const f1 = actionAsync(async () => { if (x.a <= 0) return @@ -433,7 +476,11 @@ test("it should support parallel async", async () => { mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) const f1 = actionAsync(async () => { x.a = 2 @@ -470,7 +517,7 @@ test("it should support parallel async", async () => { delayFn(26, expectNoActionsRunning), delayFn(35, expectNoActionsRunning), delayFn(44, expectNoActionsRunning), - delayFn(46, expectNoActionsRunning) + delayFn(46, expectNoActionsRunning), ]) expect(values).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) expectNoActionsRunning() @@ -480,7 +527,11 @@ test("calling async actions that do not await should be ok", async () => { mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) const f1 = actionAsync("f1", async () => { x.a++ @@ -504,7 +555,11 @@ test("complex case", async () => { mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) const f1 = actionAsync("f1", async (fn: any) => { x.a++ @@ -539,7 +594,11 @@ test("immediately resolved promises", async () => { mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) const f1 = actionAsync("f1", async () => { await task(Promise.resolve("")) @@ -562,7 +621,11 @@ test("reusing promises", async () => { mobx.configure({ enforceActions: "observed" }) const values = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) const p = delay(10, 2) @@ -582,7 +645,7 @@ test("reusing promises", async () => { test("actions that throw in parallel", async () => { mobx.configure({ enforceActions: "observed" }) - const r = shouldThrow => + const r = (shouldThrow) => new Promise((resolve, reject) => { setTimeout(() => { if (shouldThrow) { diff --git a/test/async-action.ts b/test/async-action.ts index 182e335..51eaeb8 100644 --- a/test/async-action.ts +++ b/test/async-action.ts @@ -10,13 +10,17 @@ function delay(time: number, value: T, shouldThrow = false): Promise { }) } -test("it should support async generator actions", done => { +test("it should support async generator actions", (done) => { mobx.configure({ enforceActions: "observed" }) const values: any[] = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) - const f = utils.asyncAction(function*(initial: number) { + const f = utils.asyncAction(function* (initial: number) { x.a = initial // this runs in action x.a = yield delay(100, 3) // and this as well! yield delay(100, 0) @@ -34,13 +38,17 @@ test("it should support async generator actions", done => { }, 10) }) -test("it should support try catch in async generator", done => { +test("it should support try catch in async generator", (done) => { mobx.configure({ enforceActions: "observed" }) const values: any[] = [] const x = mobx.observable({ a: 1 }) - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) - const f = utils.asyncAction(function*(initial: number) { + const f = utils.asyncAction(function* (initial: number) { x.a = initial // this runs in action try { x.a = yield delay(100, 5, true) // and this as well! @@ -62,9 +70,9 @@ test("it should support try catch in async generator", done => { }, 10) }) -test("it should support throw from async generator", done => { +test("it should support throw from async generator", (done) => { utils - .asyncAction(function*() { + .asyncAction(function* () { throw 7 })() .then( @@ -72,16 +80,16 @@ test("it should support throw from async generator", done => { fail() done() }, - e => { + (e) => { expect(e).toBe(7) done() } ) }) -test("it should support throw from yielded promise generator", done => { +test("it should support throw from yielded promise generator", (done) => { utils - .asyncAction(function*() { + .asyncAction(function* () { return yield delay(10, 7, true) })() .then( @@ -89,14 +97,14 @@ test("it should support throw from yielded promise generator", done => { fail() done() }, - e => { + (e) => { expect(e).toBe(7) done() } ) }) -test("it should support asyncAction as decorator", done => { +test("it should support asyncAction as decorator", (done) => { const values: any[] = [] mobx.configure({ enforceActions: "observed" }) @@ -119,7 +127,11 @@ test("it should support asyncAction as decorator", done => { } const x = new X() - mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + mobx.reaction( + () => x.a, + (v) => values.push(v), + { fireImmediately: true } + ) setTimeout(() => { // TODO: mweh on any cast... @@ -133,7 +145,7 @@ test("it should support asyncAction as decorator", done => { }, 10) }) -test("it should support logging", done => { +test("it should support logging", (done) => { mobx.configure({ enforceActions: "observed" }) const events: any[] = [] const x = mobx.observable({ a: 1 }) @@ -145,7 +157,7 @@ test("it should support logging", done => { x.a = yield delay(100, 3) return x.a }) - const d = mobx.spy(ev => events.push(ev)) + const d = mobx.spy((ev) => events.push(ev)) setTimeout(() => { f(2).then(() => { @@ -157,7 +169,7 @@ test("it should support logging", done => { }) function stripEvents(events) { - return events.map(e => { + return events.map((e) => { delete e.object delete e.fn delete e.time diff --git a/test/chunk-processor.js b/test/chunk-processor.js index dca1d64..2ef8dd9 100644 --- a/test/chunk-processor.js +++ b/test/chunk-processor.js @@ -9,7 +9,7 @@ test("sync processor should work with max", () => { const q = mobx.observable([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) const res = [] - const stop = utils.chunkProcessor(q, v => res.push(v), 0, 3) + const stop = utils.chunkProcessor(q, (v) => res.push(v), 0, 3) expect(res).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]) expect(q.length).toBe(0) @@ -41,7 +41,7 @@ test("sync processor should work with max", () => { [4, 5], [3], [8, 9], - [6, 7] + [6, 7], ]) stop() @@ -56,7 +56,7 @@ test("sync processor should work with max", () => { [4, 5], [3], [8, 9], - [6, 7] + [6, 7], ]) }) @@ -64,21 +64,24 @@ test("sync processor should work with default max", () => { const q = mobx.observable([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) const res = [] - utils.chunkProcessor(q, v => res.push(v)) + utils.chunkProcessor(q, (v) => res.push(v)) expect(res).toEqual([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]) expect(q.length).toBe(0) mobx.runInAction(() => q.push(1, 2, 3, 4, 5)) - expect(res).toEqual([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5]]) + expect(res).toEqual([ + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + [1, 2, 3, 4, 5], + ]) expect(q.length).toBe(0) }) -test("async processor should work", done => { +test("async processor should work", (done) => { const q = mobx.observable([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) const res = [] - const stop = utils.chunkProcessor(q, v => res.push(v), 10, 3) + const stop = utils.chunkProcessor(q, (v) => res.push(v), 10, 3) expect(res.length).toBe(0) expect(q.length).toBe(10) @@ -101,11 +104,11 @@ test("async processor should work", done => { }, 50) }) -test("async processor should combine smaller chunks to max size", done => { +test("async processor should combine smaller chunks to max size", (done) => { const q = mobx.observable([1, 2]) const res = [] - const stop = utils.chunkProcessor(q, v => res.push(v), 10, 3) + const stop = utils.chunkProcessor(q, (v) => res.push(v), 10, 3) expect(res.length).toBe(0) expect(q.length).toBe(2) diff --git a/test/computedFn.ts b/test/computedFn.ts index 45fa334..fcf31bf 100644 --- a/test/computedFn.ts +++ b/test/computedFn.ts @@ -6,20 +6,20 @@ import { action, getDependencyTree, comparer, - getObserverTree + getObserverTree, } from "mobx" const john = { name: "john", - age: 15 + age: 15, } const jane = { name: "jane", - age: 45 + age: 45, } const able = { name: "able", - age: 12 + age: 12, } class Store { @@ -29,7 +29,7 @@ class Store { filter(age: number, firstLetter: string) { this.events.push(`f ${age} ${firstLetter}`) - return this.persons.filter(p => { + return this.persons.filter((p) => { return p.age > age && (!firstLetter || p.name[0] === firstLetter) }) } @@ -55,7 +55,7 @@ test("basics - kept alive", () => { events.push( s .filter(1, "j") - .map(p => p.name) + .map((p) => p.name) .join("-") ) }) @@ -78,7 +78,7 @@ test("basics - kept alive", () => { "john-jable", "f 1 j", "john", - "f 20 " + "f 20 ", ]) }) @@ -99,14 +99,14 @@ test("basics - auto suspend", () => { expect(events.splice(0)).toEqual([ "f 20 ", "f 1 j", - "f 20 " //suspended + "f 20 ", //suspended ]) const d = autorun(() => { events.push( s .filter(1, "j") - .map(p => p.name) + .map((p) => p.name) .join("-") ) }) @@ -132,7 +132,7 @@ test("basics - auto suspend", () => { "john", "unobserved persons", // all suspended! "f 20 ", - "f 20 " + "f 20 ", ]) }) @@ -147,7 +147,7 @@ test("make sure the fn is cached", () => { expect(this).toBe(store) events.push("calc " + x) return this.a * this.b * x - }) + }), }) const d = autorun(() => { @@ -165,7 +165,7 @@ test("make sure the fn is cached", () => { test("supports options", () => { const events: number[][] = [] const xs = observable([1, 2, 3]) - const xsLessThan = computedFn(n => xs.filter(x => x < n), { equals: comparer.structural }) + const xsLessThan = computedFn((n) => xs.filter((x) => x < n), { equals: comparer.structural }) autorun(() => events.push(xsLessThan(3))) expect(events).toEqual([[1, 2]]) diff --git a/test/create-transformer.ts b/test/create-transformer.ts index af4bc6d..f6cc2c8 100644 --- a/test/create-transformer.ts +++ b/test/create-transformer.ts @@ -7,22 +7,22 @@ test("transform1", () => { let state = m.observable({ todos: [ { - title: "coffee" - } + title: "coffee", + }, ], - name: "michel" + name: "michel", }) let mapped let unloaded = [] - let transformState = createTransformer(function(state: any) { + let transformState = createTransformer(function (state: any) { stateCalc++ return state.name + state.todos.map(transformTodo).join(",") }) let transformTodo = createTransformer( - function(todo: any) { + function (todo: any) { todoCalc++ return todo.title.toUpperCase() }, @@ -31,7 +31,7 @@ test("transform1", () => { } ) - m.autorun(function() { + m.autorun(function () { mapped = transformState(state) }) @@ -79,7 +79,7 @@ test("createTransformer as off-instance computed", () => { // observable in closure let capitalize = m.observable.box(false) - let _computeDisplayName = person => { + let _computeDisplayName = (person) => { runs++ // count the runs let res = person.firstName + " " + person.lastName if (capitalize.get()) return res.toUpperCase() @@ -91,19 +91,19 @@ test("createTransformer as off-instance computed", () => { let person1 = m.observable({ firstName: "Mickey", - lastName: "Mouse" + lastName: "Mouse", }) let person2 = m.observable({ firstName: "Donald", - lastName: "Duck" + lastName: "Duck", }) let persons = m.observable([]) let displayNames = [] let disposer = m.autorun(() => { - displayNames = persons.map(p => displayName(p)) + displayNames = persons.map((p) => displayName(p)) }) expect(runs).toBe(0) @@ -150,7 +150,7 @@ test("transform into reactive graph", () => { function Folder(name) { m.extendObservable(this, { name: name, - children: [] + children: [], }) } @@ -171,26 +171,26 @@ test("transform into reactive graph", () => { }, get children() { _childrenRecalc++ - return this.baseFolder.children.map(transformFolder).filter(function(folder) { + return this.baseFolder.children.map(transformFolder).filter(function (folder) { return folder.isVisible === true }) - } + }, }) } let state: any = m.observable({ - filter: null + filter: null, }) let _transformCount = 0 - let transformFolder = createTransformer(function(folder) { + let transformFolder = createTransformer(function (folder) { _transformCount++ // console.log("Transform", folder.name) return new DerivedFolder(state, folder) }) state.root = new Folder("/") - m.autorun(function() { + m.autorun(function () { state.derived = transformFolder(state.root) state.derived.children }) @@ -249,7 +249,7 @@ test("transform tree (modifying tree incrementally)", () => { let renderNodeCount = 0 let transformNode = createTransformer( - function(node) { + function (node) { nodeCreateCount++ return new DisplayNode(node) }, @@ -259,7 +259,7 @@ test("transform tree (modifying tree incrementally)", () => { ) // transform nodes to renderedNodes - m.autorun(function() { + m.autorun(function () { // KM: ideally, I would like to do an assignment here, but it creates a cycle and would need to preserve ms.modifiers.structure: // // state.renderedNodes = state.root ? state.root.map(transformNode) : []; @@ -270,7 +270,7 @@ test("transform tree (modifying tree incrementally)", () => { }) // render - m.autorun(function() { + m.autorun(function () { renderCount++ renderNodeCount += state.renderedNodes.length }) @@ -317,11 +317,11 @@ test("transform tree (modifying tree incrementally)", () => { expect(state.renderedNodes.map(pluckFn("node.name"))).toEqual([ "root", "root-child-1", - "root-child-2" + "root-child-2", ]) // add first child to second child - node = state.root.find(function(node) { + node = state.root.find(function (node) { return node.name === "root-child-2" }) node.addChild(new TreeNode("root-child-2-child-1")) @@ -335,11 +335,11 @@ test("transform tree (modifying tree incrementally)", () => { "root", "root-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // add first child to first child - node = state.root.find(function(node) { + node = state.root.find(function (node) { return node.name === "root-child-1" }) node.addChild(new TreeNode("root-child-1-child-1")) @@ -354,11 +354,11 @@ test("transform tree (modifying tree incrementally)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // remove children from first child - node = state.root.find(function(node) { + node = state.root.find(function (node) { return node.name === "root-child-1" }) node.children.splice(0) @@ -372,11 +372,11 @@ test("transform tree (modifying tree incrementally)", () => { "root", "root-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // remove children from first child with no children should be a no-op - node = state.root.find(function(node) { + node = state.root.find(function (node) { return node.name === "root-child-1" }) node.children.splice(0) @@ -390,7 +390,7 @@ test("transform tree (modifying tree incrementally)", () => { "root", "root-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // remove children from root @@ -424,7 +424,7 @@ test("transform tree (modifying tree incrementally)", () => { let renderNodeCount = 0 let transformNode = createTransformer( - function(node) { + function (node) { nodeCreateCount++ return new DisplayNode(node) }, @@ -434,13 +434,13 @@ test("transform tree (modifying tree incrementally)", () => { ) // transform nodes to renderedNodes - m.autorun(function() { + m.autorun(function () { let renderedNodes = state.root ? state.root.map(transformNode) : [] state.renderedNodes.replace(renderedNodes) }) // render - m.autorun(function() { + m.autorun(function () { renderCount++ renderNodeCount += state.renderedNodes.length }) @@ -478,7 +478,7 @@ test("transform tree (modifying tree incrementally)", () => { "root-1-child-1b", "root-1-child-1b-child-1", "root-1-child-2b", - "root-1-child-2b-child-1" + "root-1-child-2b-child-1", ]) // remove root-1 @@ -511,7 +511,7 @@ test("transform tree (modifying tree incrementally)", () => { "root-2-child-1", "root-2-child-1-child-1", "root-2-child-2", - "root-2-child-2-child-1" + "root-2-child-2-child-1", ]) // teardown @@ -535,7 +535,7 @@ test("transform tree (modifying expanded)", () => { let renderNodeCount = 0 let transformNode = createTransformer( - function(node) { + function (node) { nodeCreateCount++ return new DisplayNode(node) }, @@ -545,24 +545,24 @@ test("transform tree (modifying expanded)", () => { ) // transform nodes to renderedNodes - m.autorun(function() { + m.autorun(function () { let renderedNodes = state.root ? state.root.transform(transformNode) : [] state.renderedNodes.replace(renderedNodes) }) // render - m.autorun(function() { + m.autorun(function () { renderCount++ renderNodeCount += state.renderedNodes.length }) // patch for collapsed - TreeNode.prototype.transform = function(iteratee, results) { + TreeNode.prototype.transform = function (iteratee, results) { if (this.parent && state.collapsed.has(this.parent.path())) return results || [] // not visible results = results || [] results.push(iteratee(this)) - this.children.forEach(function(child) { + this.children.forEach(function (child) { child.transform(iteratee, results) }) return results @@ -586,7 +586,7 @@ test("transform tree (modifying expanded)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) //////////////////////////////////// @@ -616,7 +616,7 @@ test("transform tree (modifying expanded)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // toggle child-1 collapsed @@ -631,7 +631,7 @@ test("transform tree (modifying expanded)", () => { "root", "root-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // toggle child-2-child-1 collapsed should be a no-op @@ -646,7 +646,7 @@ test("transform tree (modifying expanded)", () => { "root", "root-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // teardown @@ -671,7 +671,7 @@ test("transform tree (modifying render observable)", () => { let renderIconCalc = 0 let transformNode = createTransformer( - function(node) { + function (node) { nodeCreateCount++ return new DisplayNode(node) }, @@ -681,24 +681,24 @@ test("transform tree (modifying render observable)", () => { ) // transform nodes to renderedNodes - m.autorun(function() { + m.autorun(function () { let renderedNodes = state.root ? state.root.transform(transformNode) : [] state.renderedNodes.replace(renderedNodes) }) // render - m.autorun(function() { + m.autorun(function () { renderCount++ renderNodeCount += state.renderedNodes.length }) // custom transform - TreeNode.prototype.transform = function(iteratee, results) { + TreeNode.prototype.transform = function (iteratee, results) { node.icon.get() // icon dependency results = results || [] results.push(iteratee(this)) - this.children.forEach(function(child) { + this.children.forEach(function (child) { child.transform(iteratee, results) }) return results @@ -722,7 +722,7 @@ test("transform tree (modifying render observable)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) //////////////////////////////////// @@ -742,7 +742,7 @@ test("transform tree (modifying render observable)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // teardown @@ -767,7 +767,7 @@ test("transform tree (modifying render-only observable)", () => { let renderIconCalc = 0 let transformNode = createTransformer( - function(node) { + function (node) { nodeCreateCount++ return new DisplayNode(node) }, @@ -777,18 +777,18 @@ test("transform tree (modifying render-only observable)", () => { ) // transform nodes to renderedNodes - m.autorun(function() { + m.autorun(function () { let renderedNodes = state.root ? state.root.map(transformNode) : [] state.renderedNodes.replace(renderedNodes) }) // render - m.autorun(function() { + m.autorun(function () { renderCount++ renderNodeCount += state.renderedNodes.length - state.renderedNodes.forEach(function(renderedNode) { - m.autorun(function() { + state.renderedNodes.forEach(function (renderedNode) { + m.autorun(function () { renderIconCalc++ renderedNode.node.icon.get() // icon dependency }) @@ -814,7 +814,7 @@ test("transform tree (modifying render-only observable)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) //////////////////////////////////// @@ -835,7 +835,7 @@ test("transform tree (modifying render-only observable)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // teardown @@ -860,7 +860,7 @@ test("transform tree (static tags / global filter only)", () => { let renderNodeCount = 0 let transformNode = createTransformer( - function(node) { + function (node) { nodeCreateCount++ return new DisplayNode(node) }, @@ -870,13 +870,13 @@ test("transform tree (static tags / global filter only)", () => { ) // transform nodes to renderedNodes - m.autorun(function() { + m.autorun(function () { let renderedNodes = state.root ? state.root.transform(transformNode) : [] state.renderedNodes.replace(renderedNodes) }) // render - m.autorun(function() { + m.autorun(function () { renderCount++ renderNodeCount += state.renderedNodes.length }) @@ -885,11 +885,11 @@ test("transform tree (static tags / global filter only)", () => { state.tags = m.observable.array([]) // custom transform - TreeNode.prototype.transform = function(iteratee, results) { + TreeNode.prototype.transform = function (iteratee, results) { results = results || [] if (!state.tags.length || intersection(state.tags, this.tags).length) results.push(iteratee(this)) - this.children.forEach(function(child) { + this.children.forEach(function (child) { child.transform(iteratee, results) }) return results @@ -913,7 +913,7 @@ test("transform tree (static tags / global filter only)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) //////////////////////////////////// @@ -942,7 +942,7 @@ test("transform tree (static tags / global filter only)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // add search tag @@ -958,7 +958,7 @@ test("transform tree (static tags / global filter only)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // remove search tags @@ -992,7 +992,7 @@ test("transform tree (dynamic tags - peek / rebuild)", () => { let renderNodeCount = 0 let transformNode = createTransformer( - function(node) { + function (node) { nodeCreateCount++ return new DisplayNode(node) }, @@ -1002,13 +1002,13 @@ test("transform tree (dynamic tags - peek / rebuild)", () => { ) // transform nodes to renderedNodes - m.autorun(function() { + m.autorun(function () { let renderedNodes = state.root ? state.root.transform(transformNode) : [] state.renderedNodes.replace(renderedNodes) }) // render - m.autorun(function() { + m.autorun(function () { renderCount++ renderNodeCount += state.renderedNodes.length }) @@ -1017,11 +1017,11 @@ test("transform tree (dynamic tags - peek / rebuild)", () => { state.tags = m.observable.array([]) // custom transform - TreeNode.prototype.transform = function(iteratee, results) { + TreeNode.prototype.transform = function (iteratee, results) { results = results || [] if (!state.tags.length || intersection(state.tags, this.tags).length) results.push(iteratee(this)) - this.children.forEach(function(child) { + this.children.forEach(function (child) { child.transform(iteratee, results) }) return results @@ -1049,7 +1049,7 @@ test("transform tree (dynamic tags - peek / rebuild)", () => { "root-child-1", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) //////////////////////////////////// @@ -1077,24 +1077,24 @@ test("transform tree (dynamic tags - peek / rebuild)", () => { expect(state.renderedNodes.map(pluckFn("node.name"))).toEqual([ "root", "root-child-1", - "root-child-2" + "root-child-2", ]) // perform multiple search tag operations - m.transaction(function() { + m.transaction(function () { state.root.tags.shift() // no-op state.root - .find(function(node) { + .find(function (node) { return node.name === "root-child-1" }) .tags.splice(0) state.root - .find(function(node) { + .find(function (node) { return node.name === "root-child-1-child-1" }) .tags.push(2) state.root - .find(function(node) { + .find(function (node) { return node.name === "root-child-2-child-1" }) .tags.push(2) @@ -1109,7 +1109,7 @@ test("transform tree (dynamic tags - peek / rebuild)", () => { "root", "root-child-1-child-1", "root-child-2", - "root-child-2-child-1" + "root-child-2-child-1", ]) // teardown @@ -1128,19 +1128,19 @@ test("transform with primitive key", () => { m.extendObservable(this, { get name() { return "Bob" + this.num - } + }, }) } let observableBobs = m.observable([]) let bobs = [] - let bobFactory = createTransformer(function(key) { + let bobFactory = createTransformer(function (key) { return new Bob() }) - m.autorun(function() { - bobs = observableBobs.map(function(bob) { + m.autorun(function () { + bobs = observableBobs.map(function (bob) { return bobFactory(bob) }) }) @@ -1167,7 +1167,7 @@ test("transform with primitive key", () => { // https://github.com/mobxjs/mobx-utils/issues/134 test("transform with mixed key types", () => { - const identityTransformer = createTransformer(a => a) + const identityTransformer = createTransformer((a) => a) let transformedStringKey let transformedNumberKey @@ -1182,7 +1182,7 @@ test("transform with mixed key types", () => { import * as intersection from "lodash.intersection" function pluckFn(key) { - return function(obj) { + return function (obj) { let keys = key.split("."), value = obj for (let i = 0, l = keys.length; i < l; i++) { @@ -1203,14 +1203,14 @@ function createTestSet(): any { let state = (testSet.state = m.observable({ root: null, renderedNodes: m.observable.array(), - collapsed: m.observable.map() // KM: ideally, I would like to use a set + collapsed: m.observable.map(), // KM: ideally, I would like to use a set })) let stats = (testSet.stats = { - refCount: 0 + refCount: 0, }) - let TreeNode = (testSet.TreeNode = function(name, extensions) { + let TreeNode = (testSet.TreeNode = function (name, extensions) { this.children = m.observable.array() this.icon = m.observable.box("folder") @@ -1224,19 +1224,19 @@ function createTestSet(): any { } } }) - TreeNode.prototype.addChild = function(node) { + TreeNode.prototype.addChild = function (node) { node.parent = this this.children.push(node) } - TreeNode.prototype.addChildren = function(nodes) { + TreeNode.prototype.addChildren = function (nodes) { let _this = this - nodes.map(function(node) { + nodes.map(function (node) { node.parent = _this }) this.children.splice.apply(this.children, [this.children.length, 0].concat(nodes)) } - TreeNode.prototype.path = function() { + TreeNode.prototype.path = function () { let node = this, parts = [] while (node) { @@ -1246,16 +1246,16 @@ function createTestSet(): any { return parts.join("/") } - TreeNode.prototype.map = function(iteratee, results) { + TreeNode.prototype.map = function (iteratee, results) { results = results || [] results.push(iteratee(this)) - this.children.forEach(function(child) { + this.children.forEach(function (child) { child.map(iteratee, results) }) return results } - TreeNode.prototype.find = function(predicate) { + TreeNode.prototype.find = function (predicate) { if (predicate(this)) return this let result @@ -1266,15 +1266,15 @@ function createTestSet(): any { return null } - let DisplayNode = (testSet.DisplayNode = function(node) { + let DisplayNode = (testSet.DisplayNode = function (node) { stats.refCount++ this.node = node }) - DisplayNode.prototype.destroy = function() { + DisplayNode.prototype.destroy = function () { stats.refCount-- } - DisplayNode.prototype.toggleCollapsed = function() { + DisplayNode.prototype.toggleCollapsed = function () { let path = this.node.path() state.collapsed.has(path) ? state.collapsed.delete(path) : state.collapsed.set(path, true) // KM: ideally, I would like to use a set } @@ -1287,13 +1287,13 @@ it("should throw error when passed invalid param type", () => { const validParams = [[], {}, jest.fn(), 1, "string"] const invalidParams = [null, undefined, Symbol("A"), true] - validParams.forEach(obj => { + validParams.forEach((obj) => { expect(() => { transformedFn(obj) }).not.toThrowError() }) - invalidParams.forEach(obj => { + invalidParams.forEach((obj) => { expect(() => { transformedFn(obj) }).toThrowErrorMatchingSnapshot() @@ -1302,28 +1302,28 @@ it("should throw error when passed invalid param type", () => { test("should respect onCleanup argument", () => { let state = m.observable({ todos: [{ title: "coffee" }], - name: "michel" + name: "michel", }) let mapped let unloaded = [] let objectName - let transformState = createTransformer(function(state: any) { + let transformState = createTransformer(function (state: any) { return state.name + state.todos.map(transformTodo).join(",") }) let transformTodo = createTransformer( - function(todo: any) { + function (todo: any) { return todo.title.toUpperCase() }, { - onCleanup: function(text, value) { + onCleanup: function (text, value) { unloaded.push([text, value]) }, - debugNameGenerator: function(todo) { + debugNameGenerator: function (todo) { return todo.title.toUpperCase() + "-DEBUG" - } + }, } ) - m.autorun(function() { + m.autorun(function () { mapped = transformState(state) }) state.todos.shift() @@ -1333,21 +1333,21 @@ test("should respect onCleanup argument", () => { test("should respect debugNameGenerator argument", () => { let state = m.observable.map({ title: "coffee", - name: "michel" + name: "michel", }) let mapped let objectName let transformTodo = createTransformer( - function(state: any) { + function (state: any) { return state.get("title").toUpperCase() }, { - debugNameGenerator: function(state) { + debugNameGenerator: function (state) { return state.get("title").toUpperCase() + "-DEBUG" - } + }, } ) - m.autorun(function() { + m.autorun(function () { mapped = transformTodo(state) }) state.set("name", "BISCUIT") @@ -1358,8 +1358,8 @@ test("should respect debugNameGenerator argument", () => { test("supports computed value options", () => { const events: number[][] = [] const xs = m.observable([1, 2, 3]) - const xsLessThan = createTransformer(n => xs.filter(x => x < n), { - equals: m.comparer.structural + const xsLessThan = createTransformer((n) => xs.filter((x) => x < n), { + equals: m.comparer.structural, }) m.autorun(() => events.push(xsLessThan(3))) diff --git a/test/create-view-model.ts b/test/create-view-model.ts index 64aa7e4..db81355 100644 --- a/test/create-view-model.ts +++ b/test/create-view-model.ts @@ -22,7 +22,7 @@ function Todo(title, done, usersInterested, unobservedProp) { usersInterested: usersInterested, get usersCount() { return this.usersInterested.length - } + }, }) } diff --git a/test/deepObserve.ts b/test/deepObserve.ts index fddce6a..9731c01 100644 --- a/test/deepObserve.ts +++ b/test/deepObserve.ts @@ -59,11 +59,11 @@ test("deep", () => { { a: { b: { - c: 3 - } - } + c: 3, + }, + }, }, - x => { + (x) => { x.a.b.c = 4 } ) @@ -76,7 +76,7 @@ test("add", () => { }) test("delete", () => { - assertChanges({ x: 1 }, x => { + assertChanges({ x: 1 }, (x) => { delete x.x }) }) diff --git a/test/expr.ts b/test/expr.ts index a8ed29f..fd45421 100644 --- a/test/expr.ts +++ b/test/expr.ts @@ -3,7 +3,7 @@ import * as utils from "../src/mobx-utils" import * as mobx from "mobx" -test("expr", function() { +test("expr", function () { mobx.configure({ enforceActions: "never" }) try { let factor = mobx.observable.box(0) @@ -11,11 +11,11 @@ test("expr", function() { let totalCalcs = 0 let innerCalcs = 0 - let total = mobx.computed(function() { + let total = mobx.computed(function () { totalCalcs += 1 // outer observable shouldn't recalc if inner observable didn't publish a real change return ( price.get() * - utils.expr(function() { + utils.expr(function () { innerCalcs += 1 return factor.get() % 2 === 0 ? 1 : 3 }) @@ -25,7 +25,7 @@ test("expr", function() { let b = [] let sub = mobx.observe( total, - function(x) { + function (x) { b.push(x.newValue) }, true @@ -46,7 +46,7 @@ test("expr", function() { } }) -test("expr2", function() { +test("expr2", function () { mobx.configure({ enforceActions: "never" }) try { let factor = mobx.observable.box(0) @@ -54,11 +54,11 @@ test("expr2", function() { let totalCalcs = 0 let innerCalcs = 0 - let total = mobx.computed(function() { + let total = mobx.computed(function () { totalCalcs += 1 // outer observable shouldn't recalc if inner observable didn't publish a real change return ( price.get() * - utils.expr(function() { + utils.expr(function () { innerCalcs += 1 return factor.get() % 2 === 0 ? 1 : 3 }) @@ -68,7 +68,7 @@ test("expr2", function() { let b = [] let sub = mobx.observe( total, - function(x) { + function (x) { b.push(x.newValue) }, true diff --git a/test/from-promise.js b/test/from-promise.js index 18bcafe..c1923b3 100644 --- a/test/from-promise.js +++ b/test/from-promise.js @@ -5,8 +5,8 @@ const mobx = require("mobx") mobx.configure({ enforceActions: "observed" }) -test("resolves", done => { - const p = new Promise(resolve => resolve(7)) +test("resolves", (done) => { + const p = new Promise((resolve) => resolve(7)) const obs = utils.fromPromise(p) expect(obs.value).toBe(undefined) @@ -23,8 +23,8 @@ test("resolves", done => { ) }) -test("old state is undefined", done => { - const p = new Promise(resolve => resolve(7)) +test("old state is undefined", (done) => { + const p = new Promise((resolve) => resolve(7)) const obs = utils.fromPromise(p, undefined) expect(obs.value).toBe(undefined) expect(obs.state).toBe("pending") @@ -38,12 +38,12 @@ test("old state is undefined", done => { ) }) -test("resolves old state", done => { - const oldP = utils.fromPromise(new Promise(resolve => resolve(9))) +test("resolves old state", (done) => { + const oldP = utils.fromPromise(new Promise((resolve) => resolve(9))) mobx.when( () => oldP.state == "fulfilled", () => { - const p = new Promise(resolve => resolve(7)) + const p = new Promise((resolve) => resolve(7)) const obs = utils.fromPromise(p, oldP) expect(obs.value).toBe(9) expect(obs.state).toBe("pending") @@ -52,12 +52,12 @@ test("resolves old state", done => { ) }) -test("resolves new state", done => { - const oldP = utils.fromPromise(new Promise(resolve => resolve(9))) +test("resolves new state", (done) => { + const oldP = utils.fromPromise(new Promise((resolve) => resolve(9))) mobx.when( () => oldP.state == "fulfilled", () => { - const p = new Promise(resolve => resolve(7)) + const p = new Promise((resolve) => resolve(7)) const obs = utils.fromPromise(p, oldP) mobx.when( () => obs.state === "fulfilled", @@ -70,8 +70,8 @@ test("resolves new state", done => { ) }) -test("rejects new state", done => { - const oldP = utils.fromPromise(new Promise(resolve => resolve(9))) +test("rejects new state", (done) => { + const oldP = utils.fromPromise(new Promise((resolve) => resolve(9))) mobx.when( () => oldP.state == "fulfilled", () => { @@ -90,8 +90,8 @@ test("rejects new state", done => { ) }) -test("resolves value", done => { - const p = new Promise(resolve => resolve(7)) +test("resolves value", (done) => { + const p = new Promise((resolve) => resolve(7)) const obs = utils.fromPromise(p) expect(obs.value).toBe(undefined) @@ -106,8 +106,8 @@ test("resolves value", done => { ) }) -test("resolves value from promise function", done => { - const obs = utils.fromPromise(resolve => resolve(7)) +test("resolves value from promise function", (done) => { + const obs = utils.fromPromise((resolve) => resolve(7)) expect(obs.value).toBe(undefined) expect(obs.state).toBe("pending") @@ -120,7 +120,7 @@ test("resolves value from promise function", done => { ) }) -test("rejects with reason value", done => { +test("rejects with reason value", (done) => { const p = new Promise((resolve, reject) => { reject(7) }) @@ -143,7 +143,7 @@ test("rejects with reason value", done => { ) }) -test("rejects with reason value from fn", done => { +test("rejects with reason value from fn", (done) => { const obs = utils.fromPromise( new Promise((resolve, reject) => { reject(undefined) @@ -163,7 +163,7 @@ test("rejects with reason value from fn", done => { ) }) -test("rejects when throwing", done => { +test("rejects when throwing", (done) => { const p = new Promise(() => { throw 7 }) @@ -183,14 +183,14 @@ test("rejects when throwing", done => { ) }) -test("case method, fulfillment", done => { +test("case method, fulfillment", (done) => { const p = Promise.resolve() const obs = utils.fromPromise(p) let mapping = { pending: () => 1, - fulfilled: x => 2, - rejected: y => 3 + fulfilled: (x) => 2, + rejected: (y) => 3, } let mapped = obs.case(mapping) @@ -205,7 +205,7 @@ test("case method, fulfillment", done => { ) }) -test("case method, rejection", done => { +test("case method, rejection", (done) => { const p = Promise.reject() p.then( () => {}, @@ -217,8 +217,8 @@ test("case method, rejection", done => { let mapping = { pending: () => 1, - fulfilled: x => 2, - rejected: y => 3 + fulfilled: (x) => 2, + rejected: (y) => 3, } let mapped = obs.case(mapping) @@ -233,7 +233,7 @@ test("case method, rejection", done => { ) }) -test("case method, returns fulfilled value by default", done => { +test("case method, returns fulfilled value by default", (done) => { const p = Promise.resolve(2) const obs = utils.fromPromise(p) @@ -251,7 +251,7 @@ test("case method, returns fulfilled value by default", done => { ) }) -test("case method, returns undefined when handler is missing", done => { +test("case method, returns undefined when handler is missing", (done) => { const p = Promise.resolve() const obs = utils.fromPromise(p) @@ -285,10 +285,10 @@ test("state and value are observable, #56", () => { expect(mobx.isObservableProp(obs, "value")).toBeTruthy() }) -test("the resolved value of a promise is not convertd to some deep observable, #54", done => { +test("the resolved value of a promise is not convertd to some deep observable, #54", (done) => { const someObject = { a: 3 } const obs = utils.fromPromise(Promise.resolve(someObject)) - obs.then(v => { + obs.then((v) => { expect(obs.state).toBe(utils.FULFILLED) expect(mobx.isObservable(obs.value)).toBeFalsy() expect(obs.value === someObject).toBeTruthy() @@ -297,14 +297,14 @@ test("the resolved value of a promise is not convertd to some deep observable, # }) }) -test("it is possible to create a promise in a rejected state, #36", done => { +test("it is possible to create a promise in a rejected state, #36", (done) => { const someObject = { a: 3 } const obs = utils.fromPromise.reject(someObject) expect(obs.state).toBe(utils.REJECTED) expect(obs.value).toBe(someObject) // still a real promise backing it, which can be thenned... - obs.catch(v => { + obs.catch((v) => { expect(obs.state).toBe(utils.REJECTED) expect(mobx.isObservable(obs.value)).toBeFalsy() expect(obs.value === someObject).toBeTruthy() @@ -313,14 +313,14 @@ test("it is possible to create a promise in a rejected state, #36", done => { }) }) -test("it is possible to create a promise in a fulfilled state, #36", done => { +test("it is possible to create a promise in a fulfilled state, #36", (done) => { const someObject = { a: 3 } const obs = utils.fromPromise.resolve(someObject) expect(obs.state).toBe(utils.FULFILLED) expect(obs.value).toBe(someObject) // still a real promise backing it, which can be thenned... - obs.then(v => { + obs.then((v) => { expect(obs.state).toBe(utils.FULFILLED) expect(mobx.isObservable(obs.value)).toBeFalsy() expect(obs.value === someObject).toBeTruthy() @@ -329,7 +329,7 @@ test("it is possible to create a promise in a fulfilled state, #36", done => { }) }) -test("when creating a promise in a fulfilled state it should not fire twice, #36", done => { +test("when creating a promise in a fulfilled state it should not fire twice, #36", (done) => { let events = 0 const obs = utils.fromPromise.resolve(3) @@ -339,7 +339,7 @@ test("when creating a promise in a fulfilled state it should not fire twice, #36 events++ }) - obs.then(v => { + obs.then((v) => { expect(events).toBe(1) // only initial run should have run done() }) @@ -347,7 +347,7 @@ test("when creating a promise in a fulfilled state it should not fire twice, #36 test("it creates a real promise, #45", () => { return Promise.all([utils.fromPromise.resolve(2), utils.fromPromise(Promise.resolve(3))]).then( - x => { + (x) => { expect(x).toEqual([2, 3]) } ) @@ -358,8 +358,8 @@ test("it can construct new promises from function, #45", () => { utils.fromPromise((resolve, reject) => { setTimeout(() => resolve(2), 200) }), - utils.fromPromise(Promise.resolve(3)) - ]).then(x => { + utils.fromPromise(Promise.resolve(3)), + ]).then((x) => { expect(x).toEqual([2, 3]) }) }) diff --git a/test/from-resource.js b/test/from-resource.js index 94a8b35..508d1f0 100644 --- a/test/from-resource.js +++ b/test/from-resource.js @@ -9,11 +9,11 @@ function Record(name) { this.data = { name: name } this.subscriptions = [] } -Record.prototype.updateName = function(newName) { +Record.prototype.updateName = function (newName) { this.data.name = newName - this.subscriptions.forEach(f => f()) + this.subscriptions.forEach((f) => f()) } -Record.prototype.subscribe = function(cb) { +Record.prototype.subscribe = function (cb) { this.subscriptions.push(cb) return () => { const idx = this.subscriptions.indexOf(cb) @@ -25,7 +25,7 @@ Record.prototype.subscribe = function(cb) { function createObservable(record) { let subscription return utils.fromResource( - sink => { + (sink) => { sink(record.data) subscription = record.subscribe(() => { sink(record.data) @@ -38,7 +38,7 @@ function createObservable(record) { test("basics", () => { let base = console.warn // eslint-disable-line no-console let warn = [] - console.warn = msg => warn.push(msg) // eslint-disable-line no-console + console.warn = (msg) => warn.push(msg) // eslint-disable-line no-console var me = new Record("michel") var me$ = createObservable(me) @@ -69,7 +69,7 @@ test("basics", () => { // test warning expect(me$.current().name).toBe("noa") // happens to be visible through the data reference, but no autorun tragger expect(warn).toEqual([ - "Called `get` of a subscribingObservable outside a reaction. Current value will be returned but no new subscription has started" + "Called `get` of a subscribingObservable outside a reaction. Current value will be returned but no new subscription has started", ]) // resubscribe diff --git a/test/guarded-when.js b/test/guarded-when.js index 16834ea..9142426 100644 --- a/test/guarded-when.js +++ b/test/guarded-when.js @@ -5,25 +5,39 @@ const mobx = require("mobx") mobx.configure({ enforceActions: "observed" }) -test("whenWithTimeout should operate normally", done => { +test("whenWithTimeout should operate normally", (done) => { var a = mobx.observable.box(1) - utils.whenWithTimeout(() => a.get() === 2, () => done(), 500, () => done.fail()) - - setTimeout(mobx.action(() => a.set(2)), 200) -}) - -test("whenWithTimeout should timeout", done => { - const a = mobx.observable.box(1) - - utils.whenWithTimeout(() => a.get() === 2, () => done.fail("should have timed out"), 500, () => - done() + utils.whenWithTimeout( + () => a.get() === 2, + () => done(), + 500, + () => done.fail() ) - setTimeout(mobx.action(() => a.set(2)), 1000) + setTimeout( + mobx.action(() => a.set(2)), + 200 + ) }) -test("whenWithTimeout should dispose", done => { +test("whenWithTimeout should timeout", (done) => { + const a = mobx.observable.box(1) + + utils.whenWithTimeout( + () => a.get() === 2, + () => done.fail("should have timed out"), + 500, + () => done() + ) + + setTimeout( + mobx.action(() => a.set(2)), + 1000 + ) +}) + +test("whenWithTimeout should dispose", (done) => { const a = mobx.observable.box(1) const d1 = utils.whenWithTimeout( diff --git a/test/keep-alive.js b/test/keep-alive.js index 4c88205..a437634 100644 --- a/test/keep-alive.js +++ b/test/keep-alive.js @@ -48,7 +48,7 @@ test("keep alive should work for properties", () => { get doubler() { calcs++ return this.a * 2 - } + }, }) x.doubler diff --git a/test/lazy-observable.js b/test/lazy-observable.js index b4bf540..d135d3f 100644 --- a/test/lazy-observable.js +++ b/test/lazy-observable.js @@ -5,9 +5,9 @@ const mobx = require("mobx") mobx.configure({ enforceActions: "observed" }) -test("lazy observable should work", done => { +test("lazy observable should work", (done) => { let started = false - const lo = utils.lazyObservable(sink => { + const lo = utils.lazyObservable((sink) => { started = true setTimeout(() => sink(4), 50) setTimeout(() => sink(5), 100) @@ -33,17 +33,17 @@ test("lazy observable should work", done => { }, 200) }) -test("lazy observable refresh", done => { +test("lazy observable refresh", (done) => { let started = 0 let i = 10 const lo = utils.lazyObservable( - sink => - new Promise(resolve => { + (sink) => + new Promise((resolve) => { started = started + 1 resolve(i) i++ - }).then(value => { + }).then((value) => { sink(value) }), 1 @@ -66,12 +66,12 @@ test("lazy observable refresh", done => { }, 200) }) -test("lazy observable reset", done => { +test("lazy observable reset", (done) => { const lo = utils.lazyObservable( - sink => - new Promise(resolve => { + (sink) => + new Promise((resolve) => { resolve(2) - }).then(value => { + }).then((value) => { sink(value) }), 1 @@ -95,9 +95,9 @@ test("lazy observable reset", done => { }, 250) }) -test("lazy observable pending", done => { - const lo = utils.lazyObservable(sink => - new Promise(resolve => { +test("lazy observable pending", (done) => { + const lo = utils.lazyObservable((sink) => + new Promise((resolve) => { setTimeout(resolve, 100) }).then(sink) ) diff --git a/test/now.js b/test/now.js index b5e6c48..a609e31 100644 --- a/test/now.js +++ b/test/now.js @@ -3,7 +3,7 @@ const utils = require("../src/mobx-utils") const mobx = require("mobx") -test("now should tick", done => { +test("now should tick", (done) => { const values = [] const d = mobx.autorun(() => { utils.now(100) @@ -19,7 +19,7 @@ test("now should tick", done => { }, 500) }) -test("now should be up to date outside reaction, #40", done => { +test("now should be up to date outside reaction, #40", (done) => { const d1 = utils.now(1000) expect(typeof d1 === "number").toBeTruthy() setTimeout(() => { diff --git a/test/observable-stream.ts b/test/observable-stream.ts index 3bbf673..e356a78 100644 --- a/test/observable-stream.ts +++ b/test/observable-stream.ts @@ -6,7 +6,7 @@ import { map } from "rxjs/operators" test("to observable - should push the initial value by default", () => { const user = mobx.observable({ firstName: "C.S", - lastName: "Lewis" + lastName: "Lewis", }) mobx.configure({ enforceActions: "never" }) @@ -14,8 +14,8 @@ test("to observable - should push the initial value by default", () => { let values = [] const sub = from(utils.toStream(() => user.firstName + user.lastName, true)) - .pipe(map(x => x.toUpperCase())) - .subscribe(v => values.push(v)) + .pipe(map((x) => x.toUpperCase())) + .subscribe((v) => values.push(v)) user.firstName = "John" @@ -34,7 +34,7 @@ test("to observable - should push the initial value by default", () => { test("to observable - should not push the initial value", () => { const user = mobx.observable({ firstName: "C.S", - lastName: "Lewis" + lastName: "Lewis", }) mobx.configure({ enforceActions: "never" }) @@ -42,8 +42,8 @@ test("to observable - should not push the initial value", () => { let values = [] const sub = from(utils.toStream(() => user.firstName + user.lastName)) - .pipe(map(x => x.toUpperCase())) - .subscribe(v => values.push(v)) + .pipe(map((x) => x.toUpperCase())) + .subscribe((v) => values.push(v)) user.firstName = "John" @@ -59,7 +59,7 @@ test("to observable - should not push the initial value", () => { expect(values).toEqual(["JOHNLEWIS", "JANEJACK"]) }) -test("from observable", done => { +test("from observable", (done) => { mobx.configure({ enforceActions: "observed" }) const fromStream = utils.fromStream(interval(10), -1) const values = [] diff --git a/test/queue-processor.js b/test/queue-processor.js index 92a791a..d351c07 100644 --- a/test/queue-processor.js +++ b/test/queue-processor.js @@ -9,7 +9,7 @@ test("sync processor should work", () => { const q = mobx.observable([1, 2]) const res = [] - const stop = utils.queueProcessor(q, v => res.push(v * 2)) + const stop = utils.queueProcessor(q, (v) => res.push(v * 2)) expect(res).toEqual([2, 4]) expect(q.length).toBe(0) @@ -36,11 +36,11 @@ test("sync processor should work", () => { expect(res).toEqual([2, 4, 6, 8, 10, 12, 14]) }) -test("async processor should work", done => { +test("async processor should work", (done) => { const q = mobx.observable([1, 2]) const res = [] - const stop = utils.queueProcessor(q, v => res.push(v * 2), 10) + const stop = utils.queueProcessor(q, (v) => res.push(v * 2), 10) expect(res.length).toBe(0) expect(q.length).toBe(2) diff --git a/yarn.lock b/yarn.lock index 5c5389f..b81c7b8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5121,10 +5121,10 @@ prepend-http@^1.0.0: resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-1.0.4.tgz#d4f4562b0ce3696e41ac52d0e002e57a635dc6dc" integrity sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw= -prettier@^1.7.2: - version "1.18.2" - resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea" - integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw== +prettier@^2.0.4: + version "2.0.4" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.4.tgz#2d1bae173e355996ee355ec9830a7a1ee05457ef" + integrity sha512-SVJIQ51spzFDvh4fIbCLvciiDMCrRhlN3mbZvv/+ycjvmF5E73bKdGfU8QDLNmjYJf+lsGnDBC4UUnvTe5OO0w== pretty-format@^24.9.0: version "24.9.0"