Example #1
0
	test('Push/Iter', function () {
		const list = new LinkedList<number>();
		list.push(0);
		list.push(1);
		list.push(2);
		assertElements(list, 0, 1, 2);
	});
Example #2
0
	test('unshift/Iter', function () {
		const list = new LinkedList<number>();
		list.unshift(0);
		list.unshift(1);
		list.unshift(2);
		assertElements(list, 2, 1, 0);
	});
Example #3
0
	test('Insert/Iter', function () {
		const list = new LinkedList<number>();
		list.insert(0);
		list.insert(1);
		list.insert(2);
		assertElements(list, 0, 1, 2);
	});
Example #4
0
	test('unshift/toArray', () => {
		let list = new LinkedList<string>();
		list.unshift('foo');
		list.unshift('bar');
		list.unshift('far');
		list.unshift('boo');
		assertElements(list, 'boo', 'far', 'bar', 'foo');
	});
Example #5
0
	function assertElements<E>(list: LinkedList<E>, ...elements: E[]) {
		// first: assert toArray
		assert.deepEqual(list.toArray(), elements);

		// second: assert iterator
		for (let iter = list.iterator(), element = iter.next(); !element.done; element = iter.next()) {
			assert.equal(elements.shift(), element.value);
		}
		assert.equal(elements.length, 0);
	}
Example #6
0
	public entries(): [Function, any][] {
		if (!this._callbacks) {
			return [];
		}
		return this._callbacks
			? this._callbacks.toArray()
			: [];
	}
Example #7
0
	public add(callback: Function, context: any = null, bucket?: IDisposable[]): () => void {
		if (!this._callbacks) {
			this._callbacks = new LinkedList<[Function, any]>();
		}
		const remove = this._callbacks.push([callback, context]);
		if (Array.isArray(bucket)) {
			bucket.push({ dispose: remove });
		}
		return remove;
	}
Example #8
0
	test('unshift/Remove', function () {
		let list = new LinkedList<number>();
		let disp = list.unshift(0);
		list.unshift(1);
		list.unshift(2);
		disp();
		assertElements(list, 2, 1);

		list = new LinkedList<number>();
		list.unshift(0);
		disp = list.unshift(1);
		list.unshift(2);
		disp();
		assertElements(list, 2, 0);

		list = new LinkedList<number>();
		list.unshift(0);
		list.unshift(1);
		disp = list.unshift(2);
		disp();
		assertElements(list, 1, 0);
	});
Example #9
0
	test('Push/Remove', function () {
		let list = new LinkedList<number>();
		let disp = list.push(0);
		list.push(1);
		list.push(2);
		disp();
		assertElements(list, 1, 2);

		list = new LinkedList<number>();
		list.push(0);
		disp = list.push(1);
		list.push(2);
		disp();
		assertElements(list, 0, 2);

		list = new LinkedList<number>();
		list.push(0);
		list.push(1);
		disp = list.push(2);
		disp();
		assertElements(list, 0, 1);
	});
Example #10
0
	public invoke(...args: any[]): any[] {
		if (!this._callbacks) {
			return undefined;
		}

		const ret: any[] = [];
		const elements = this._callbacks.toArray();

		for (const [callback, context] of elements) {
			try {
				ret.push(callback.apply(context, args));
			} catch (e) {
				onUnexpectedError(e);
			}
		}
		return ret;
	}