Skip to content

Commit

Permalink
Revamp project
Browse files Browse the repository at this point in the history
 - Use yarn 2
 - Use package-preview
 - Remove redundant l1.sequence
 - Remove rollup
 - Use for..of loops instead of forEach
 - Remove runtime parameter validations
  • Loading branch information
sajmoni committed Jul 26, 2021
1 parent 3393a17 commit 16d0dad
Show file tree
Hide file tree
Showing 14 changed files with 8,804 additions and 7,891 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ node_modules
*.log
dist
.vscode
.parcel-cache

.yarn/*
!.yarn/patches
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

77 changes: 77 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions .yarn/releases/yarn-2.4.2.cjs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"

yarnPath: .yarn/releases/yarn-2.4.2.cjs
23 changes: 0 additions & 23 deletions example/package.json

This file was deleted.

2,483 changes: 0 additions & 2,483 deletions example/yarn.lock

This file was deleted.

30 changes: 20 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
"description": "Delayed and repeated code execution for games",
"main": "./dist/index.js",
"files": [
"/dist/*"
"/dist"
],
"scripts": {
"clean": "rm -f l1.tgz",
"release": "yarn clean && yarn audit && yarn build && np --no-tests",
"build": "rm -rf dist && rollup -c rollup.config.js",
"release": "yarn build && np --no-tests",
"build": "rm -rf dist && tsc",
"qa": "tsc && xo",
"go": "yarn clean && yarn build && yarn pack --filename l1.tgz && cd example && yarn refresh && yarn test && cd -"
"test": "yarn build && preview && ava && node performance.js"
},
"repository": {
"type": "git",
Expand All @@ -27,6 +26,14 @@
"useTabs": false,
"bracketSpacing": true
},
"ava": {
"require": [
"ts-node/register"
],
"extensions": [
"ts"
]
},
"xo": {
"prettier": true,
"env": [
Expand All @@ -42,11 +49,14 @@
}
},
"devDependencies": {
"@rollup/plugin-typescript": "8.1.0",
"np": "7.1.0",
"rollup": "2.35.1",
"tslib": "2.0.3",
"typescript": "4.1.3",
"@types/lodash": "^4.14.171",
"@types/node": "^16.4.3",
"ava": "^3.15.0",
"lodash": "^4.17.21",
"np": "7.5.0",
"package-preview": "4.0.0",
"ts-node": "^10.1.0",
"typescript": "4.3.5",
"xo": "0.36.1"
}
}
3 changes: 1 addition & 2 deletions example/performance.js → performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ const _update = () => {

const _get = () => {
_.times((index) => {
const behavior = forever(() => {})
behavior.id = `id-${index}`
const behavior = forever(() => {}, 1, { id: `id-${index}` })
return behavior
}, 1000)
const before = performance.now()
Expand Down
15 changes: 0 additions & 15 deletions rollup.config.js

This file was deleted.

129 changes: 61 additions & 68 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const behaviors: Behavior[] = []
let behaviorsToAdd: Behavior[] = []
let behaviorsToRemove: Behavior[] = []
let _logging = true
const mapIdToBehavior: Record<string, Behavior> = {}

enum BehaviorType {
ONCE = 'once',
Expand All @@ -14,10 +15,11 @@ export type foreverCallback = (updates: number, deltaTime: number) => void
export type everyCallback = (
updates: number,
deltaTime: number,
) => (() => void) | undefined
) => void | (() => void)

// TODO: Split this into 3 subtypes
export type Behavior = {
id: string | null
id?: string
labels: readonly string[]
counter: number
readonly callback: onceCallback | foreverCallback | everyCallback
Expand All @@ -42,10 +44,6 @@ export type Options = {
* Configure level1
*/
export const init = (options: Options): void => {
if (!options) {
throw new Error('level1: The first argument to init is an options object')
}

const { logging = true } = options

_logging = logging
Expand All @@ -55,23 +53,26 @@ export const init = (options: Options): void => {
* Needs to be called on every game update.
*/
export const update = (deltaTime: number): void => {
behaviorsToAdd.forEach((behaviorToAdd: Readonly<Behavior>) => {
for (const behaviorToAdd of behaviorsToAdd) {
behaviors.push(behaviorToAdd)
})
}

behaviorsToAdd = []

behaviorsToRemove.forEach((behaviorToRemove: Readonly<Behavior>) => {
// * Mutate original array for performance reasons
for (const behaviorToRemove of behaviorsToRemove) {
if (behaviorToRemove.id) {
delete mapIdToBehavior[behaviorToRemove.id]
}

const indexToRemove = behaviors.indexOf(behaviorToRemove)
if (indexToRemove >= 0) {
behaviors.splice(indexToRemove, 1)
}
})
}

behaviorsToRemove = []

behaviors.forEach((behavior) => {
for (const behavior of behaviors) {
behavior.counter += 1
if (behavior.type === BehaviorType.ONCE) {
if (behavior.counter === behavior.delay) {
Expand All @@ -91,74 +92,89 @@ export const update = (deltaTime: number): void => {
onDone()
}

remove(behavior)
behaviorsToRemove.push(behavior)
}
}
})
}

const commonBehaviorProperties = {
id: null,
labels: [],
counter: 0,
}
}

/**
* Call a function once after a delay.
*/
export const once = (callback: onceCallback, delay = 1): Behavior => {
if (!callback) {
throw new Error('The fist argument to l1.once needs to be a function')
}

export const once = (
callback: onceCallback,
delay = 1,
options: BehaviorOptions = {},
): Behavior => {
const behavior = {
callback,
delay,
type: BehaviorType.ONCE,
...commonBehaviorProperties,
id: options.id,
labels: options.labels || [],
counter: 0,
}
behaviorsToAdd.push(behavior)

if (options.id) {
mapIdToBehavior[options.id] = behavior
}

return behavior
}

/**
* Call a function forever, each interval game update
*/
export const forever = (callback: foreverCallback, interval = 1): Behavior => {
if (!callback) {
throw new Error('The fist argument to l1.forever needs to be a function')
}

export const forever = (
callback: foreverCallback,
interval = 1,
options: BehaviorOptions = {},
): Behavior => {
const behavior = {
callback,
interval,
type: BehaviorType.FOREVER,
...commonBehaviorProperties,
id: options.id,
labels: options.labels || [],
counter: 0,
}
behaviorsToAdd.push(behavior)

if (options.id) {
mapIdToBehavior[options.id] = behavior
}

return behavior
}

type BehaviorOptions = {
id?: string
labels?: string[]
}

/**
* Call a function `every` update until duration is reached
*/
export const every = (callback: everyCallback, duration: number): Behavior => {
if (!callback || !duration) {
throw new Error(
'The fist argument to l1.every needs to be a function. The second one a duration',
)
}

export const every = (
callback: everyCallback,
duration: number,
options: BehaviorOptions = {},
): Behavior => {
const behavior = {
callback,
duration,
type: BehaviorType.EVERY,
...commonBehaviorProperties,
id: options.id,
labels: options.labels || [],
counter: 0,
}
behaviorsToAdd.push(behavior)

if (options.id) {
mapIdToBehavior[options.id] = behavior
}

return behavior
}

Expand All @@ -172,31 +188,6 @@ export const delay = (delay = 1): Promise<void> =>
}, delay)
})

type sequence<T> = (
callback: (item: T) => void,
interval: number,
list: readonly T[],
) => Promise<void>

/**
* Apply a callback to an item in a list every interval updates.
*/
export const sequence = <T>(
callback: (item: T) => void,
interval: number,
list: readonly T[],
): Promise<void> => {
// eslint-disable-next-line unicorn/no-reduce
return list.reduce(
(p: Readonly<Promise<void>>, item: T) =>
p.then(() => {
callback(item)
return delay(interval)
}),
Promise.resolve(),
)
}

/**
* Remove a behavior
*/
Expand All @@ -219,18 +210,20 @@ export const remove = (behavior: string | Behavior): void => {
/**
* Get a behavior by id
*/
export const get = (id: string) =>
behaviors.find((behavior: Readonly<Behavior>) => behavior.id === id)
export const get = (id: string): Behavior | undefined => mapIdToBehavior[id]

/**
* Get all behaviors
*/
export const getAll = (): Behavior[] => behaviors

// TODO: Index this one
/**
* Get a behavior by label
*
* This is currently not indexed and very slow
*/
export const getByLabel = (label: string) =>
export const getByLabel = (label: string): Behavior[] =>
behaviors.filter((behavior: Readonly<Behavior>) =>
behavior.labels.includes(label),
)
Loading

0 comments on commit 16d0dad

Please sign in to comment.