-
Notifications
You must be signed in to change notification settings - Fork 12
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 options objects so we can have namespace/bundle support #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,37 @@ | ||
import {useEffect, useState} from 'react'; | ||
import clone from 'lodash.clone'; | ||
|
||
export interface UseReplicantOptions { | ||
namespace: string; | ||
} | ||
|
||
/** | ||
* Subscribe to a replicant, returns tuple of the replicant value and `setValue` function. | ||
* The component using this function gets re-rendered when the value is updated. | ||
* The `setValue` function can be used to update replicant value. | ||
* @param repName The name of the replicant to use | ||
* @param initialValue Initial value to pass to `useState` function | ||
* @param options Options object. Currently supports the optional `namespace` option | ||
*/ | ||
export const useReplicant = <T>( | ||
repName: string, | ||
initialValue: T, | ||
options?: UseReplicantOptions, | ||
): [T, (newValue: T) => void] => { | ||
// Local state to store replicant value | ||
const [value, updateValue] = useState<T>(initialValue); | ||
|
||
// Declare replicant | ||
const replicant = nodecg.Replicant(repName, { | ||
defaultValue: initialValue, | ||
}); | ||
let replicant: any; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I remember correctly, the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I'll check that out. I wasn't aware of any of the nodecg types. |
||
if (options && options.namespace) { | ||
replicant = nodecg.Replicant(repName, options.namespace, { | ||
defaultValue: initialValue, | ||
}); | ||
} else { | ||
replicant = nodecg.Replicant(repName, { | ||
defaultValue: initialValue, | ||
}); | ||
} | ||
|
||
// Change handler to listen replicant changes | ||
const changeHandler = (newValue: T): void => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
import {useState} from 'react'; | ||
|
||
export interface UseReplicantOnceOptions { | ||
bundle: string; | ||
} | ||
|
||
export const useReplicantOnce = <T>( | ||
replicantName: string, | ||
initialValue: T, | ||
options?: UseReplicantOnceOptions, | ||
): T => { | ||
const [state, setState] = useState(initialValue); | ||
nodecg.readReplicant<T>(replicantName, (value) => { | ||
setState(value); | ||
}); | ||
if (options && options.bundle) { | ||
nodecg.readReplicant<T>(replicantName, options.bundle, (value) => { | ||
setState(value); | ||
}); | ||
} else { | ||
nodecg.readReplicant<T>(replicantName, (value) => { | ||
setState(value); | ||
}); | ||
} | ||
return state; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd become simpler with
initialValue
combined withoptions
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some way to do runtime checking of a type? Or do I basically have to check if options has any of the options arguments (bundle, namespace, etc) and if not, then treat it like an initial value? Like:
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately given how flexible Replicants are, I don't think merging them will work. The only real choice I can think of here is to remove initialValue and use options instead. Options provides a defaultValue parameter as a substitute.
It'd be a breaking change but pre-release that's not too big a deal. You could also handle it by making a separate function and making the existing one proxy to that one.