Skip to content

Commit

Permalink
Merge 0612cb1 into c74c9f8
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Dec 24, 2018
2 parents c74c9f8 + 0612cb1 commit 537135b
Show file tree
Hide file tree
Showing 8 changed files with 692 additions and 673 deletions.
1,244 changes: 632 additions & 612 deletions CHANGELOG.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -77,6 +77,7 @@
"tape": "^4.2.2",
"ts-jest": "^22.0.0",
"tslib": "^1.7.1",
"tslint-config-prettier": "^1.17.0",
"typescript": "^3.2.1",
"uglify-es": "^3.3.9"
},
Expand Down
49 changes: 38 additions & 11 deletions src/api/flow.ts
Expand Up @@ -4,16 +4,31 @@ let generatorId = 0

export type CancellablePromise<T> = Promise<T> & { cancel(): void }

export interface FlowIterator<T> {
next(value?: any): IteratorResult<T> | Promise<IteratorResult<T>>
return?(value?: any): IteratorResult<T> | Promise<IteratorResult<T>>
throw?(e?: any): IteratorResult<T> | Promise<IteratorResult<T>>
export interface FlowYield {
// fake, only for typing
"!!flowYield": undefined
}

export function flow<T, U extends any[]>(
generator: (...args: U) => FlowIterator<any>
): (...args: U) => CancellablePromise<T>
export function flow(generator: Function): Function {
export interface FlowReturn<T> {
// fake, only for typing
"!!flowReturn": T
}

// we skip promises that are the result of yielding promises (except if they use flowReturn)
export type FlowReturnType<R> = IfAllAreFlowYieldThenVoid<
R extends FlowReturn<infer FR>
? FR extends Promise<infer FRP> ? FRP : FR
: R extends Promise<any> ? FlowYield : R
>

// we extract yielded promises from the return type
export type IfAllAreFlowYieldThenVoid<R> = Exclude<R, FlowYield> extends never
? void
: Exclude<R, FlowYield>

export function flow<R, Args extends any[]>(
generator: (...args: Args) => IterableIterator<R>
): (...args: Args) => CancellablePromise<FlowReturnType<R>> {
if (arguments.length !== 1)
fail(process.env.NODE_ENV && `Flow expects one 1 argument and cannot be used as decorator`)
const name = generator.name || "<unnamed flow>"
Expand All @@ -27,7 +42,7 @@ export function flow(generator: Function): Function {
let rejector: (error: any) => void
let pendingPromise: CancellablePromise<any> | undefined = undefined

const res = new Promise(function(resolve, reject) {
const promise = new Promise<R>(function(resolve, reject) {
let stepId = 0
rejector = reject

Expand Down Expand Up @@ -74,7 +89,7 @@ export function flow(generator: Function): Function {
onFulfilled(undefined) // kick off the process
}) as any

res.cancel = action(`${name} - runid: ${runId} - cancel`, function() {
promise.cancel = action(`${name} - runid: ${runId} - cancel`, function() {
try {
if (pendingPromise) cancelPromise(pendingPromise)
// Finally block can return (or yield) stuff..
Expand All @@ -89,10 +104,22 @@ export function flow(generator: Function): Function {
rejector(e) // there could be a throwing finally block
}
})
return res
return promise as CancellablePromise<FlowReturnType<R>>
}
}

function cancelPromise(promise) {
if (typeof promise.cancel === "function") promise.cancel()
}

/**
* Used for TypeScript to make flows that return a promise return the actual promise result.
*
* @export
* @template T
* @param {T} val
* @returns {FlowReturn<T>}
*/
export function castFlowReturn<T>(val: T): FlowReturn<T> {
return val as any
}
3 changes: 2 additions & 1 deletion src/mobx.ts
Expand Up @@ -29,7 +29,7 @@ try {
// (in which case the expression below would be substituted with 'production')
process.env.NODE_ENV
} catch (e) {
var g = typeof window !== "undefined" ? window : global
const g = typeof window !== "undefined" ? window : global
if (typeof process === "undefined") g.process = {}
g.process.env = {}
}
Expand Down Expand Up @@ -125,6 +125,7 @@ export {
onBecomeObserved,
onBecomeUnobserved,
flow,
castFlowReturn,
toJS,
trace,
IObserverTree,
Expand Down
1 change: 1 addition & 0 deletions test/base/api.js
Expand Up @@ -20,6 +20,7 @@ test("correct api should be exposed", function() {
"decorate",
"extendObservable",
"flow",
"castFlowReturn",
"get",
"_getAdministration",
"getAtom",
Expand Down
6 changes: 5 additions & 1 deletion test/base/flow.js
@@ -1,5 +1,5 @@
import * as mobx from "../../src/mobx.ts"
const { flow } = mobx
import { castFlowReturn, flow } from "../../src/mobx"

function delay(time, value, shouldThrow = false) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -349,3 +349,7 @@ test("cancelled flow should not result in runaway reject", async () => {
expect("" + e).toBe("Error: FLOW_CANCELLED")
}
})

test("castFlowReturn should return the passed argument", () => {
expect(castFlowReturn(5)).toBe(5)
})
56 changes: 8 additions & 48 deletions tslint.json
@@ -1,55 +1,26 @@
{
"extends": ["tslint-config-prettier"],
"rules": {
"class-name": true,
"comment-format": [
true,
"check-space"
],
"comment-format": [true, "check-space"],
"curly": false,
"indent": [
true,
"tabs"
],
"interface-name": true,
"interface-name": false,
"jsdoc-format": true,
"no-consecutive-blank-lines" : true,
"no-consecutive-blank-lines": true,
"no-debugger": true,
"no-duplicate-key": true,
"no-duplicate-variable": true,
"no-eval": true,
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-shadowed-variable": true,
"no-switch-case-fall-through": true,
"no-unreachable": true,
"no-unused-expression": true,
"no_unused-variable": [
true
],
"no_unused-variable": [true],
"no-use-before-declare": false,
"no-var-keyword": true,
"one-line": [
true,
"check-open-brace",
"check-whitespace",
"check-catch"
],
"quotemark": [
true,
"double"
],
"semicolon": true,
"trailing-comma": [
true,
{
"multiline": "never",
"singleline": "never"
}
],
"triple-equals": [
true,
"allow-null-check"
],
"one-line": [true, "check-open-brace", "check-whitespace", "check-catch"],
"triple-equals": [true, "allow-null-check"],
"typedef-whitespace": [
true,
{
Expand All @@ -60,17 +31,6 @@
"variable-declaration": "nospace"
}
],
"variable-name": [
true,
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
"variable-name": [true, "ban-keywords"]
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -6031,6 +6031,11 @@ tslib@^1.7.1:
version "1.9.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.1.tgz#a5d1f0532a49221c87755cfcc89ca37197242ba7"

tslint-config-prettier@^1.17.0:
version "1.17.0"
resolved "https://registry.yarnpkg.com/tslint-config-prettier/-/tslint-config-prettier-1.17.0.tgz#946ed6117f98f3659a65848279156d87628c33dc"
integrity sha512-NKWNkThwqE4Snn4Cm6SZB7lV5RMDDFsBwz6fWUkTxOKGjMx8ycOHnjIbhn7dZd5XmssW3CwqUjlANR6EhP9YQw==

tty-browserify@0.0.0, tty-browserify@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.0.tgz#a157ba402da24e9bf957f9aa69d524eed42901a6"
Expand Down

0 comments on commit 537135b

Please sign in to comment.