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

Rework subscribe function to add more typesafety #1

Closed
kalvinpearce opened this issue Nov 16, 2019 · 8 comments
Closed

Rework subscribe function to add more typesafety #1

kalvinpearce opened this issue Nov 16, 2019 · 8 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@kalvinpearce
Copy link
Owner

Need to look into if theres a better way of subscribing to state pieces. At the moment there is little safety around what gets put in the stateValue param. Would be good to rework to make it more typesafe if possible?

This would remove the need for branching statement and broken subscribes.

seorsum/src/index.ts

Lines 44 to 60 in 446d28f

const subscribe = <T>(
stateValue: (state: State) => T,
callback: (state: T) => void,
) => {
// Little messy way of grabbing the state path from the params
const res = stateValue.toString().match(/\.(.*);/);
// This should always pass but is needed for intellisense
if (res && res.length > 1) {
const key = res[1];
const fn = () => callback(stateValue(state));
// Add to event bus
bus.addListener(key, fn);
// Return function to remove from event bus
return () => {
bus.removeListener(key, fn);
};
}

@kalvinpearce kalvinpearce self-assigned this Nov 16, 2019
@kalvinpearce kalvinpearce added enhancement New feature or request help wanted Extra attention is needed labels Nov 16, 2019
@kalvinpearce kalvinpearce reopened this Nov 16, 2019
@kalvinpearce
Copy link
Owner Author

Something like this could be on the right track?
https://github.com/RafaelSalguero/DeepPath/

The idea is that you can curry the path out from an object type using keyof typed options.
Perhaps subscribe could be more along the lines of subscibe('name')('first');?

@kalvinpearce
Copy link
Owner Author

So this is now an important fix. The subscribing is based on the way you write the state piece out and if it compiles differently then it can break.
Since it is based on this regex it is quite easy to break and it breaks silently.

const res = stateValue.toString().match(/\.([a-z\.]*)/);

@kalvinpearce
Copy link
Owner Author

This looks like a promising solution rather than currying.
microsoft/TypeScript#12290 (comment)
With some adapting it means you could run subscribe(['name', 'first'1]); and each element in the array would be a string literal for the possible keys of state for that path. Limitation is that it only supports up to 7 layers deep but could be a good solution for now.

@kalvinpearce
Copy link
Owner Author

This is somewhat solved in #2 but I still feel like this needs to be left open and continue to investigate the solution. As @JamesYFC has suggested, maybe the solution could be to add more layers to the tuple path type but it seems limiting to have a hard upper limit of how deep the state can be until intellesense breaks.

@kalvinpearce
Copy link
Owner Author

@JamesYFC there is an issue with the new tuple path method that we missed. It seems to only support a depth of 2 at the moment

subscribe(['one', 'two']); // Works fine
subscribe(['one', 'two', 'three']); //Types of property '['2']' are incompatible.
      // Type 'string' is not assignable to type 'undefined'.

@JamesYFC
Copy link
Contributor

JamesYFC commented Nov 19, 2019 via email

@kalvinpearce
Copy link
Owner Author

Closing now due to being fixed in b19bf2f

@kalvinpearce
Copy link
Owner Author

Current limit is 10 paths deep but after adding more I realised TS currently doesn't types of more than 10 deep... at 13 it will throw errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants