UNPKG

3.28 kBJavaScriptView Raw
1"use strict";
2
3const Call = Function.prototype.call.bind(Function.prototype.call);
4const Apply = Function.prototype.call.bind(Function.prototype.apply);
5
6const ArraySlice = Array.prototype.slice;
7const hasOwnProperty = Object.prototype.hasOwnProperty;
8
9const I = require("immutable");
10const { base } = require("generic-jsx");
11const getChecksum = require("@njudah/get-checksum");
12
13const AsynchronousRequest = I.Record({ function: null, response: null }, "AsynchronousRequest");
14const AsynchronousFunction = I.Record({ UUID: null, function: null }, "AsynchronousFunction");
15const AsynchronousResponse = I.Record({ isError: false, value: null }, "AsynchronousResponse");
16
17module.exports = AsynchronousRequest;
18
19function 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
37module.exports.fromPushFunction = fromPushFunction;
38
39const NativeRegExp = /^function [$A-Z_a-z][0-9A-Z_a-z$]*\(\) { \[native code\] }$/;
40const UUIDSymbol = Symbol("UUID");
41
42function getFunctionCallUUID(aFunction, aFunctionUUID, args) {
43 return getFunctionUUID(aFunction, aFunctionUUID) + getArgumentsUUID(args);
44}
45
46module.exports.getFunctionCallUUID = getFunctionCallUUID;
47
48function 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
68function getFunctionUUID(aFunction, aUUID) {
69 return aUUID || getBaseFunctionUUID(aFunction);
70}
71
72function 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
85function 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
100module.exports.fromAsyncFunction = fromAsyncFunction;
101
102module.exports.AsynchronousRequest = AsynchronousRequest;
103module.exports.AsynchronousResponse = AsynchronousResponse;
\No newline at end of file