@byte-this/funscript
Advanced tools
Comparing version
{ | ||
"name": "@byte-this/funscript", | ||
"version": "1.0.30", | ||
"version": "1.0.31", | ||
"description": "A typescript API which facilitates functional programming.", | ||
@@ -5,0 +5,0 @@ "main": "public-api.js", |
@@ -14,3 +14,2 @@ import { Clone } from "../clone/clone"; | ||
this.head = null; | ||
this.size = 0; | ||
this.head = new ListNode(pending); | ||
@@ -32,6 +31,2 @@ } | ||
this.head = newNode; | ||
this.size++; | ||
if (this.size > 1000) { | ||
console.log("DEGUB: too many items"); | ||
} | ||
} | ||
@@ -45,14 +40,20 @@ deleteFromList(args) { | ||
prevNode.next = node.next; | ||
this.size--; | ||
return false; | ||
return { | ||
itemRemoved: true, | ||
listIsEmpty: false | ||
}; | ||
} | ||
else if (!node.next) { | ||
this.size = 0; | ||
this.head = null; | ||
return true; | ||
return { | ||
itemRemoved: true, | ||
listIsEmpty: true | ||
}; | ||
} | ||
else { | ||
this.head = node.next; | ||
this.size--; | ||
return false; | ||
return { | ||
itemRemoved: true, | ||
listIsEmpty: false | ||
}; | ||
} | ||
@@ -63,38 +64,54 @@ } | ||
} | ||
return false; | ||
return { | ||
itemRemoved: false, | ||
listIsEmpty: false | ||
}; | ||
} | ||
} | ||
const pending = new Map(); | ||
const findPendingItem = (func, ...params) => { | ||
const argList = pending.get(func); | ||
if (!argList) { | ||
return void 0; | ||
class PendingCallsContainer { | ||
constructor() { | ||
this.funcMap = new Map(); | ||
this.size = 0; | ||
} | ||
const pendingArgs = argList.findInList(params); | ||
if (!pendingArgs) { | ||
return void 0; | ||
findPendingItem(func, ...params) { | ||
const argList = this.funcMap.get(func); | ||
if (!argList) { | ||
return void 0; | ||
} | ||
const pendingArgs = argList.findInList(params); | ||
if (!pendingArgs) { | ||
return void 0; | ||
} | ||
return { | ||
func, | ||
...pendingArgs | ||
}; | ||
} | ||
return { | ||
func, | ||
...pendingArgs | ||
}; | ||
}; | ||
const addPendingItem = (func, args, promise) => { | ||
if (!pending.has(func)) { | ||
pending.set(func, new ArgsLinkedList({ args, promise })); | ||
addPendingItem(func, args, promise) { | ||
if (!this.funcMap.has(func)) { | ||
this.funcMap.set(func, new ArgsLinkedList({ args, promise })); | ||
} | ||
else { | ||
this.funcMap.get(func).addToList({ args, promise }); | ||
} | ||
this.size++; | ||
if (this.size > 1000) { | ||
console.log("DEBUG issue"); | ||
} | ||
} | ||
else { | ||
pending.get(func).addToList({ args, promise }); | ||
removePendingItem(func, args) { | ||
const funcPending = this.funcMap.get(func); | ||
if (!funcPending) { | ||
return; | ||
} | ||
const { itemRemoved, listIsEmpty } = funcPending.deleteFromList(args); | ||
if (itemRemoved) { | ||
this.size--; | ||
} | ||
if (listIsEmpty) { | ||
this.funcMap.delete(func); | ||
} | ||
} | ||
}; | ||
const removePendingItem = (func, args) => { | ||
const funcPending = pending.get(func); | ||
if (!funcPending) { | ||
return; | ||
} | ||
const listIsEmpty = funcPending.deleteFromList(args); | ||
if (listIsEmpty) { | ||
pending.delete(func); | ||
} | ||
}; | ||
} | ||
const pendingCollection = new PendingCallsContainer(); | ||
export const CollectPendingInvocations = (func) => { | ||
@@ -106,3 +123,3 @@ const decorated = function (...params) { | ||
const clonedParams = Clone(params); | ||
const pendingItem = findPendingItem(func, params); | ||
const pendingItem = pendingCollection.findPendingItem(func, params); | ||
if (pendingItem) { | ||
@@ -113,7 +130,7 @@ return pendingItem.promise; | ||
const promise = func.apply(me, clonedParams); | ||
addPendingItem(func, clonedParams, promise); | ||
pendingCollection.addPendingItem(func, clonedParams, promise); | ||
promise | ||
.catch(() => { }) | ||
.finally(() => { | ||
removePendingItem(func, clonedParams); | ||
pendingCollection.removePendingItem(func, clonedParams); | ||
}); | ||
@@ -120,0 +137,0 @@ return promise; |
279349
0.24%3179
0.54%