Skip to content

Commit

Permalink
Improves aragon context management and grapql global vars
Browse files Browse the repository at this point in the history
  • Loading branch information
emmdim committed Feb 8, 2024
1 parent 16a2749 commit 38c7de9
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions packages/js-client/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import { DEFAULT_GASLESS_VOTING_BACKEND_URL, DEFAULT_GASLESS_VOTING_REPO_ADDRESS, DEFAULT_GASLESS_VOTING_SUBHGRAPH_URL } from "./internal";
import {
GaslessVotingContextState as GaslessVotingContextState,
GaslessVotingOverriddenState as GaslessVotingOverriddenState,
} from "./types";
import { GaslessVotingContextParams } from "./types";
import { Context, ContextCore } from "@aragon/sdk-client-common";
DEFAULT_GASLESS_VOTING_BACKEND_URL,
DEFAULT_GASLESS_VOTING_REPO_ADDRESS,
DEFAULT_GASLESS_VOTING_SUBHGRAPH_URL,
} from './internal';
import {
GaslessVotingContextState,
GaslessVotingOverriddenState,
} from './types';
import { GaslessVotingContextParams } from './types';
import { Context, ContextCore } from '@aragon/sdk-client-common';

export class GaslessVotingContext extends ContextCore {
// This variable keeps track of the state of the context and is an extension of the Base Context State
protected state: GaslessVotingContextState = this.state;
// This variable keeps track of the properties that were manually overriden by the user
protected overriden: GaslessVotingOverriddenState = this.overriden;
constructor(
contextParams?: Partial<GaslessVotingContextParams>,
aragonContext?: Context,
aragonContext?: Context
) {
// call the parent constructor to be able to have access to this
// and set the default values
super();
// if the user alredy provides an aragon context that we can use
if (aragonContext) {
aragonContext.set({graphqlNodes: [{"url":DEFAULT_GASLESS_VOTING_SUBHGRAPH_URL}]})
const context = Object.assign({}, aragonContext);
// override the default values with the ones from the aragon context
Object.assign(this, aragonContext);
Object.assign(this, context);
}
// contextParams have priority over the aragonContext
if (contextParams) {
// overide the aragonContext and default values with the ones from the contextParams
this.set(contextParams);
this.set({
graphqlNodes: [{ url: DEFAULT_GASLESS_VOTING_SUBHGRAPH_URL }],
});
this.overriden.graphqlNodes = true;
this.overriden.graphql = true;
}
}

Expand All @@ -46,8 +56,9 @@ export class GaslessVotingContext extends ContextCore {
this.overriden.gaslessVotingBackendUrl = true;
}

if(contextParams.gaslessVotingRepoAddress) {
this.state.gaslessVotingRepoAddress = contextParams.gaslessVotingRepoAddress;
if (contextParams.gaslessVotingRepoAddress) {
this.state.gaslessVotingRepoAddress =
contextParams.gaslessVotingRepoAddress;
this.overriden.gaslessVotingRepoAddress = true;
}
}
Expand Down

0 comments on commit 38c7de9

Please sign in to comment.