Debian Codegraph @

memoizee/ext/async.js

raw
1 /* eslint consistent-this: 0, no-shadow:0, no-eq-null: 0, eqeqeq: 0, no-unused-vars: 0 */
2
3 // Support for asynchronous functions
4
5 "use strict";
6
7 var aFrom = require("es5-ext/array/from")
8 , objectMap = require("es5-ext/object/map")
9 , mixin = require("es5-ext/object/mixin")
10 , defineLength = require("es5-ext/function/_define-length")
11 , nextTick = require("next-tick");
12
13 var slice = Array.prototype.slice, apply = Function.prototype.apply, create = Object.create;
14
15 require("../lib/registered-extensions").async = function (tbi, conf) {
16 var waiting = create(null)
17 , cache = create(null)
18 , base = conf.memoized
19 , original = conf.original
20 , currentCallback
21 , currentContext
22 , currentArgs;
23
24 // Initial
25 conf.memoized = defineLength(function (arg) {
26 var args = arguments, last = args[args.length - 1];
27 if (typeof last === "function") {
28 currentCallback = last;
29 args = slice.call(args, 0, -1);
30 }
31 return base.apply((currentContext = this), (currentArgs = args));
32 }, base);
33 try { mixin(conf.memoized, base); }
34 catch (ignore) {}
35
36 // From cache (sync)
37 conf.on("get", function (id) {
38 var cb, context, args;
39 if (!currentCallback) return;
40
41 // Unresolved
42 if (waiting[id]) {
43 if (typeof waiting[id] === "function") waiting[id] = [waiting[id], currentCallback];
44 else waiting[id].push(currentCallback);
45 currentCallback = null;
46 return;
47 }
48
49 // Resolved, assure next tick invocation
50 cb = currentCallback;
51 context = currentContext;
52 args = currentArgs;
53 currentCallback = currentContext = currentArgs = null;
54 nextTick(function () {
55 var data;
56 if (hasOwnProperty.call(cache, id)) {
57 data = cache[id];
58 conf.emit("getasync", id, args, context);
59 apply.call(cb, data.context, data.args);
60 } else {
61 // Purged in a meantime, we shouldn't rely on cached value, recall
62 currentCallback = cb;
63 currentContext = context;
64 currentArgs = args;
65 base.apply(context, args);
66 }
67 });
68 });
69
70 // Not from cache
71 conf.original = function () {
72 var args, cb, origCb, result;
73 if (!currentCallback) return apply.call(original, this, arguments);
74 args = aFrom(arguments);
75 cb = function self(err) {
76 var cb, args, id = self.id;
77 if (id == null) {
78 // Shouldn't happen, means async callback was called sync way
79 nextTick(apply.bind(self, this, arguments));
80 return undefined;
81 }
82 delete self.id;
83 cb = waiting[id];
84 delete waiting[id];
85 if (!cb) {
86 // Already processed,
87 // outcome of race condition: asyncFn(1, cb), asyncFn.clear(), asyncFn(1, cb)
88 return undefined;
89 }
90 args = aFrom(arguments);
91 if (conf.has(id)) {
92 if (err) {
93 conf.delete(id);
94 } else {
95 cache[id] = { context: this, args: args };
96 conf.emit("setasync", id, typeof cb === "function" ? 1 : cb.length);
97 }
98 }
99 if (typeof cb === "function") {
100 result = apply.call(cb, this, args);
101 } else {
102 cb.forEach(function (cb) { result = apply.call(cb, this, args); }, this);
103 }
104 return result;
105 };
106 origCb = currentCallback;
107 currentCallback = currentContext = currentArgs = null;
108 args.push(cb);
109 result = apply.call(original, this, args);
110 cb.cb = origCb;
111 currentCallback = cb;
112 return result;
113 };
114
115 // After not from cache call
116 conf.on("set", function (id) {
117 if (!currentCallback) {
118 conf.delete(id);
119 return;
120 }
121 if (waiting[id]) {
122 // Race condition: asyncFn(1, cb), asyncFn.clear(), asyncFn(1, cb)
123 if (typeof waiting[id] === "function") waiting[id] = [waiting[id], currentCallback.cb];
124 else waiting[id].push(currentCallback.cb);
125 } else {
126 waiting[id] = currentCallback.cb;
127 }
128 delete currentCallback.cb;
129 currentCallback.id = id;
130 currentCallback = null;
131 });
132
133 // On delete
134 conf.on("delete", function (id) {
135 var result;
136 // If false, we don't have value yet, so we assume that intention is not
137 // to memoize this call. After value is obtained we don't cache it but
138 // gracefully pass to callback
139 if (hasOwnProperty.call(waiting, id)) return;
140 if (!cache[id]) return;
141 result = cache[id];
142 delete cache[id];
143 conf.emit("deleteasync", id, slice.call(result.args, 1));
144 });
145
146 // On clear
147 conf.on("clear", function () {
148 var oldCache = cache;
149 cache = create(null);
150 conf.emit(
151 "clearasync", objectMap(oldCache, function (data) { return slice.call(data.args, 1); })
152 );
153 });
154 };