Created
January 20, 2018 04:56
-
-
Save leppaott/fe699e04cdd730946199060f73198913 to your computer and use it in GitHub Desktop.
Merging state changes functionally
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
/** | |
* @param {string} propName New property name for target | |
* @param {Object} target Target object | |
* @param {Object} changes Modified properties of target | |
* @param {Object} restOfProps | |
* @return {Promise} resolve({propName: {target}, ...restOfProps}) | |
*/ | |
function _mergePropChanges(propName, target, changes, ...restOfProps) { | |
return Promise.resolve(Object.assign({ | |
[propName]: Object.assign(target, changes) | |
}, ...restOfProps)) | |
} | |
const resolveDataChanges = (propName) => | |
(target = {}) => (changes = {}, ...restOfProps) => | |
_mergePropChanges(propName, target, changes, ...restOfProps) | |
export { resolveDataChanges }; | |
/* | |
This was used to remove a lot of repetive code in our chatbot's (FaaS) actions that asynchronously | |
had to resolve and return with modified data whis was later used in our bot dialog. | |
Data structure: { | |
context: { | |
name: 'Matti Luukkainen', | |
}, | |
input: '2', | |
} | |
The code had to handle merging a new state (changes) into the named object (context) while retaining its name and | |
concatenate it with other object(s) or a value (restOfProps) in this example, into an object with multiple objects. | |
Think of Object.assign() and setState(). | |
So we had: | |
export function setName({ context, input }) { | |
return Promise.resolve({ | |
context: { | |
...context, | |
name: input, | |
}, | |
}); | |
} | |
turned into saner: | |
const contextChanges = resolveDataChanges('context'); | |
export function setName({ context, input }) { | |
return contextChanges(context)({ | |
name: input, | |
}); | |
} | |
here contextChanges determines the name of the named object inside the modified state object. Unfortunately each named object | |
requires its own 'resolver' even though the name is implied by the target object (context) in this example. | |
However the target object can be any object wherefrom the data is loaded. The target object is copied not modified. | |
A more complex example showing this specific data structure behind the alghorithm. | |
This wouldn't work with more complex data structures without changes. Adding Promiseless version is easily done though. | |
return Promise.resolve({ | |
context: { | |
...context, | |
communicationMethods: { | |
...context.communicationMethods, | |
[method.identifier]: undefinedCommunicationInfo, | |
}, | |
}, | |
result: method.infoRequestText, | |
}); | |
...turned into: | |
return contextChanges(context)( | |
communicationChanges(context.communicationMethods)({ | |
[method.identifier]: undefinedCommunicationInfo, | |
}), { | |
result: method.infoRequestText, | |
}); | |
Clearing was done with: | |
return contextChanges()(); | |
...which was equal to: | |
Promise.resolve({ | |
context: {}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment