Skip to content
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

Moving types and classes into separate files #1

Merged
merged 3 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ module.exports = {
ecmaVersion: 2017,
sourceType: 'module',
},
plugins: ['ember', 'prettier'],
plugins: ['ember', 'prettier', '@typescript-eslint'],
extends: ['eslint:recommended', 'plugin:ember/recommended', 'prettier'],
env: {
browser: true,
node: false,
},
rules: {
'prettier/prettier': 'error',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
},
overrides: [
// node files
Expand Down
5 changes: 0 additions & 5 deletions .template-lintrc.js

This file was deleted.

11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ language: node_js
node_js:
# we recommend testing addons with the same minimum supported node version as Ember CLI
# so that your addon works for all apps
- "6"
- '6'

sudo: false
dist: trusty
Expand Down Expand Up @@ -33,22 +33,21 @@ jobs:
include:
# runs linting and tests with current locked deps

- stage: "Tests"
name: "Tests"
- stage: 'Tests'
name: 'Tests'
install:
- yarn install --non-interactive
script:
- yarn lint:hbs
- yarn lint:js
- yarn test

- name: "Floating Dependencies"
- name: 'Floating Dependencies'
script:
- yarn test

# we recommend new addons test the current and previous LTS
# as well as latest stable release (bonus points to beta/canary)
- stage: "Additional Tests"
- stage: 'Additional Tests'
env: EMBER_TRY_SCENARIO=ember-lts-2.18
- env: EMBER_TRY_SCENARIO=ember-lts-3.4
- env: EMBER_TRY_SCENARIO=ember-release
Expand Down
161 changes: 11 additions & 150 deletions addon/index.ts
Original file line number Diff line number Diff line change
@@ -1,150 +1,11 @@
import { DEBUG } from '@glimmer/env';

type WaiterName = string;

export interface IWaiter {
name: WaiterName;
waitUntil(): boolean;
debugInfo(): unknown[] | undefined | void;
}

export interface ISimpleWaiterDebugInfo {
stack: string | undefined;
label: string | undefined;
}

export class SimpleWaiter<T> implements IWaiter {
public name: WaiterName;

items = new Map<unknown, ISimpleWaiterDebugInfo>();

constructor(name: WaiterName) {
this.name = name;
}

add(item: T, label: string) {
this.items.set(item, {
stack: new Error().stack,
label,
});
}

delete(item: T) {
this.items.delete(item);
}

waitUntil() {
return this.items.size === 0;
}

debugInfo(): ISimpleWaiterDebugInfo[] {
let output: ISimpleWaiterDebugInfo[] = [];

this.items.forEach(item => output.push(item));

return output;
}
}

class PromiseWaiter<T> implements IWaiter {
name = 'promise-waiter';
items = new Map<Promise<T>, ISimpleWaiterDebugInfo>();

add(promise: Promise<T>, label?: string) {
this.items.set(promise, {
stack: new Error().stack,
label,
});
}

delete(promise: Promise<T>) {
this.items.delete(promise);
}

waitUntil(): boolean {
return this.items.size === 0;
}

debugInfo(): ISimpleWaiterDebugInfo[] {
let output: ISimpleWaiterDebugInfo[] = [];

this.items.forEach(item => output.push(item));

return output;
}
}

const PROMISE_WAITER = new PromiseWaiter();
export function waitForPromise<T>(promise: Promise<T>, label?: string) {
let result = promise;

if (DEBUG) {
PROMISE_WAITER.add(promise, label);

result = promise.then(
value => {
PROMISE_WAITER.delete(promise);
return value;
},
error => {
PROMISE_WAITER.delete(promise);
throw error;
}
);
}

return result;
}

const WAITERS = new Set();

export function register(waiter: IWaiter) {
WAITERS.add(waiter);
}

export function unregister(waiter: IWaiter) {
WAITERS.delete(waiter);
}

export function getWaiters(): IWaiter[] {
let result: IWaiter[] = [];

WAITERS.forEach(item => result.push(item));

return result;
}

export function reset() {
WAITERS.clear();
}

interface IPendingWaiterState {
pending: number;
waiters: {
[waiterName: string]: true | unknown[];
};
}

export function getPendingWaiterState(): IPendingWaiterState {
let result: IPendingWaiterState = {
pending: 0,
waiters: {},
};

WAITERS.forEach(waiter => {
if (waiter.waitUntil()) {
result.pending++;

let debugInfo = waiter.debugInfo();
result.waiters[waiter.name] = debugInfo || true;
}
});

return result;
}

export function shouldWait(): boolean {
let state = getPendingWaiterState();

return state.pending > 0;
}
export {
register,
unregister,
getWaiters,
reset,
getPendingWaiterState,
shouldWait,
} from './waiters';

export { SimpleWaiter } from './simple-waiter';
export { PromiseWaiter, waitForPromise } from './promise-waiter';
52 changes: 52 additions & 0 deletions addon/promise-waiter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { DEBUG } from '@glimmer/env';
import { IWaiter, ISimpleWaiterDebugInfo } from './types';

export class PromiseWaiter<T> implements IWaiter {
name = 'promise-waiter';
items = new Map<Promise<T>, ISimpleWaiterDebugInfo>();

add(promise: Promise<T>, label?: string) {
this.items.set(promise, {
stack: new Error().stack,
label,
});
}

delete(promise: Promise<T>) {
this.items.delete(promise);
}

waitUntil(): boolean {
return this.items.size === 0;
}

debugInfo(): ISimpleWaiterDebugInfo[] {
let output: ISimpleWaiterDebugInfo[] = [];

this.items.forEach(item => output.push(item));

return output;
}
}

const PROMISE_WAITER = new PromiseWaiter();
export function waitForPromise<T>(promise: Promise<T>, label?: string) {
let result = promise;

if (DEBUG) {
PROMISE_WAITER.add(promise, label);

result = promise.then(
value => {
PROMISE_WAITER.delete(promise);
return value;
},
error => {
PROMISE_WAITER.delete(promise);
throw error;
}
);
}

return result;
}
34 changes: 34 additions & 0 deletions addon/simple-waiter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { IWaiter, WaiterName, ISimpleWaiterDebugInfo } from './types';

export class SimpleWaiter<T> implements IWaiter {
public name: WaiterName;

items = new Map<unknown, ISimpleWaiterDebugInfo>();

constructor(name: WaiterName) {
this.name = name;
}

add(item: T, label: string) {
this.items.set(item, {
stack: new Error().stack,
label,
});
}

delete(item: T) {
this.items.delete(item);
}

waitUntil() {
return this.items.size === 0;
}

debugInfo(): ISimpleWaiterDebugInfo[] {
let output: ISimpleWaiterDebugInfo[] = [];

this.items.forEach(item => output.push(item));

return output;
}
}
19 changes: 19 additions & 0 deletions addon/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type WaiterName = string;

export interface IWaiter {
name: WaiterName;
waitUntil(): boolean;
debugInfo(): unknown[] | undefined | void;
}

export interface ISimpleWaiterDebugInfo {
stack: string | undefined;
label: string | undefined;
}

export interface IPendingWaiterState {
pending: number;
waiters: {
[waiterName: string]: true | unknown[];
};
}
47 changes: 47 additions & 0 deletions addon/waiters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { IWaiter, IPendingWaiterState } from './types';

const WAITERS = new Set();

export function register(waiter: IWaiter) {
WAITERS.add(waiter);
}

export function unregister(waiter: IWaiter) {
WAITERS.delete(waiter);
}

export function getWaiters(): IWaiter[] {
let result: IWaiter[] = [];

WAITERS.forEach(item => result.push(item));

return result;
}

export function reset() {
WAITERS.clear();
}

export function getPendingWaiterState(): IPendingWaiterState {
let result: IPendingWaiterState = {
pending: 0,
waiters: {},
};

WAITERS.forEach(waiter => {
if (waiter.waitUntil()) {
result.pending++;

let debugInfo = waiter.debugInfo();
result.waiters[waiter.name] = debugInfo || true;
}
});

return result;
}

export function shouldWait(): boolean {
let state = getPendingWaiterState();

return state.pending > 0;
}
Loading