Skip to content

Commit

Permalink
fix(nile-js): add typedoc to events class
Browse files Browse the repository at this point in the history
  • Loading branch information
jrea committed Aug 12, 2022
1 parent fa464e5 commit e4f66e8
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 112 deletions.
69 changes: 62 additions & 7 deletions lib/nile/src/EventsApi.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
import { EntitiesApi, InstanceEvent } from './generated/openapi/src';

export class EventsApi {
export type TimersType = { [key: number]: ReturnType<typeof setTimeout> };
export type EventListenerOptions = { type: string; seq: number };
export type ListenerCallback = (event: InstanceEvent) => Promise<void>;

export interface EventsApiInterface {
entities: EntitiesApi;
timers: TimersType;
on(
options: EventListenerOptions,
listener: ListenerCallback,
refresh?: number
): number;
cancel(timerId: number): void;
}

/**
* EventsApi - Interface
* @export
* @interface EventsApiInterface
*/
export default class EventsApi implements EventsApiInterface {
static onCounter = 0;
entities: EntitiesApi;
timers: { [key: number]: ReturnType<typeof setTimeout> };
timers: TimersType;

constructor(entities: EntitiesApi) {
this.entities = entities;
this.timers = {};
}

/**
* Listen for Nile events
* @example
* ```typescript
* import Nile from '@theniledev/js';
* const nile = new Nile({ apiUrl: 'http://localhost:8080', workspace: 'myWorkspace' });
*
* const listenerOptions = {
* type: 'myEntityType',
* seq: 0 // from the beginning of time
* };
*
* nile.events.on(listenerOptions, (instanceEvent) => {
* console.log(JSON.stringify(instanceEvent, null, 2));
* });
* ```
*/
on(
options: { type: string; seq: number },
listener: (event: InstanceEvent) => Promise<void>,
options: EventListenerOptions,
listener: ListenerCallback,
refresh = 5000
): number {
const id = EventsApi.onCounter++;
Expand Down Expand Up @@ -41,11 +78,29 @@ export class EventsApi {
return id;
}

cancel(id: number): void {
const timer = this.timers[id];
/**
* Remove and cancel a running timer
* @example
* ```typescript
* import Nile from '@theniledev/js';
* const nile = new Nile({ apiUrl: 'http://localhost:8080', workspace: 'myWorkspace' });
*
* const listenerOptions = {
* type: 'myEntityType',
* seq: 0 // from the beginning of time
* };
*
* const timerId = nile.events.on(listenerOptions, (instanceEvent) => {
* console.log(JSON.stringify(instanceEvent, null, 2));
* });
* nile.events.cancel(timerId);
* ```
*/
cancel(timerId: number): void {
const timer = this.timers[timerId];
if (timer) {
clearTimeout(timer);
delete this.timers[id];
delete this.timers[timerId];
}
}
}
52 changes: 1 addition & 51 deletions lib/nile/src/Nile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import {
DevelopersApi,
EntitiesApi,
InstanceEvent,
OrganizationsApi,
UsersApi,
WorkspacesApi,
Expand All @@ -13,56 +12,7 @@ import {
Configuration,
ConfigurationParameters,
} from './generated/openapi/src/runtime';

export class EventsApi {
static onCounter = 0;
entities: EntitiesApi;
timers: { [key: number]: ReturnType<typeof setTimeout> };

constructor(entities: EntitiesApi) {
this.entities = entities;
this.timers = {};
}

on(
options: { type: string; seq: number },
listener: (event: InstanceEvent) => Promise<void>,
refresh = 5000
): number {
const id = EventsApi.onCounter++;
let seq = options.seq;
const getEvents = async () => {
const events = await this.entities.instanceEvents({
type: options.type,
seq,
});
if (events) {
for (let i = 0; i < events.length; i++) {
const event = events[i];
if (event) {
await listener(event);
if (seq == null || (event?.after?.seq || seq) > seq) {
seq = event?.after?.seq || seq;
}
}
}
}
const timer = setTimeout(getEvents, refresh);
this.timers[id] = timer;
};
const timer = setTimeout(getEvents, 0);
this.timers[id] = timer;
return id;
}

cancel(id: number): void {
const timer = this.timers[id];
if (timer) {
clearTimeout(timer);
delete this.timers[id];
}
}
}
import EventsApi from './EventsApi';

export class NileApi {
users: UsersApi;
Expand Down
2 changes: 1 addition & 1 deletion lib/nile/templates/apis.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class {{classname}} extends runtime.BaseAPI {
*```typescript
* import {{moduleName}} from '{{{projectName}}}';
*
* const nile = new {{{moduleName}}}({ apiUrl: 'http://localhost:8080', workspace: "myWorkspace" });
* const nile = new {{{moduleName}}}({ apiUrl: 'http://localhost:8080', workspace: 'myWorkspace' });
*
{{#hasParams}}
* const body = {
Expand Down
32 changes: 13 additions & 19 deletions packages/events-example/src/commands/reconcile/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Command } from '@oclif/core';
import Nile, { Instance, NileApi } from '@theniledev/js';

import { ReconciliationPlan } from '../../model/ReconciliationPlan';

import { pulumiS3, PulumiAwsDeployment } from './lib/pulumi';
import { ReconciliationPlan } from './ReconciliationPlan';
import { flagDefaults } from './flagDefaults';

// configuration for interacting with nile
Expand All @@ -18,21 +19,16 @@ type DeveloperCreds = {
export default class Reconcile extends Command {
static enableJsonFlag = true;
static description = 'reconcile nile/pulumi deploys';

static flags = flagDefaults;

deployment!: PulumiAwsDeployment;
nile!: NileApi;

/**
* Runner for oclif
* @returns void
*/
async run(): Promise<unknown> {
const { flags } = await this.parse(Reconcile);

const {
status,
region,
organization,
entity,
basePath,
Expand All @@ -41,15 +37,14 @@ export default class Reconcile extends Command {
password,
} = flags;

// Nile setup
// nile setup
await this.connectNile({ basePath, workspace, email, password });
const instances = await this.loadNileInstances(organization, entity);

// Pulumi setup
// pulumi setup
this.deployment = await PulumiAwsDeployment.create(
'nile-examples',
pulumiS3,
{ region }
pulumiS3
);
const stacks = await this.deployment.loadPulumiStacks();

Expand Down Expand Up @@ -79,26 +74,25 @@ export default class Reconcile extends Command {

/**
* sets up Nile instance, and set auth token to the logged in developer
* @param config Configuration for instantiating Nile and logging ing
* @param config Configuration for instantiating Nile and logging in
*/
async connectNile({
basePath,
workspace,
email,
password,
}: NileConfig & DeveloperCreds) {
const developerPayload = {
loginInfo: {
email,
password,
},
};
this.nile = Nile({
basePath,
workspace,
});
const token = await this.nile.developers
.loginDeveloper(developerPayload)
.loginDeveloper({
loginInfo: {
email,
password,
},
})
.catch((error: unknown) => {
// eslint-disable-next-line no-console
console.error('Nile authentication failed', error);
Expand Down

0 comments on commit e4f66e8

Please sign in to comment.