Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safeMap utility (or something like it) #2109

Open
coolsoftwaretyler opened this issue Oct 24, 2023 · 0 comments
Open

Add safeMap utility (or something like it) #2109

coolsoftwaretyler opened this issue Oct 24, 2023 · 0 comments
Labels
brainstorming/wild idea Brainstorming / Wild idea docs or examples Documentation or examples related enhancement Possible enhancement hacktoberfest level: easy

Comments

@coolsoftwaretyler
Copy link
Collaborator

If you have something like:

const DocumentModel = types.model({
  id: types.identifier,
  text: types.string
})

const DrawerModel = types.model({
  id: types.identifier,
  documents: types.array(types.reference(types.late(() => DocumentModel))),
})

And you load all your Drawers before loading Documents, and you can't guarantee that drawer.documents will end up referring to valid IDs, you'll end up with an error when if you call drawer.documents.map, because we're invoking the reference.

It would be awesome to be able to safely iterate the array in some way with a helper function. Something like this:

const safeMap = (array, success, error) => {
    if (array.length === 0) {
        return;
    }

    const isValid = isValidReference(() => array[0]);
    if (isValid) {
        success(array[0]);
    } else {
        error(array[0]);
    }

    safeMap(array.slice(1), success, error);
}

(This implementation may crash right now, I'm not exactly sure how to get it to work).

Would be nice to offer it as a utility on types.array, and similarly on types.map for things.

This came from a real-world problem using MST to load data from an API where document IDs couldn't be guaranteed to be valid by the time they get loaded.

I don't love expanding the API surface of our already-large API, but this could be helpful. At the very least, maybe we offer a code snippet in the docs to help folks in this scenario.

Happy to see some reproductions, demos, PRs, etc.

@coolsoftwaretyler coolsoftwaretyler added enhancement Possible enhancement brainstorming/wild idea Brainstorming / Wild idea level: easy docs or examples Documentation or examples related labels Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
brainstorming/wild idea Brainstorming / Wild idea docs or examples Documentation or examples related enhancement Possible enhancement hacktoberfest level: easy
Projects
None yet
Development

No branches or pull requests

1 participant