-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proof of concept: publisher-subscriber model for asset loading #96
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4c6ac3f
pub-sub model for async font loading
mggower 2b30114
pubsub for Asset loading and Icon render object
mggower 9bd25f2
implement new Icon with pubsub loading
mggower 71fc8e7
fix diff in TextHighlight
mggower fd72323
remove old icon textures
mggower File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { warn } from '../../../utils' | ||
|
||
export interface Subscriber<T> { | ||
(asset: T): void | ||
} | ||
|
||
export interface Subscription { | ||
ready: boolean | ||
unsubscribe(): void | ||
} | ||
|
||
export default abstract class Publisher<T, S extends Subscription> { | ||
ready = false | ||
asset: T | null = null | ||
subscribers = new Set<Subscriber<T>>() | ||
|
||
protected abstract caller(): Promise<T> | ||
protected abstract subscription(subscriber: Subscriber<T>): S | ||
|
||
subscribe(subscriber: Subscriber<T>) { | ||
if (this.ready && this.asset !== null) { | ||
subscriber(this.asset) | ||
} else { | ||
this.subscribers.add(subscriber) | ||
} | ||
|
||
return this.subscription(subscriber) | ||
} | ||
|
||
unsubscribe(fn: Subscriber<T>) { | ||
this.subscribers.delete(fn) | ||
return undefined | ||
} | ||
|
||
delete() { | ||
this.subscribers = new Set() | ||
return undefined | ||
} | ||
|
||
protected async load() { | ||
try { | ||
this.asset = await this.caller() | ||
this.ready = true | ||
this.notify(this.asset) | ||
} catch (error) { | ||
warn(error) | ||
this.asset = null | ||
this.ready = false | ||
this.delete() | ||
} | ||
} | ||
|
||
protected notify(asset: T) { | ||
for (const fn of this.subscribers) { | ||
fn(asset) | ||
} | ||
|
||
this.subscribers = new Set() | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { MSAA_QUALITY, RenderTexture, Renderer as PixiRenderer, IRenderableObject, IRendererRenderOptions } from 'pixi.js' | ||
import { MIN_ZOOM } from '../../../utils' | ||
import { Renderer } from '../..' | ||
|
||
export default abstract class TextureAbstract { | ||
resolution = 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: plug in to Renderer options |
||
scaleFactor = MIN_ZOOM | ||
|
||
constructor(protected renderer: Renderer) { | ||
this.renderer = renderer | ||
} | ||
|
||
abstract delete(): void | ||
|
||
protected createRenderTexture(width: number, height: number) { | ||
return RenderTexture.create({ | ||
width, | ||
height, | ||
resolution: this.resolution, | ||
multisample: MSAA_QUALITY.HIGH | ||
}) | ||
} | ||
|
||
protected render<T extends IRenderableObject>(graphic: T, options?: IRendererRenderOptions) { | ||
this.renderer.app.renderer.render(graphic, options) | ||
|
||
return this | ||
} | ||
|
||
protected blit() { | ||
if (this.renderer.app.renderer instanceof PixiRenderer) { | ||
this.renderer.app.renderer.framebuffer.blit() | ||
} | ||
|
||
return this | ||
} | ||
|
||
protected destroy<T extends { destroy: (options?: boolean) => void }>(graphic: T) { | ||
graphic.destroy(true) | ||
return this | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default abstract class TextureCache<T extends { delete: () => void }> { | ||
protected cache: { [key: string]: T } = {} | ||
|
||
delete() { | ||
for (const key in this.cache) { | ||
this.cache[key].delete() | ||
} | ||
|
||
this.cache = {} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
couldn't think of a better name for this method lol