-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds more typesafe options to the store.
- Loading branch information
Showing
1 changed file
with
37 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,40 @@ | ||
import { ImageAsset, ImageOptions, SpriteOptions, SpriteAsset } from './image' | ||
import { ImageAsset, ImageOptions, SpriteAsset, SpriteOptions } from './image' | ||
|
||
export class Store { | ||
private database: { [key: string]: any } = [] | ||
|
||
constructor() {} | ||
|
||
public store(key: string, val: any): void { | ||
this.database[key] = val | ||
} | ||
|
||
public get(key: string): any | undefined { | ||
return this.database[key] | ||
} | ||
|
||
public image(key: string, src: string, options?: ImageOptions): void { | ||
const img = new ImageAsset(key, src, options) | ||
this.store(key, img) | ||
} | ||
|
||
public sprite(key: string, src: string, options?: SpriteOptions): void { | ||
const img = new SpriteAsset(key, src, options) | ||
this.store(key, img) | ||
} | ||
|
||
// This function does not work yet! | ||
public select(key: string, val: string): any[] { | ||
return this.database.filter((obj: any) => { | ||
return obj[key] === val | ||
}) | ||
} | ||
private database: { [key: string]: any } = [] | ||
|
||
constructor() {} | ||
|
||
public store(key: string, val: any): void { | ||
this.database[key] = val | ||
} | ||
|
||
public image(key: string, src: string, options?: ImageOptions): void { | ||
const img = new ImageAsset(key, src, options) | ||
this.store(key, img) | ||
} | ||
|
||
public sprite(key: string, src: string, options?: SpriteOptions): void { | ||
const img = new SpriteAsset(key, src, options) | ||
this.store(key, img) | ||
} | ||
|
||
public get<T = any>(key: string): T | undefined { | ||
return this.database[key] | ||
} | ||
|
||
public getImage(key: string): ImageAsset | undefined { | ||
return this.get<ImageAsset>(key) | ||
} | ||
|
||
public getSprite(key: string): SpriteAsset | undefined { | ||
return this.get<SpriteAsset>(key) | ||
} | ||
|
||
// This function does not work yet! | ||
public select(key: string, val: string): any[] { | ||
return this.database.filter((obj: any) => { | ||
return obj[key] === val | ||
}) | ||
} | ||
} |