1 | "use strict";
|
2 |
|
3 | const Call = Function.prototype.call.bind(Function.prototype.call);
|
4 | const Apply = Function.prototype.call.bind(Function.prototype.apply);
|
5 |
|
6 | const ArraySlice = Array.prototype.slice;
|
7 | const hasOwnProperty = Object.prototype.hasOwnProperty;
|
8 |
|
9 | const I = require("immutable");
|
10 | const { base } = require("generic-jsx");
|
11 | const getChecksum = require("@njudah/get-checksum");
|
12 |
|
13 | const AsynchronousRequest = I.Record({ function: null, response: null }, "AsynchronousRequest");
|
14 | const AsynchronousFunction = I.Record({ UUID: null, function: null }, "AsynchronousFunction");
|
15 | const AsynchronousResponse = I.Record({ isError: false, value: null }, "AsynchronousResponse");
|
16 |
|
17 | module.exports = AsynchronousRequest;
|
18 |
|
19 | function fromPushFunction(aFunction, aUUID) {
|
20 | return function () {
|
21 | const count = arguments.length;
|
22 |
|
23 | var index = 0;
|
24 | const passed = [];
|
25 |
|
26 | for (; index < count; ++index) passed[index] = arguments[index];
|
27 |
|
28 | return new AsynchronousRequest({
|
29 | function: new AsynchronousFunction({
|
30 | UUID: getFunctionCallUUID(aFunction, aUUID, passed),
|
31 | function: (push, reject) => aFunction.apply(this, passed.concat(push, reject))
|
32 | })
|
33 | });
|
34 | };
|
35 | }
|
36 |
|
37 | module.exports.fromPushFunction = fromPushFunction;
|
38 |
|
39 | const NativeRegExp = /^function [$A-Z_a-z][0-9A-Z_a-z$]*\(\) { \[native code\] }$/;
|
40 | const UUIDSymbol = Symbol("UUID");
|
41 |
|
42 | function getFunctionCallUUID(aFunction, aFunctionUUID, args) {
|
43 | return getFunctionUUID(aFunction, aFunctionUUID) + getArgumentsUUID(args);
|
44 | }
|
45 |
|
46 | module.exports.getFunctionCallUUID = getFunctionCallUUID;
|
47 |
|
48 | function getArgumentsUUID(args) {
|
49 | if (args.length === 0) return "";
|
50 |
|
51 | if (args.length === 1) {
|
52 | const first = args[0];
|
53 | const type = typeof first;
|
54 |
|
55 | if (type === "function") return type + " " + getFunctionCallUUID(first, null, []);
|
56 |
|
57 | if (type !== "object") return type + " " + first;
|
58 |
|
59 | if (first === null) return "null";
|
60 |
|
61 | return first[UUIDSymbol] || (first[UUIDSymbol] = "object " + JSON.stringify(first));
|
62 | }
|
63 |
|
64 | console.log("called...", args);
|
65 | return JSON.stringify(args);
|
66 | }
|
67 |
|
68 | function getFunctionUUID(aFunction, aUUID) {
|
69 | return aUUID || getBaseFunctionUUID(aFunction);
|
70 | }
|
71 |
|
72 | function getBaseFunctionUUID(aFunction) {
|
73 | const baseFunction = base(aFunction);
|
74 | const baseUUID = aFunction[UUIDSymbol];
|
75 |
|
76 | if (typeof baseUUID === "string") return baseUUID;
|
77 |
|
78 | const source = baseFunction + "";
|
79 |
|
80 | if (NativeRegExp.test(source)) throw new Error("Can't auto-generate UUID.");
|
81 |
|
82 | return baseFunction[UUIDSymbol] = baseFunction.name + " " + getChecksum(source);
|
83 | }
|
84 |
|
85 | function fromAsyncFunction(aFunction, aUUID, another) {
|
86 | return fromPushFunction(function () {
|
87 | const count = arguments.length;
|
88 | const push = arguments[count - 2];
|
89 | const reject = arguments[count - 1];
|
90 |
|
91 | var index = 0;
|
92 | const passed = [];
|
93 |
|
94 | for (; index < count - 2; ++index) passed[index] = arguments[index];
|
95 |
|
96 | aFunction.apply(this, passed).then(push).catch(reject);
|
97 | }, getFunctionUUID(aFunction, aUUID));
|
98 | }
|
99 |
|
100 | module.exports.fromAsyncFunction = fromAsyncFunction;
|
101 |
|
102 | module.exports.AsynchronousRequest = AsynchronousRequest;
|
103 | module.exports.AsynchronousResponse = AsynchronousResponse; |
\ | No newline at end of file |