UNPKG

4.86 kBSource Map (JSON)View Raw
1{"version":3,"file":"observableToPromise.js","sourceRoot":"","sources":["../../../src/testing/core/observableToPromise.ts"],"names":[],"mappings":";AAyBA,MAAM,UAAU,kCAAkC,CAChD,EAA6E;QAA3E,UAAU,gBAAA,EAAE,qBAAoB,EAApB,aAAa,mBAAG,IAAI,KAAA,EAAE,YAAS,EAAT,IAAI,mBAAG,CAAC,CAAC,KAAA,EAAE,sBAAmB,EAAnB,cAAc,mBAAG,EAAE,KAAA;IAClE,aAAwB;SAAxB,UAAwB,EAAxB,qBAAwB,EAAxB,IAAwB;QAAxB,4BAAwB;;IAExB,IAAI,YAAY,GAA2B,IAAa,CAAC;IACzD,IAAM,OAAO,GAAG,IAAI,OAAO,CAAQ,UAAC,OAAO,EAAE,MAAM;QACjD,IAAI,UAAU,GAAG,CAAC,CAAC;QACnB,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,IAAM,OAAO,GAAU,EAAE,CAAC;QAE1B,IAAM,YAAY,GAAG;YACnB,IAAI,CAAC,aAAa,EAAE;gBAClB,OAAO;aACR;YAED,IAAM,IAAI,GAAG;gBACX,YAAY,CAAC,WAAW,EAAE,CAAC;gBAE3B,OAAO,CAAC,OAAO,CAAC,CAAC;YACnB,CAAC,CAAC;YAEF,IAAI,OAAO,KAAK,GAAG,CAAC,MAAM,IAAI,UAAU,KAAK,cAAc,CAAC,MAAM,EAAE;gBAClE,IAAI,IAAI,KAAK,CAAC,CAAC,EAAE;oBACf,IAAI,EAAE,CAAC;iBACR;qBAAM;oBACL,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;iBACxB;aACF;QACH,CAAC,CAAC;QAEF,IAAI,KAAK,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;QAE9B,YAAY,GAAG,UAAU,CAAC,SAAS,CAAC;YAClC,IAAI,YAAC,MAA8B;gBACjC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC;oBACjB,IAAM,EAAE,GAAG,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;oBAC1B,IAAI,EAAE;wBAAE,OAAO,EAAE,CAAC,MAAM,CAAC,CAAC;oBAC1B,MAAM,CAAC,IAAI,KAAK,CAAC,oDAA6C,GAAG,CAAC,MAAM,WAAQ,CAAC,CAAC,CAAC;gBACrF,CAAC,CAAC,CAAC,IAAI,CACL,UAAA,GAAG;oBACD,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;oBAClB,YAAY,EAAE,CAAC;gBACjB,CAAC,EACD,MAAM,CACP,CAAC;YACJ,CAAC;YACD,KAAK,YAAC,KAAY;gBAChB,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC;oBACjB,IAAM,OAAO,GAAG,cAAc,CAAC,UAAU,EAAE,CAAC,CAAC;oBAC7C,IAAI,OAAO;wBAAE,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC;oBACnC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAChB,CAAC,CAAC,CAAC,IAAI,CACL,YAAY,EACZ,MAAM,CACP,CAAC;YACJ,CAAC;SACF,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,OAAO,SAAA;QACP,YAAY,cAAA;KACb,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,OAAO,WACZ,OAAgB;IAChB,aAAwB;SAAxB,UAAwB,EAAxB,qBAAwB,EAAxB,IAAwB;QAAxB,4BAAwB;;IAExB,OAAO,kCAAkC,8BAAC,OAAO,GAAK,GAAG,UAAE,OAAO,CAAC;AACrE,CAAC","sourcesContent":["import { ObservableQuery, ApolloQueryResult } from '../../core';\nimport { ObservableSubscription } from '../../utilities';\n\n/**\n *\n * @param observable the observable query to subscribe to\n * @param shouldResolve should we resolve after seeing all our callbacks [default: true]\n * (use this if you are racing the promise against another)\n * @param wait how long to wait after seeing desired callbacks before resolving\n * [default: -1 => don't wait]\n * @param errorCallbacks an expected set of errors\n */\nexport type Options = {\n observable: ObservableQuery<any>;\n shouldResolve?: boolean;\n wait?: number;\n errorCallbacks?: ((error: Error) => any)[];\n};\n\nexport type ResultCallback = ((result: ApolloQueryResult<any>) => any);\n\n// Take an observable and N callbacks, and observe the observable,\n// ensuring it is called exactly N times, resolving once it has done so.\n// Optionally takes a timeout, which it will wait X ms after the Nth callback\n// to ensure it is not called again.\nexport function observableToPromiseAndSubscription(\n { observable, shouldResolve = true, wait = -1, errorCallbacks = [] }: Options,\n ...cbs: ResultCallback[]\n): { promise: Promise<any[]>; subscription: ObservableSubscription } {\n let subscription: ObservableSubscription = null as never;\n const promise = new Promise<any[]>((resolve, reject) => {\n let errorIndex = 0;\n let cbIndex = 0;\n const results: any[] = [];\n\n const tryToResolve = () => {\n if (!shouldResolve) {\n return;\n }\n\n const done = () => {\n subscription.unsubscribe();\n // XXX: we could pass a few other things out here?\n resolve(results);\n };\n\n if (cbIndex === cbs.length && errorIndex === errorCallbacks.length) {\n if (wait === -1) {\n done();\n } else {\n setTimeout(done, wait);\n }\n }\n };\n\n let queue = Promise.resolve();\n\n subscription = observable.subscribe({\n next(result: ApolloQueryResult<any>) {\n queue = queue.then(() => {\n const cb = cbs[cbIndex++];\n if (cb) return cb(result);\n reject(new Error(`Observable 'next' method called more than ${cbs.length} times`));\n }).then(\n res => {\n results.push(res);\n tryToResolve();\n },\n reject,\n );\n },\n error(error: Error) {\n queue = queue.then(() => {\n const errorCb = errorCallbacks[errorIndex++];\n if (errorCb) return errorCb(error);\n reject(error);\n }).then(\n tryToResolve,\n reject,\n );\n },\n });\n });\n\n return {\n promise,\n subscription,\n };\n}\n\nexport default function(\n options: Options,\n ...cbs: ResultCallback[]\n): Promise<any[]> {\n return observableToPromiseAndSubscription(options, ...cbs).promise;\n}\n"]}
\No newline at end of file