-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3351bc1
commit f8348fc
Showing
1 changed file
with
89 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,89 @@ | ||
"use strict"; | ||
|
||
function requireCondition(condition, options = {}) { | ||
options = { | ||
delay: 50, | ||
maxCycles: 1000, | ||
...options, | ||
}; | ||
|
||
// Preserve stack for throwing later when needed. | ||
const error = new Error("Maximum cycles reached."); | ||
|
||
return new Promise((resolve, reject) => { | ||
if (checkCondition()) return; | ||
|
||
let counter = 0; | ||
const checker = setInterval(() => { | ||
if (checkCounter(counter++) || checkCondition()) return clearInterval(checker); | ||
}, options.delay); | ||
|
||
function checkCondition() { | ||
const response = condition(); | ||
if (!response) return false; | ||
|
||
if (typeof response === "boolean") { | ||
if (response) resolve(); | ||
else reject(); | ||
} else if (typeof response === "object") { | ||
if (response.hasOwnProperty("success")) { | ||
if (response.success === true) resolve(response.value); | ||
else reject(response.value); | ||
} else { | ||
resolve(response); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
function checkCounter(count) { | ||
if (options.maxCycles <= 0) return false; | ||
|
||
if (count > options.maxCycles) { | ||
reject(error); | ||
console.trace(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
function requireElement(selector, attributes) { | ||
attributes = { | ||
invert: false, | ||
parent: document, | ||
...attributes, | ||
}; | ||
if (attributes.invert) { | ||
return requireCondition(() => !attributes.parent.find(selector), attributes); | ||
} else { | ||
return requireCondition(() => attributes.parent.find(selector), attributes); | ||
} | ||
} | ||
|
||
function requireSidebar() { | ||
return requireElement("#sidebar"); | ||
} | ||
|
||
function requireContent() { | ||
return requireElement(".content-wrapper"); | ||
} | ||
|
||
function requireItemsLoaded() { | ||
return requireElement(".items-cont[aria-expanded=true] > li > .title-wrap"); | ||
} | ||
|
||
function requireChatsLoaded() { | ||
return requireElement("#chatRoot [class*='chat-list-button__']"); | ||
} | ||
|
||
function requireFeatureManager() { | ||
return new Promise((resolve) => { | ||
const featureManagerIntervalID = setInterval(() => { | ||
while (typeof featureManager === "undefined") {} | ||
|
||
clearInterval(featureManagerIntervalID); | ||
resolve(); | ||
}, 100); | ||
}); | ||
} | ||
"use strict"; | ||
|
||
function requireCondition(condition, options = {}) { | ||
options = { | ||
delay: 50, | ||
maxCycles: 1000, | ||
...options, | ||
}; | ||
|
||
// Preserve stack for throwing later when needed. | ||
const error = new Error("Maximum cycles reached."); | ||
|
||
return new Promise((resolve, reject) => { | ||
if (checkCondition()) return; | ||
|
||
let counter = 0; | ||
const checker = setInterval(() => { | ||
if (checkCounter(counter++) || checkCondition()) return clearInterval(checker); | ||
}, options.delay); | ||
|
||
function checkCondition() { | ||
const response = condition(); | ||
if (!response) return false; | ||
|
||
if (typeof response === "boolean") { | ||
if (response) resolve(); | ||
else reject(); | ||
} else if (typeof response === "object") { | ||
if (response.hasOwnProperty("success")) { | ||
if (response.success === true) resolve(response.value); | ||
else reject(response.value); | ||
} else { | ||
resolve(response); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
function checkCounter(count) { | ||
if (options.maxCycles <= 0) return false; | ||
|
||
if (count > options.maxCycles) { | ||
reject(error); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
function requireElement(selector, attributes) { | ||
attributes = { | ||
invert: false, | ||
parent: document, | ||
...attributes, | ||
}; | ||
if (attributes.invert) { | ||
return requireCondition(() => !attributes.parent.find(selector), attributes); | ||
} else { | ||
return requireCondition(() => attributes.parent.find(selector), attributes); | ||
} | ||
} | ||
|
||
function requireSidebar() { | ||
return requireElement("#sidebar"); | ||
} | ||
|
||
function requireContent() { | ||
return requireElement(".content-wrapper"); | ||
} | ||
|
||
function requireItemsLoaded() { | ||
return requireElement(".items-cont[aria-expanded=true] > li > .title-wrap"); | ||
} | ||
|
||
function requireChatsLoaded() { | ||
return requireElement("#chatRoot [class*='chat-list-button__']"); | ||
} | ||
|
||
function requireFeatureManager() { | ||
return new Promise((resolve) => { | ||
const featureManagerIntervalID = setInterval(() => { | ||
while (typeof featureManager === "undefined") {} | ||
|
||
clearInterval(featureManagerIntervalID); | ||
resolve(); | ||
}, 100); | ||
}); | ||
} |