Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Changes to align with shim refactors #346

Merged
merged 2 commits into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 16 additions & 16 deletions src/IdentityRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ interface Entry<V> {
readonly value: V;
}

interface State<V> {
interface State<V extends object> {
readonly entryMap: Map<Identity, Entry<V>>;
readonly idMap: WeakMap<V, Identity>;
}

const privateStateMap = new WeakMap<IdentityRegistry<any>, State<any>>();

function getState<V>(instance: IdentityRegistry<V>): State<V> {
return privateStateMap.get(instance);
function getState<V extends object>(instance: IdentityRegistry<V>): State<V> {
return privateStateMap.get(instance)!;
}

/**
Expand All @@ -31,7 +31,7 @@ export type Identity = string | symbol;
/**
* A registry of values, mapped by identities.
*/
export default class IdentityRegistry<V extends Object> implements Iterable<[Identity, V]> {
export default class IdentityRegistry<V extends object> implements Iterable<[Identity, V]> {
constructor() {
privateStateMap.set(this, {
entryMap: new Map<Identity, Entry<V>>(),
Expand All @@ -48,7 +48,7 @@ export default class IdentityRegistry<V extends Object> implements Iterable<[Ide
* @return The value
*/
get(id: Identity): V {
const entry = getState<V>(this).entryMap.get(id);
const entry = getState(this).entryMap.get(id);
if (!entry) {
throw new Error(`Could not find a value for identity '${id.toString()}'`);
}
Expand All @@ -62,7 +62,7 @@ export default class IdentityRegistry<V extends Object> implements Iterable<[Ide
* @return `true` if the value has been registered, `false` otherwise
*/
contains(value: V): boolean {
return getState<V>(this).idMap.has(value);
return getState(this).idMap.has(value);
}

/**
Expand All @@ -71,7 +71,7 @@ export default class IdentityRegistry<V extends Object> implements Iterable<[Ide
* @return `true` if the value was removed, `false` otherwise
*/
delete(id: Identity): boolean {
const entry = getState<V>(this).entryMap.get(id);
const entry = getState(this).entryMap.get(id);
if (!entry) {
return false;
}
Expand All @@ -86,7 +86,7 @@ export default class IdentityRegistry<V extends Object> implements Iterable<[Ide
* @return `true` if a value has been registered, `false` otherwise
*/
has(id: Identity): boolean {
return getState<V>(this).entryMap.has(id);
return getState(this).entryMap.has(id);
}

/**
Expand All @@ -97,12 +97,12 @@ export default class IdentityRegistry<V extends Object> implements Iterable<[Ide
* @param value The value
* @return The identifier otherwise
*/
identify(value: V): Identity {
identify(value: V): Identity | undefined {
if (!this.contains(value)) {
throw new Error('Could not identify non-registered value');
}

return getState<V>(this).idMap.get(value);
return getState(this).idMap.get(value);
}

/**
Expand All @@ -126,7 +126,7 @@ export default class IdentityRegistry<V extends Object> implements Iterable<[Ide

const existingId = this.contains(value) ? this.identify(value) : null;
if (existingId && existingId !== id) {
const str = (<Identity> existingId).toString();
const str = existingId.toString();
throw new Error(`The value has already been registered with a different identity (${str})`);
}

Expand All @@ -138,20 +138,20 @@ export default class IdentityRegistry<V extends Object> implements Iterable<[Ide
const handle = {
destroy: () => {
handle.destroy = noop;
getState<V>(this).entryMap.delete(id);
getState(this).entryMap.delete(id);
}
};

entryMap.set(id, { handle, value });
getState<V>(this).idMap.set(value, id);
getState(this).idMap.set(value, id);

return handle;
}

entries(): IterableIterator<[Identity, V]> {
const values = new List<[Identity, V]>();

getState<V>(this).entryMap.forEach((value: Entry<V>, key: Identity) => {
getState(this).entryMap.forEach((value: Entry<V>, key: Identity) => {
values.add([key, value.value]);
});

Expand All @@ -161,15 +161,15 @@ export default class IdentityRegistry<V extends Object> implements Iterable<[Ide
values(): IterableIterator<V> {
const values = new List<V>();

getState<V>(this).entryMap.forEach((value: Entry<V>, key: Identity) => {
getState(this).entryMap.forEach((value: Entry<V>, key: Identity) => {
values.add(value.value);
});

return values.values();
}

ids(): IterableIterator<Identity> {
return getState<V>(this).entryMap.keys();
return getState(this).entryMap.keys();
}

[Symbol.iterator](): IterableIterator<[Identity, V]> {
Expand Down
3 changes: 1 addition & 2 deletions src/MultiMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import '@dojo/shim/Symbol';
import Map from '@dojo/shim/Map';
import { ArrayLike } from '@dojo/shim/interfaces';
import { from as arrayFrom } from '@dojo/shim/array';
import { forOf, Iterable, IterableIterator, ShimIterator } from '@dojo/shim/iterator';

Expand Down Expand Up @@ -36,7 +35,7 @@ export default class MultiMap<T> implements Map<any[], T> {
*
* @return the multi map instance
*/
set(keys: any[], value: T): MultiMap<T> {
set(keys: any[], value: T): this {
let map = this._map;
let childMap;

Expand Down
6 changes: 3 additions & 3 deletions src/aspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function adviseJoinPoint<F extends GenericFunction<T>, T>(this: any, joinPoint:
}
else {
dispatcher = getJoinPointDispatcher(joinPoint);
const adviceMap = dispatchAdviceMap.get(dispatcher);
const adviceMap = dispatchAdviceMap.get(dispatcher)!;
if (type === 'before') {
(adviceMap.before || (adviceMap.before = [])).unshift(<JoinPointBeforeAdvice> advice);
}
Expand Down Expand Up @@ -281,7 +281,7 @@ function getDispatcherObject(target: Targetable, methodName: string): Dispatcher
function getJoinPointDispatcher<F extends GenericFunction<T>, T>(joinPoint: F): F {

function dispatcher(this: Function, ...args: any[]): T {
const { before, after, joinPoint } = dispatchAdviceMap.get(dispatcher);
const { before, after, joinPoint } = dispatchAdviceMap.get(dispatcher)!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be ignoring the strict null checks, I believe you mentioned that it can now correctly return undefined so we should probably guard against that appropriately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was debating this, since from the code below, you could never get a situation where the map for the dispatcher is undefined, because the only way the way the function could be called is if its map had been set.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added comments to document the use of not null assertion is appropriate because we can't logically have undefined values from the maps.

if (before) {
args = before.reduce((previousArgs, advice) => {
const currentArgs = advice.apply(this, previousArgs);
Expand All @@ -300,7 +300,7 @@ function getJoinPointDispatcher<F extends GenericFunction<T>, T>(joinPoint: F):
/* We want to "clone" the advice that has been applied already, if this
* joinPoint is already advised */
if (dispatchAdviceMap.has(joinPoint)) {
const adviceMap = dispatchAdviceMap.get(joinPoint);
const adviceMap = dispatchAdviceMap.get(joinPoint)!;
let { before, after } = adviceMap;
if (before) {
before = before.slice(0);
Expand Down
21 changes: 2 additions & 19 deletions src/lang.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import has from './has';
import { Handle } from '@dojo/interfaces/core';
import { assign } from '@dojo/shim/object';
export { assign } from '@dojo/shim/object';

const slice = Array.prototype.slice;
const hasOwnProperty = Object.prototype.hasOwnProperty;
Expand Down Expand Up @@ -93,24 +94,6 @@ interface ObjectAssignConstructor extends ObjectConstructor {
assign(target: any, ...sources: any[]): any;
}

/**
* Copies the values of all enumerable own properties of one or more source objects to the target object.
*
* @param target The target object to receive values from source objects
* @param sources Any number of objects whose enumerable own properties will be copied to the target object
* @return The modified target object
*/
export const assign = has('object-assign') ?
(<ObjectAssignConstructor> Object).assign :
function (target: any, ...sources: any[]): any {
return _mixin({
deep: false,
inherited: false,
sources: sources,
target: target
});
};

/**
* Creates a new object from the given prototype, and copies all enumerable own properties of one or more
* source objects to the newly created target object.
Expand Down
14 changes: 7 additions & 7 deletions src/request/providers/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const discardedDuplicates = new Set<string>([
]);

function getDataTask(response: NodeResponse): Task<RequestData> {
const data = dataMap.get(response);
const data = dataMap.get(response)!;

if (data.used) {
return Task.reject<any>(new TypeError('Body already read'));
Expand All @@ -134,27 +134,27 @@ export class NodeResponse extends Response {
downloadBody = true;

get bodyUsed(): boolean {
return dataMap.get(this).used;
return dataMap.get(this)!.used;
}

get nativeResponse(): http.IncomingMessage {
return dataMap.get(this).nativeResponse;
return dataMap.get(this)!.nativeResponse;
}

get requestOptions(): NodeRequestOptions {
return dataMap.get(this).requestOptions;
return dataMap.get(this)!.requestOptions;
}

get url(): string {
return dataMap.get(this).url;
return dataMap.get(this)!.url;
}

get download(): Observable<number> {
return dataMap.get(this).downloadObservable;
return dataMap.get(this)!.downloadObservable;
}

get data(): Observable<any> {
return dataMap.get(this).dataObservable;
return dataMap.get(this)!.dataObservable;
}

constructor(response: http.IncomingMessage) {
Expand Down
14 changes: 7 additions & 7 deletions src/request/providers/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface RequestData {
const dataMap = new WeakMap<XhrResponse, RequestData>();

function getDataTask(response: XhrResponse): Task<XMLHttpRequest> {
const data = dataMap.get(response);
const data = dataMap.get(response)!;

if (data.used) {
return Task.reject<any>(new TypeError('Body already read'));
Expand All @@ -54,27 +54,27 @@ export class XhrResponse extends Response {
readonly statusText: string;

get bodyUsed(): boolean {
return dataMap.get(this).used;
return dataMap.get(this)!.used;
}

get nativeResponse(): XMLHttpRequest {
return dataMap.get(this).nativeResponse;
return dataMap.get(this)!.nativeResponse;
}

get requestOptions(): XhrRequestOptions {
return dataMap.get(this).requestOptions;
return dataMap.get(this)!.requestOptions;
}

get url(): string {
return dataMap.get(this).url;
return dataMap.get(this)!.url;
}

get download(): Observable<number> {
return dataMap.get(this).downloadObservable;
return dataMap.get(this)!.downloadObservable;
}

get data(): Observable<any> {
return dataMap.get(this).dataObservable;
return dataMap.get(this)!.dataObservable;
}

constructor(request: XMLHttpRequest) {
Expand Down
7 changes: 2 additions & 5 deletions tests/support/util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Thenable } from '@dojo/shim/interfaces';
export { Thenable } from '@dojo/shim/interfaces';

export function isEventuallyRejected<T>(promise: Thenable<T>): Thenable<boolean> {
return promise.then<any>(function () {
export function isEventuallyRejected<T>(promise: PromiseLike<T>): PromiseLike<boolean> {
return promise.then(function () {
throw new Error('unexpected code path');
}, function () {
return true; // expect rejection
Expand Down
Loading