Skip to content

Commit

Permalink
feat(service): trigger onInit after initializing service.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayank1791989 committed May 15, 2017
1 parent b8e9c9c commit 0cc0157
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ class GQLService {

type Options = {
cwd?: string,
onChange?: () => void // called when something changes
onChange?: () => void, // called when something changes
onInit?: () => void, // called once after initialization
debug?: boolean, // enable debug logs
};

type CommandParams = {
Expand Down
29 changes: 20 additions & 9 deletions src/GQLService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { GQLError } from './shared/GQLError';
type Options = $Exact<{
cwd?: string,
onChange?: () => void,
onInit?: () => void,
debug?: true,
}>;

Expand All @@ -26,28 +27,38 @@ type CommandParams = $Exact<{
position: Position,
}>;

export type { Options as GQLServiceOptions };

export class GQLService {
_isInitialized: boolean = false;
_schemaBuilder: schema.SchemaBuilder;
_queryManager: query.QueryManager;
_queryManager: ?query.QueryManager;

_config: GQLConfig;

constructor(options: ?Options) {
const { onChange, debug: enableDebug, ...configOptions } = options || {};
const { onChange, onInit, debug: enableDebug, ...configOptions } = options || {};
if (enableDebug) { (debug: any).enable(); }
this._config = new GQLConfig(configOptions);
this._schemaBuilder = new schema.SchemaBuilder({
config: this._config,
watch: true,
onChange,
onInit: () => {
this._isInitialized = true;
this._queryManager = new query.QueryManager({
config: this._config,
getSchema: () => this._schemaBuilder.getSchema(),
onChange,
});
if (this._config.getQuery()) {
this._queryManager = new query.QueryManager({
config: this._config,
getSchema: () => this._schemaBuilder.getSchema(),
onInit: () => {
this._isInitialized = true;
if (onInit) { onInit(); }
},
onChange,
});
} else {
this._isInitialized = true;
if (onInit) { onInit(); }
}
},
});
}
Expand All @@ -60,7 +71,7 @@ export class GQLService {
status(): Array<GQLError> {
if (!this._isInitialized) { return []; }
const schemaErrors = this._schemaBuilder.getSchemaErrors();
const queryErrors = this._queryManager.getErrors();
const queryErrors = this._queryManager ? this._queryManager.getErrors() : [];
return schemaErrors.concat(queryErrors.filter(err => Boolean(err)));
}

Expand Down

0 comments on commit 0cc0157

Please sign in to comment.