allow whitelist to be a function #148

Merged
mattkrick merged 1 commit from master into master 2016-08-04 19:27:19 +00:00
mattkrick commented 2016-07-08 00:11:53 +00:00 (Migrated from github.com)

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.

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 2016-07-08 00:13:58 +00:00 (Migrated from github.com)

Also, this maintains backwards compatibility, so no breaking changes

Also, this maintains backwards compatibility, so no breaking changes
mattkrick commented 2016-07-16 22:01:57 +00:00 (Migrated from github.com)

@michaelcontento any thoughts? feedback?

@michaelcontento any thoughts? feedback?
michaelcontento commented 2016-07-25 21:02:03 +00:00 (Migrated from github.com)

Sorry for the late response! 😞

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

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 2016-07-26 15:11:47 +00:00 (Migrated from github.com)

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

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 2016-08-04 18:43:29 +00:00 (Migrated from github.com)

🐻 👈

🐻 👈
michaelcontento commented 2016-08-04 19:58:33 +00:00 (Migrated from github.com)

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

Merged but the behaviour in `master` is slightly changed as I now pass the **whole action object** to the whitelist function (see cbe98031ffea10b6ab882f840cd5e900c46473c4).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MichaelContento/redux-storage!148
No description provided.