-
Notifications
You must be signed in to change notification settings - Fork 225
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
wip: add support for lazily replacing variables #1388
base: develop
Are you sure you want to change the base?
wip: add support for lazily replacing variables #1388
Conversation
1770756
to
2219c42
Compare
lib/collection/variable.js
Outdated
@@ -208,6 +222,7 @@ _.assign(Variable.prototype, /** @lends Variable.prototype */ { | |||
_.has(options, 'system') && (this.system = options.system); | |||
_.has(options, 'disabled') && (this.disabled = options.disabled); | |||
_.has(options, 'description') && (this.describe(options.description)); | |||
_.has(options, 'lazy') && (this.lazy = options.lazy); |
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.
@appurva21 Thoughts on this approach?
The advantage is that it makes it clear which variables are to be resolved lazily, and which are to be replaced immediately. The disadvantage is that lazy
has no meaning for replaceSubstitutions
( non lazy ), and makes the replaceSubstitutionsLazy
interface more complicated.
Some other approaches I thought about
- A) lazily replace all values ( when invoked through
replaceSubstitutionsLazy
) but this increases the amount of memory needed, and slightly slows down the function. - B) Only lazily replace
Promise
values. This is a good solution but there is no reliable method of identifying if something is a promise or not. ( Duck typing is not accurate ) - C) Only lazily replace function values. Could be a good tradeoff but has the same problems as (A), also makes the code slightly less intuitive.
7d554c9
to
9507de0
Compare
a2042c1
to
8873d7f
Compare
8873d7f
to
46fac10
Compare
let index = 0, | ||
match; | ||
|
||
while ((match = regex.exec(this.value.slice(index)))) { | ||
try { | ||
// eslint-disable-next-line no-await-in-loop | ||
let value = await replacerFn(...match); | ||
|
||
index += match.index; | ||
this.value = this.value.slice(0, index) + value + this.value.slice(index + match[0].length); | ||
index += match[0].length; | ||
} | ||
catch (_err) { /* empty */ } | ||
} |
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.
custom string replace logic instead of using String.replace ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace ) as it does not support async replacer fn
No description provided.