Skip to content

Commit

Permalink
Fixed formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
drpepper committed Apr 10, 2024
1 parent e2dae6f commit 745e027
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
71 changes: 37 additions & 34 deletions src/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as _ from "underscore";
// @es-li
export function fillInOptions<T>(
options: Partial<T> | unknown,
defaults: T
defaults: T,
): T {
if (options) return { ...defaults, ...(options as object) };
else return defaults;
Expand Down Expand Up @@ -41,7 +41,7 @@ export function isEventTarget(emitter: object): emitter is EventTarget {
export type UnsubscribeFunction = (
emitter: object,
event: string,
cb: () => void
cb: () => void,
) => void;

export interface SubscriptionHandler {
Expand Down Expand Up @@ -104,7 +104,7 @@ export interface Signal {

export function makeSignal(
name = "default",
params: SignalParams = {}
params: SignalParams = {},
): Signal {
return { name, params };
}
Expand Down Expand Up @@ -159,7 +159,7 @@ export interface TickInfo {
*/
export type ChipFactory = (
context: ChipContext,
signal: Signal
signal: Signal,
) => Chip | undefined;

export type ChipResolvable = Chip | ChipFactory;
Expand All @@ -180,7 +180,7 @@ export function isChip(e: any): e is Chip {
}

export function isChipResolvable(
e: ChipResolvable | ChipActivationInfo
e: ChipResolvable | ChipActivationInfo,
): e is ChipResolvable {
return typeof e === "function" || isChip(e);
}
Expand Down Expand Up @@ -220,7 +220,7 @@ export interface Chip extends NodeEventSource {
tickInfo: TickInfo,
chipContext: ChipContext,
inputSignal: Signal,
reloadMemento?: ReloadMemento
reloadMemento?: ReloadMemento,
): void;

/** Update the chip, provided a new time */
Expand Down Expand Up @@ -269,7 +269,7 @@ export abstract class ChipBase extends EventEmitter implements Chip {
tickInfo: TickInfo,
chipContext: ChipContext,
inputSignal: Signal,
reloadMemento?: ReloadMemento
reloadMemento?: ReloadMemento,
): void {
if (this._state !== "inactive")
throw new Error(`activate() called from state ${this._state}`);
Expand Down Expand Up @@ -342,7 +342,7 @@ export abstract class ChipBase extends EventEmitter implements Chip {
event: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cb: (...args: any[]) => void,
subscriptionHandler?: SubscriptionHandler
subscriptionHandler?: SubscriptionHandler,
): void {
if (!subscriptionHandler) {
if (isNodeEventSource(emitter)) {
Expand All @@ -351,7 +351,7 @@ export abstract class ChipBase extends EventEmitter implements Chip {
subscriptionHandler = new EventTargetSubscriptionHandler();
} else {
throw new Error(
`Emitter is of unknown type "${typeof emitter}", requires custom SubscriptionHandler`
`Emitter is of unknown type "${typeof emitter}", requires custom SubscriptionHandler`,
);
}
}
Expand Down Expand Up @@ -381,7 +381,7 @@ export abstract class ChipBase extends EventEmitter implements Chip {
event: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cb: (...args: any[]) => void,
subscriptionHandler?: SubscriptionHandler
subscriptionHandler?: SubscriptionHandler,
): void {
if (!subscriptionHandler) {
if (isNodeEventSource(emitter)) {
Expand All @@ -390,7 +390,7 @@ export abstract class ChipBase extends EventEmitter implements Chip {
subscriptionHandler = new EventTargetSubscriptionHandler();
} else {
throw new Error(
`Emitter is of unknown type "${typeof emitter}", requires custom SubscriptionHandler`
`Emitter is of unknown type "${typeof emitter}", requires custom SubscriptionHandler`,
);
}
}
Expand All @@ -414,7 +414,7 @@ export abstract class ChipBase extends EventEmitter implements Chip {
protected _unsubscribe(
emitter?: object,
event?: string,
cb?: (...args: unknown[]) => void
cb?: (...args: unknown[]) => void,
): void {
// props should only contain defined arguments
const props = _.pick(
Expand All @@ -423,18 +423,18 @@ export abstract class ChipBase extends EventEmitter implements Chip {
event,
cb,
},
(v) => !!v
(v) => !!v,
);

const [listenersToRemove, listenersToKeep] = _.partition(
this._eventListeners,
props
props,
);
for (const listener of listenersToRemove)
listener.subscriptionHandler.unsubscribe(
listener.emitter,
listener.event,
listener.boundCb
listener.boundCb,
);

this._eventListeners = listenersToKeep;
Expand Down Expand Up @@ -566,7 +566,7 @@ export abstract class Composite extends ChipBase {
tickInfo: TickInfo,
chipContext: ChipContext,
inputSignal: Signal,
reloadMemento?: ReloadMemento
reloadMemento?: ReloadMemento,
): void {
this._childChips = {};
this._childChipContext = {};
Expand Down Expand Up @@ -651,21 +651,21 @@ export abstract class Composite extends ChipBase {
*/
protected _activateChildChip(
chipResolvable: ChipResolvable,
options?: Partial<ActivateChildChipOptions>
options?: Partial<ActivateChildChipOptions>,
): Chip;
/**
* Activate a child chip
* @param options The chip and its options
* @returns The activated chip
*/
protected _activateChildChip(
options: Partial<ChipActivationInfo> & { chip: ChipResolvable }
options: Partial<ChipActivationInfo> & { chip: ChipResolvable },
): Chip;
protected _activateChildChip(
chipOrOptions:
| ChipResolvable
| (Partial<ChipActivationInfo> & { chip: ChipResolvable }),
options?: Partial<ActivateChildChipOptions>
options?: Partial<ActivateChildChipOptions>,
): Chip {
if (this.state === "inactive") throw new Error("Composite is inactive");

Expand Down Expand Up @@ -698,7 +698,7 @@ export abstract class Composite extends ChipBase {
options.attribute
} would replace a non-chip. Current attribute value = ${
thisAsAny[options.attribute]
}`
}`,
);

const existingChip = thisAsAny[options.attribute] as Chip;
Expand All @@ -721,7 +721,7 @@ export abstract class Composite extends ChipBase {
this._chipContext,
this._childChipContext,
this.defaultChildChipContext,
options.context
options.context,
);

let chip: Chip;
Expand Down Expand Up @@ -793,7 +793,7 @@ export abstract class Composite extends ChipBase {
if (options.includeInChildContext) {
if (!providedId)
throw new Error(
"To include a child chip in the context, provide an attribute name or ID"
"To include a child chip in the context, provide an attribute name or ID",
);

this._childChipContext[providedId] = chip;
Expand Down Expand Up @@ -884,7 +884,7 @@ export class Parallel extends Composite {

constructor(
chipActivationInfos: Array<ChipActivationInfo | ChipResolvable>,
options?: Partial<ParallelOptions>
options?: Partial<ParallelOptions>,
) {
super();

Expand Down Expand Up @@ -990,7 +990,7 @@ export class Parallel extends Composite {
export class ContextProvider extends Composite {
constructor(
private readonly _context: Record<string, ChipResolvable>,
private readonly _child: ChipResolvable
private readonly _child: ChipResolvable,
) {
super();
}
Expand Down Expand Up @@ -1033,7 +1033,7 @@ export class Sequence extends Composite {

constructor(
chipActivationInfos: Array<ChipActivationInfo | ChipResolvable>,
options?: Partial<SequenceOptions>
options?: Partial<SequenceOptions>,
) {
super();

Expand Down Expand Up @@ -1074,7 +1074,7 @@ export class Sequence extends Composite {
// Copy chip activation info and optionally extend it
const info = Object.assign(
{},
this._chipActivationInfos[this._currentChipIndex]
this._chipActivationInfos[this._currentChipIndex],
);

if (signal) info.inputSignal = signal;
Expand Down Expand Up @@ -1165,7 +1165,7 @@ export type StateTableDescriptor = {

export type SignalFunction = (
context: ChipContext,
signal: Signal
signal: Signal,
) => Signal | string;
export type SignalDescriptor = Signal | SignalFunction;
export type SignalTable = { [name: string]: SignalDescriptor };
Expand Down Expand Up @@ -1203,7 +1203,7 @@ export class StateMachine extends Composite {

constructor(
states: StateTableDescriptor,
options?: Partial<StateMachineOptions>
options?: Partial<StateMachineOptions>,
) {
super();

Expand Down Expand Up @@ -1238,7 +1238,7 @@ export class StateMachine extends Composite {
tickInfo: TickInfo,
chipContext: ChipContext,
inputSignal?: Signal,
reloadMemento?: ReloadMemento
reloadMemento?: ReloadMemento,
) {
super.activate(tickInfo, chipContext, inputSignal, reloadMemento);

Expand Down Expand Up @@ -1276,7 +1276,7 @@ export class StateMachine extends Composite {
this._signals[this._lastSignal.name];
if (_.isFunction(signalDescriptor)) {
nextStateDescriptor = resolveSignal(
signalDescriptor(this._chipContext, signal)
signalDescriptor(this._chipContext, signal),
);
} else {
nextStateDescriptor = signalDescriptor;
Expand Down Expand Up @@ -1480,7 +1480,10 @@ export class Functional extends Composite {
Optionally takes a @that parameter, which is set as _this_ during the call.
*/
export class Lambda extends ChipBase {
constructor(public f: (arg: unknown) => unknown, public that?: unknown) {
constructor(
public f: (arg: unknown) => unknown,
public that?: unknown,
) {
super();
this.that = that || this;
}
Expand Down Expand Up @@ -1536,7 +1539,7 @@ export class WaitForEvent extends ChipBase {
constructor(
public emitter: NodeEventSource,
public eventName: string,
public handler: (...args: unknown[]) => Signal | boolean = _.constant(true)
public handler: (...args: unknown[]) => Signal | boolean = _.constant(true),
) {
super();
}
Expand Down Expand Up @@ -1572,7 +1575,7 @@ export class Alternative extends Composite {

// signal defaults to the string version of the index in the array (to avoid problem of 0 being considered as falsy)
constructor(
chipActivationInfos: Array<ChipResolvable | AlternativeChipActivationInfo>
chipActivationInfos: Array<ChipResolvable | AlternativeChipActivationInfo>,
) {
super();

Expand All @@ -1594,7 +1597,7 @@ export class Alternative extends Composite {
const chipActivationInfo = this._chipActivationInfos[i];

this._subscribe(chipActivationInfo.chip, "terminated", (outputSignal) =>
this._onChildTerminated(i, outputSignal)
this._onChildTerminated(i, outputSignal),
);
this._activateChildChip(chipActivationInfo.chip, {
context: chipActivationInfo.context,
Expand Down
12 changes: 6 additions & 6 deletions src/running.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Runner {
*/
constructor(
private readonly _rootChipResolvable: chip.ChipResolvable,
options?: Partial<RunnerOptions>
options?: Partial<RunnerOptions>,
) {
this._options = chip.fillInOptions(options, new RunnerOptions());
}
Expand Down Expand Up @@ -68,7 +68,7 @@ export class Runner {
};
document.addEventListener(
"visibilitychange",
this._visibilityChangeHandler
this._visibilityChangeHandler,
);

this._runningStatus = "running";
Expand All @@ -87,7 +87,7 @@ export class Runner {
this._rootChip.activate(
tickInfo,
this._rootContext,
this._options.inputSignal
this._options.inputSignal,
);

requestAnimationFrame(() => this._onTick());
Expand All @@ -103,7 +103,7 @@ export class Runner {

document.removeEventListener(
"visibilitychange",
this._visibilityChangeHandler
this._visibilityChangeHandler,
);
delete this._visibilityChangeHandler;
}
Expand Down Expand Up @@ -149,7 +149,7 @@ export class Runner {
tickInfo,
this._rootContext,
chip.makeSignal("afterReload"),
reloadMemento
reloadMemento,
);
});
}
Expand All @@ -169,7 +169,7 @@ export class Runner {
if (this._options.minFps >= 0) {
timeSinceLastTick = Math.min(
timeSinceLastTick,
1000 / this._options.minFps
1000 / this._options.minFps,
);
}

Expand Down

0 comments on commit 745e027

Please sign in to comment.