Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #239 from web3well/cells-formula-rename
Browse files Browse the repository at this point in the history
Use prefixKeys to avoid shadowing
  • Loading branch information
voltrevo authored Jul 5, 2022
2 parents 9cc3db2 + 49a3661 commit 565e767
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion extension/source/Home/QuillContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function QuillContextCells(
...storageCells,
networkJson: new FormulaCell(
{ network: storageCells.network },
({ network }) => JSON.stringify(network, null, 2),
({ $network }) => JSON.stringify($network, null, 2),
),
blockNumber: QuillLongPollingCell(ethereum, 'blockNumber'),
rpcBackgroundLogging: TransformCell.Sub(rpcLogging, 'background'),
Expand Down
11 changes: 9 additions & 2 deletions extension/source/QuillStorageCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,19 @@ function QuillStorageCells(storage: CellCollection) {

return {
...rootCells,
providerState: new FormulaCell(providerStateCells, (values) => values),
providerState: new FormulaCell(
providerStateCells,
({ $chainId, $developerSettings, $selectedAddress }) => ({
chainId: $chainId,
developerSettings: $developerSettings,
selectedAddress: $selectedAddress,
}),
),
...providerStateCells,

theme: new FormulaCell(
{ preferences: rootCells.preferences },
({ preferences: { selectedAddress, identities } }): Theme => {
({ $preferences: { selectedAddress, identities } }): Theme => {
if (selectedAddress === undefined) {
return 'light';
}
Expand Down
5 changes: 2 additions & 3 deletions extension/source/background/CurrencyConversionCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ export default function CurrencyConversionCell(
chainCurrency,
time: TimeCell(config.pollInterval),
},
// eslint-disable-next-line @typescript-eslint/no-shadow
({ preferredCurrency, chainCurrency }) =>
fetchRate(config.api, chainCurrency, preferredCurrency),
({ $preferredCurrency, $chainCurrency }) =>
fetchRate(config.api, $chainCurrency, $preferredCurrency),
);
}

Expand Down
4 changes: 2 additions & 2 deletions extension/source/background/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export default class PreferencesController {
this.preferredCurrency = new FormulaCell(
{ preferences },
// eslint-disable-next-line @typescript-eslint/no-shadow
({ preferences }) => {
const { selectedAddress, identities } = preferences;
({ $preferences }) => {
const { selectedAddress, identities } = $preferences;

if (selectedAddress === undefined) {
return undefined;
Expand Down
19 changes: 9 additions & 10 deletions extension/source/cells/FormulaCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import MemoryCell from './MemoryCell';
import AsyncIteratee from './AsyncIteratee';
import Stoppable from './Stoppable';
import nextEvent from './nextEvent';
import prefixKeys, { PrefixKeys } from './prefixKeys';
import assert from '../helpers/assert';
import delay from '../helpers/delay';

Expand All @@ -39,7 +40,9 @@ export class FormulaCell<

constructor(
public inputCells: InputCells,
public formula: (inputValues: InputValues<InputCells>) => T,
public formula: (
inputValues: PrefixKeys<'$', InputValues<InputCells>>,
) => T,
public hasChanged: (
previous: Awaited<T> | undefined,
latest: Awaited<T>,
Expand Down Expand Up @@ -145,7 +148,9 @@ export class FormulaCell<
}

const latest = deepCopy(
await this.formula(inputValues.value as InputValues<InputCells>),
await this.formula(
prefixKeys('$', inputValues.value as InputValues<InputCells>),
),
);

iterationCell.write({ value: latest });
Expand Down Expand Up @@ -180,12 +185,7 @@ export class FormulaCell<
latest: Input[K],
) => boolean = jsonHasChanged,
): IReadableCell<Input[K]> {
return new FormulaCell(
{ input },
// eslint-disable-next-line @typescript-eslint/no-shadow
({ input }) => input[key],
hasChanged,
);
return new FormulaCell({ input }, ({ $input }) => $input[key], hasChanged);
}

/** Like Sub, but also maps undefined|null to defaultValue. */
Expand All @@ -203,8 +203,7 @@ export class FormulaCell<
): IReadableCell<Exclude<Input[K], undefined | null>> {
return new FormulaCell(
{ input },
// eslint-disable-next-line @typescript-eslint/no-shadow
({ input }) => input[key] ?? defaultValue,
({ $input }) => $input[key] ?? defaultValue,
hasChanged,
);
}
Expand Down
3 changes: 1 addition & 2 deletions extension/source/cells/TransformCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export default class TransformCell<Input, T> implements ICell<Awaited<T>> {

this.formulaCell = new FormulaCell(
{ input },
// eslint-disable-next-line @typescript-eslint/no-shadow
({ input }) => mapInput(input),
({ $input }) => mapInput($input),
hasChanged,
);

Expand Down
2 changes: 1 addition & 1 deletion extension/source/cells/approximate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function approximate(
return new FormulaCell<{ value: IReadableCell<number> }, number>(
{ value },
// eslint-disable-next-line @typescript-eslint/no-shadow
({ value }) => value,
({ $value }) => $value,
(previous, latest) => {
if (previous === undefined) {
return true;
Expand Down
20 changes: 20 additions & 0 deletions extension/source/cells/prefixKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type RemovePrefix<
Prefix extends string,
T extends string,
> = T extends `${Prefix}${infer AfterPrefix}` ? AfterPrefix : never;

export type PrefixKeys<
Prefix extends string,
Obj extends Record<string, unknown>,
> = {
[K in `${Prefix}${keyof Obj & string}`]: Obj[RemovePrefix<Prefix, K>];
};

export default function prefixKeys<
Prefix extends string,
Obj extends Record<string, unknown>,
>(prefix: Prefix, obj: Obj): PrefixKeys<Prefix, Obj> {
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => [`${prefix}${k}`, v]),
) as PrefixKeys<Prefix, Obj>;
}

0 comments on commit 565e767

Please sign in to comment.