Skip to content

Commit

Permalink
feat(Events): use strongly typed event emitters
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This removes the string based on/off API that was present on most core game classes in favor of strongly typed event emitters that are compatible with Angular bindings.

This makes the API usage simpler, and adds strong types to emitted events making them more reliable to use in the frontend.
  • Loading branch information
justindujardin committed Oct 30, 2022
1 parent cde20f5 commit dbeb450
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 393 deletions.
118 changes: 0 additions & 118 deletions src/app/core/events.spec.ts

This file was deleted.

182 changes: 0 additions & 182 deletions src/app/core/events.ts

This file was deleted.

74 changes: 2 additions & 72 deletions src/app/core/time.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import * as _ from 'underscore';
import { Events } from './events';
import { IProcessObject, Time } from './time';

class MockTimeObject extends Events implements IProcessObject {
_uid: string = _.uniqueId('p');

tick(elapsed: number) {
this.trigger('tick');
}
}
import { Time } from './time';

describe('Time', () => {
it('should be defined', () => {
Expand All @@ -17,7 +8,7 @@ describe('Time', () => {

it('should expose a static instance for shared use', () => {
const t: Time = Time.get();
expect(t.polyFillAnimationFrames).toBeDefined();
expect(t).toBeDefined();
});

describe('addObject', () => {
Expand Down Expand Up @@ -81,65 +72,4 @@ describe('Time', () => {
});
});
});

describe('polyFillAnimationFrames', () => {
it('should trigger time updates with polyfill and setInterval', (done) => {
let olds: any = {
requestAnimationFrame: window.requestAnimationFrame,
};
const vendors: string[] = ['ms', 'moz', 'webkit', 'o'];
const w: any = window;
for (let i = 0; i < vendors.length; i++) {
olds = w[vendors[i] + 'RequestAnimationFrame'];
w[vendors[i] + 'RequestAnimationFrame'] = null;
}
const oldRaf = w.requestAnimationFrame;
w.requestAnimationFrame = null;

const t: Time = new Time();
const m: MockTimeObject = new MockTimeObject();
t.addObject(m);
m.once('tick', () => {
t.stop();
t.removeObject(m);
expect(window.requestAnimationFrame).toBeDefined();
_.each(olds, (value, key: any) => {
(window as any)[key] = value;
});
w.requestAnimationFrame = oldRaf;
done();
});
t.start();
});
});

const functions = ['webkitRequestAnimationFrame', 'mozRequestAnimationFrame'];
functions.forEach((fnName) => {
const w: any = window;
if (!w[fnName]) {
return;
}
it('should apply polyfill if not present on window', () => {
const oldRaf: any = w.requestAnimationFrame;
const oldVendorRaf: any = w[fnName];

w.requestAnimationFrame = null;
w[fnName] = null;

let t = new Time();
expect(window.requestAnimationFrame).toBeDefined();

w.requestAnimationFrame = oldRaf;
w[fnName] = oldVendorRaf;
});
it('should be patched as requestAnimationFrame if present on window', () => {
if (window.hasOwnProperty(fnName)) {
const oldRaf: any = w.requestAnimationFrame;
w.requestAnimationFrame = null;
let t = new Time();
expect(window.requestAnimationFrame).toBeDefined();
w.requestAnimationFrame = oldRaf;
}
});
});
});
Loading

0 comments on commit dbeb450

Please sign in to comment.