Debian Codegraph @

memoizee/ext/promise.js

raw
1 /* eslint max-statements: 0 */
2
3 // Support for functions returning promise
4
5 "use strict";
6
7 var objectMap = require("es5-ext/object/map")
8 , primitiveSet = require("es5-ext/object/primitive-set")
9 , ensureString = require("es5-ext/object/validate-stringifiable-value")
10 , toShortString = require("es5-ext/to-short-string-representation")
11 , isPromise = require("is-promise")
12 , nextTick = require("next-tick");
13
14 var create = Object.create
15 , supportedModes = primitiveSet("then", "then:finally", "done", "done:finally");
16
17 require("../lib/registered-extensions").promise = function (mode, conf) {
18 var waiting = create(null), cache = create(null), promises = create(null);
19
20 if (mode === true) {
21 mode = null;
22 } else {
23 mode = ensureString(mode);
24 if (!supportedModes[mode]) {
25 throw new TypeError("'" + toShortString(mode) + "' is not valid promise mode");
26 }
27 }
28
29 // After not from cache call
30 conf.on("set", function (id, ignore, promise) {
31 var isFailed = false;
32
33 if (!isPromise(promise)) {
34 // Non promise result
35 cache[id] = promise;
36 conf.emit("setasync", id, 1);
37 return;
38 }
39 waiting[id] = 1;
40 promises[id] = promise;
41 var onSuccess = function (result) {
42 var count = waiting[id];
43 if (isFailed) {
44 throw new Error(
45 "Memoizee error: Detected unordered then|done & finally resolution, which " +
46 "in turn makes proper detection of success/failure impossible (when in " +
47 "'done:finally' mode)\n" +
48 "Consider to rely on 'then' or 'done' mode instead."
49 );
50 }
51 if (!count) return; // Deleted from cache before resolved
52 delete waiting[id];
53 cache[id] = result;
54 conf.emit("setasync", id, count);
55 };
56 var onFailure = function () {
57 isFailed = true;
58 if (!waiting[id]) return; // Deleted from cache (or succeed in case of finally)
59 delete waiting[id];
60 delete promises[id];
61 conf.delete(id);
62 };
63
64 var resolvedMode = mode;
65 if (!resolvedMode) resolvedMode = "then";
66
67 if (resolvedMode === "then") {
68 var nextTickFailure = function () { nextTick(onFailure); };
69 // Eventual finally needs to be attached to non rejected promise
70 // (so we not force propagation of unhandled rejection)
71 promise = promise.then(function (result) {
72 nextTick(onSuccess.bind(this, result));
73 }, nextTickFailure);
74 // If `finally` is a function we attach to it to remove cancelled promises.
75 if (typeof promise.finally === "function") {
76 promise.finally(nextTickFailure);
77 }
78 } else if (resolvedMode === "done") {
79 // Not recommended, as it may mute any eventual "Unhandled error" events
80 if (typeof promise.done !== "function") {
81 throw new Error(
82 "Memoizee error: Retrieved promise does not implement 'done' " +
83 "in 'done' mode"
84 );
85 }
86 promise.done(onSuccess, onFailure);
87 } else if (resolvedMode === "done:finally") {
88 // The only mode with no side effects assuming library does not throw unconditionally
89 // for rejected promises.
90 if (typeof promise.done !== "function") {
91 throw new Error(
92 "Memoizee error: Retrieved promise does not implement 'done' " +
93 "in 'done:finally' mode"
94 );
95 }
96 if (typeof promise.finally !== "function") {
97 throw new Error(
98 "Memoizee error: Retrieved promise does not implement 'finally' " +
99 "in 'done:finally' mode"
100 );
101 }
102 promise.done(onSuccess);
103 promise.finally(onFailure);
104 }
105 });
106
107 // From cache (sync)
108 conf.on("get", function (id, args, context) {
109 var promise;
110 if (waiting[id]) {
111 ++waiting[id]; // Still waiting
112 return;
113 }
114 promise = promises[id];
115 var emit = function () { conf.emit("getasync", id, args, context); };
116 if (isPromise(promise)) {
117 if (typeof promise.done === "function") promise.done(emit);
118 else {
119 promise.then(function () { nextTick(emit); });
120 }
121 } else {
122 emit();
123 }
124 });
125
126 // On delete
127 conf.on("delete", function (id) {
128 delete promises[id];
129 if (waiting[id]) {
130 delete waiting[id];
131 return; // Not yet resolved
132 }
133 if (!hasOwnProperty.call(cache, id)) return;
134 var result = cache[id];
135 delete cache[id];
136 conf.emit("deleteasync", id, [result]);
137 });
138
139 // On clear
140 conf.on("clear", function () {
141 var oldCache = cache;
142 cache = create(null);
143 waiting = create(null);
144 promises = create(null);
145 conf.emit("clearasync", objectMap(oldCache, function (data) { return [data]; }));
146 });
147 };