Skip to content

Commit

Permalink
Log warning in console to hint about init not being called yet
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneLem committed Sep 9, 2022
1 parent b73a3bd commit 853d620
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/emoji-mart/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,28 @@ async function fetchJSON(src) {

let promise = null
let initCallback = null
let initialized = false

export function init(options) {
export function init(options, { caller } = {}) {
promise ||
(promise = new Promise((resolve) => {
initCallback = resolve
}))

if (options) {
_init(options)
} else if (caller && !initialized) {
console.warn(
`\`${caller}\` requires data to be initialized first. Promise will be pending until \`init\` is called.`,
)
}

return promise
}

async function _init(props) {
initialized = true

let { emojiVersion, set, locale } = props
emojiVersion || (emojiVersion = PickerProps.emojiVersion.value)
set || (set = PickerProps.set.value)
Expand Down
4 changes: 2 additions & 2 deletions packages/emoji-mart/src/helpers/search-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ function reset() {
Pool = null
}

async function search(value, { maxResults } = {}) {
async function search(value, { maxResults, caller } = {}) {
if (!value || !value.trim().length) return null
maxResults || (maxResults = 90)

await init()
await init(null, { caller: caller || 'SearchIndex.search' })

const values = value
.toLowerCase()
Expand Down

0 comments on commit 853d620

Please sign in to comment.