michaelcontento / redux-storage

Persistence layer for redux with flexible backends
MIT License
675 stars 50 forks source link

allow whitelist to be a function #148

Closed mattkrick closed 8 years ago

mattkrick commented 8 years ago

A pretty standard practice is for 3rd party reducers to use a prefix for all of their actions (eg redux-form/DESTROY). In my use case, I'd like to disallow every action triggered by a certain 3rd party reducer (eg every action that begins with redux-form/) .

Today, I'd have to know & list every single action that a 3rd party reducer can create. That's a long list. Plus, I run the risk of the list going stale when my 3rd party package updates or changes a constant.

I can accomplish what I want by iterating over the entire blacklist & checking to see if my blacklisted substring is found in each entry. However, your logic may differ.

That's why if we allow the whitelist to be a function, everyone wins. For example, I'd write something like this:

const whitelistFn = type => {
    const blacklistPrefixes = ['redux-form/', '@@router/'];
    for (let i = 0; i < blacklistPrefixes.length; i++) {
        const prefix = blacklistPrefixes[i];
        if (type.indexOf(prefix) !== -1) {
            return false;
        }
    }
    return true;
}

And you can use whatever logic you like. The idea is borrowed from webpack, where a bunch of config fields can either be an array or a function.

mattkrick commented 8 years ago

Also, this maintains backwards compatibility, so no breaking changes

mattkrick commented 8 years ago

@michaelcontento any thoughts? feedback?

michaelcontento commented 8 years ago

Sorry for the late response! 😞

I'll have a look tomorrow and, after a very quick review, it looks awesome! 🚀 👍 Thank you very much 🎉

mattkrick commented 8 years ago

sounds great! here's another example of using it in the wild: https://github.com/ParabolInc/action/blob/master/src/client/makeStore.js#L10-L19

mattkrick commented 8 years ago

🐻 👈

michaelcontento commented 8 years ago

Merged but the behaviour in master is slightly changed as I now pass the whole action object to the whitelist function (see cbe98031ffea10b6ab882f840cd5e900c46473c4).