Tracking de l'application VApp (IHM du jeu)
This commit is contained in:
68
VApp/node_modules/worker-timers-worker/src/helpers/timer.ts
generated
vendored
Normal file
68
VApp/node_modules/worker-timers-worker/src/helpers/timer.ts
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
import { ICallNotification } from '../interfaces';
|
||||
|
||||
const scheduledIntervalIdentifiers: Map<number, number> = new Map();
|
||||
const scheduledTimeoutIdentifiers: Map<number, number> = new Map();
|
||||
|
||||
export const clearScheduledInterval = (timerId: number) => {
|
||||
const identifier = scheduledIntervalIdentifiers.get(timerId);
|
||||
|
||||
if (identifier !== undefined) {
|
||||
clearTimeout(identifier);
|
||||
scheduledIntervalIdentifiers.delete(timerId);
|
||||
} else {
|
||||
throw new Error(`There is no interval scheduled with the given id "${timerId}".`);
|
||||
}
|
||||
};
|
||||
|
||||
export const clearScheduledTimeout = (timerId: number) => {
|
||||
const identifier = scheduledTimeoutIdentifiers.get(timerId);
|
||||
|
||||
if (identifier !== undefined) {
|
||||
clearTimeout(identifier);
|
||||
scheduledTimeoutIdentifiers.delete(timerId);
|
||||
} else {
|
||||
throw new Error(`There is no timeout scheduled with the given id "${timerId}".`);
|
||||
}
|
||||
};
|
||||
|
||||
const computeDelayAndExpectedCallbackTime = (delay: number, nowInMainThread: number) => {
|
||||
let now: number;
|
||||
let remainingDelay: number;
|
||||
const nowInWorker = performance.now();
|
||||
const elapsed = Math.max(0, nowInWorker - nowInMainThread);
|
||||
|
||||
now = nowInWorker;
|
||||
remainingDelay = delay - elapsed;
|
||||
|
||||
const expected = now + remainingDelay;
|
||||
|
||||
return { expected, remainingDelay };
|
||||
};
|
||||
|
||||
const setTimeoutCallback = (identifiers: Map<number, number>, timerId: number, expected: number, timerType: string) => {
|
||||
const now = performance.now();
|
||||
|
||||
if (now > expected) {
|
||||
postMessage(<ICallNotification>{ id: null, method: 'call', params: { timerId, timerType } });
|
||||
} else {
|
||||
identifiers.set(timerId, setTimeout(setTimeoutCallback, expected - now, identifiers, timerId, expected, timerType));
|
||||
}
|
||||
};
|
||||
|
||||
export const scheduleInterval = (delay: number, timerId: number, nowInMainThread: number) => {
|
||||
const { expected, remainingDelay } = computeDelayAndExpectedCallbackTime(delay, nowInMainThread);
|
||||
|
||||
scheduledIntervalIdentifiers.set(
|
||||
timerId,
|
||||
setTimeout(setTimeoutCallback, remainingDelay, scheduledIntervalIdentifiers, timerId, expected, 'interval')
|
||||
);
|
||||
};
|
||||
|
||||
export const scheduleTimeout = (delay: number, timerId: number, nowInMainThread: number) => {
|
||||
const { expected, remainingDelay } = computeDelayAndExpectedCallbackTime(delay, nowInMainThread);
|
||||
|
||||
scheduledTimeoutIdentifiers.set(
|
||||
timerId,
|
||||
setTimeout(setTimeoutCallback, remainingDelay, scheduledTimeoutIdentifiers, timerId, expected, 'timeout')
|
||||
);
|
||||
};
|
||||
5
VApp/node_modules/worker-timers-worker/src/interfaces/broker-event.ts
generated
vendored
Normal file
5
VApp/node_modules/worker-timers-worker/src/interfaces/broker-event.ts
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import { TBrokerMessage } from '../types';
|
||||
|
||||
export interface IBrokerEvent extends Event {
|
||||
data: TBrokerMessage;
|
||||
}
|
||||
13
VApp/node_modules/worker-timers-worker/src/interfaces/call-notification.ts
generated
vendored
Normal file
13
VApp/node_modules/worker-timers-worker/src/interfaces/call-notification.ts
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
import { TTimerType } from '../types';
|
||||
|
||||
export interface ICallNotification {
|
||||
id: null;
|
||||
|
||||
method: 'call';
|
||||
|
||||
params: {
|
||||
timerId: number;
|
||||
|
||||
timerType: TTimerType;
|
||||
};
|
||||
}
|
||||
13
VApp/node_modules/worker-timers-worker/src/interfaces/clear-request.ts
generated
vendored
Normal file
13
VApp/node_modules/worker-timers-worker/src/interfaces/clear-request.ts
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
import { TTimerType } from '../types';
|
||||
|
||||
export interface IClearRequest {
|
||||
id: number;
|
||||
|
||||
method: 'clear';
|
||||
|
||||
params: {
|
||||
timerId: number;
|
||||
|
||||
timerType: TTimerType;
|
||||
};
|
||||
}
|
||||
5
VApp/node_modules/worker-timers-worker/src/interfaces/clear-response.ts
generated
vendored
Normal file
5
VApp/node_modules/worker-timers-worker/src/interfaces/clear-response.ts
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
export interface IClearResponse {
|
||||
error: null;
|
||||
|
||||
id: number;
|
||||
}
|
||||
9
VApp/node_modules/worker-timers-worker/src/interfaces/error-notification.ts
generated
vendored
Normal file
9
VApp/node_modules/worker-timers-worker/src/interfaces/error-notification.ts
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
export interface IErrorNotification {
|
||||
error: {
|
||||
message: string;
|
||||
};
|
||||
|
||||
id: null;
|
||||
|
||||
result: null;
|
||||
}
|
||||
9
VApp/node_modules/worker-timers-worker/src/interfaces/error-response.ts
generated
vendored
Normal file
9
VApp/node_modules/worker-timers-worker/src/interfaces/error-response.ts
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
export interface IErrorResponse {
|
||||
error: {
|
||||
message: string;
|
||||
};
|
||||
|
||||
id: number;
|
||||
|
||||
result: null;
|
||||
}
|
||||
8
VApp/node_modules/worker-timers-worker/src/interfaces/index.ts
generated
vendored
Normal file
8
VApp/node_modules/worker-timers-worker/src/interfaces/index.ts
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
export * from './broker-event';
|
||||
export * from './call-notification';
|
||||
export * from './clear-request';
|
||||
export * from './clear-response';
|
||||
export * from './error-notification';
|
||||
export * from './error-response';
|
||||
export * from './set-notification';
|
||||
export * from './worker-event';
|
||||
17
VApp/node_modules/worker-timers-worker/src/interfaces/set-notification.ts
generated
vendored
Normal file
17
VApp/node_modules/worker-timers-worker/src/interfaces/set-notification.ts
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
import { TTimerType } from '../types';
|
||||
|
||||
export interface ISetNotification {
|
||||
id: null;
|
||||
|
||||
method: 'set';
|
||||
|
||||
params: {
|
||||
delay: number;
|
||||
|
||||
now: number;
|
||||
|
||||
timerId: number;
|
||||
|
||||
timerType: TTimerType;
|
||||
};
|
||||
}
|
||||
5
VApp/node_modules/worker-timers-worker/src/interfaces/worker-event.ts
generated
vendored
Normal file
5
VApp/node_modules/worker-timers-worker/src/interfaces/worker-event.ts
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import { TWorkerMessage } from '../types';
|
||||
|
||||
export interface IWorkerEvent extends Event {
|
||||
data: TWorkerMessage;
|
||||
}
|
||||
54
VApp/node_modules/worker-timers-worker/src/module.ts
generated
vendored
Normal file
54
VApp/node_modules/worker-timers-worker/src/module.ts
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
import { clearScheduledInterval, clearScheduledTimeout, scheduleInterval, scheduleTimeout } from './helpers/timer';
|
||||
import { IBrokerEvent, IClearResponse, IErrorNotification, IErrorResponse } from './interfaces';
|
||||
|
||||
/*
|
||||
* @todo Explicitly referencing the barrel file seems to be necessary when enabling the
|
||||
* isolatedModules compiler option.
|
||||
*/
|
||||
export * from './interfaces/index';
|
||||
export * from './types/index';
|
||||
|
||||
addEventListener('message', ({ data }: IBrokerEvent) => {
|
||||
try {
|
||||
if (data.method === 'clear') {
|
||||
const {
|
||||
id,
|
||||
params: { timerId, timerType }
|
||||
} = data;
|
||||
|
||||
if (timerType === 'interval') {
|
||||
clearScheduledInterval(timerId);
|
||||
|
||||
postMessage(<IClearResponse>{ error: null, id });
|
||||
} else if (timerType === 'timeout') {
|
||||
clearScheduledTimeout(timerId);
|
||||
|
||||
postMessage(<IClearResponse>{ error: null, id });
|
||||
} else {
|
||||
throw new Error(`The given type "${timerType}" is not supported`);
|
||||
}
|
||||
} else if (data.method === 'set') {
|
||||
const {
|
||||
params: { delay, now, timerId, timerType }
|
||||
} = data;
|
||||
|
||||
if (timerType === 'interval') {
|
||||
scheduleInterval(delay, timerId, now);
|
||||
} else if (timerType === 'timeout') {
|
||||
scheduleTimeout(delay, timerId, now);
|
||||
} else {
|
||||
throw new Error(`The given type "${timerType}" is not supported`);
|
||||
}
|
||||
} else {
|
||||
throw new Error(`The given method "${(<any>data).method}" is not supported`);
|
||||
}
|
||||
} catch (err) {
|
||||
postMessage(<IErrorNotification | IErrorResponse>{
|
||||
error: {
|
||||
message: err.message
|
||||
},
|
||||
id: data.id,
|
||||
result: null
|
||||
});
|
||||
}
|
||||
});
|
||||
6
VApp/node_modules/worker-timers-worker/src/tsconfig.json
generated
vendored
Normal file
6
VApp/node_modules/worker-timers-worker/src/tsconfig.json
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"isolatedModules": true
|
||||
},
|
||||
"extends": "tsconfig-holy-grail/src/tsconfig-web-worker"
|
||||
}
|
||||
3
VApp/node_modules/worker-timers-worker/src/types/broker-message.ts
generated
vendored
Normal file
3
VApp/node_modules/worker-timers-worker/src/types/broker-message.ts
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import { IClearRequest, ISetNotification } from '../interfaces';
|
||||
|
||||
export type TBrokerMessage = IClearRequest | ISetNotification;
|
||||
3
VApp/node_modules/worker-timers-worker/src/types/index.ts
generated
vendored
Normal file
3
VApp/node_modules/worker-timers-worker/src/types/index.ts
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './broker-message';
|
||||
export * from './timer-type';
|
||||
export * from './worker-message';
|
||||
1
VApp/node_modules/worker-timers-worker/src/types/timer-type.ts
generated
vendored
Normal file
1
VApp/node_modules/worker-timers-worker/src/types/timer-type.ts
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export type TTimerType = 'interval' | 'timeout';
|
||||
3
VApp/node_modules/worker-timers-worker/src/types/worker-message.ts
generated
vendored
Normal file
3
VApp/node_modules/worker-timers-worker/src/types/worker-message.ts
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import { ICallNotification, IClearResponse, IErrorNotification, IErrorResponse } from '../interfaces';
|
||||
|
||||
export type TWorkerMessage = ICallNotification | IClearResponse | IErrorNotification | IErrorResponse;
|
||||
Reference in New Issue
Block a user