-
Notifications
You must be signed in to change notification settings - Fork 153
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
Implement Canvas API #380
Implement Canvas API #380
Conversation
This reverts commit ca5b033.
Going to request this change holds off until the buffer branch lands since it will change the messaging format. I'll review the changes tomorrow. |
@jridgewell ready for review 👍 |
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.
Is there a test page I can try this code out on? I'm not quite sure I follow the control flow.
if (DEBUG_ENABLED) { | ||
console.info('debug', 'messageToWorker', readableMessageToWorker(this.nodeContext, message)); | ||
} | ||
if (this.config.onSendMessage) { | ||
this.config.onSendMessage(message); | ||
} | ||
this.worker.postMessage(message); | ||
this.worker.postMessage(message, transferables); |
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.
Should this be an optional parameter instead of forcing all messages without transferables to pass an empty array?
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.
Yes :)
}); | ||
|
||
describe('clearRect', () => { | ||
test('context calls clearRect', t => { |
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.
Using a fake describe()
is mixing testing paradigms. Better to just use comment blocks and stick to Ava style.
Discussion thread if you're curious: avajs/ava#222
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.
thanks!
}); | ||
}); | ||
|
||
describe('rota |
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.
s/spy/setter
A "spy" isn't the right name to describe this function.
}); | ||
}); | ||
|
||
describe('rota |
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.
:)
@@ -20,6 +20,10 @@ declare interface Sanitizer { | |||
mutateProperty(node: Node, prop: string, value: string): boolean; | |||
} | |||
|
|||
declare interface HTMLCanvasElement { |
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.
Add a comment explaining why we need to declare this here. I assume it's because OffscreenCanvas types is not available in TS yet?
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.
Correct.transferControlToOffscreen
is not detected as a method because of this.
} | ||
}; | ||
|
||
// TODO: This should only happen in test environemnet. Otherwise, we should throw. |
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.
Fix and remove TODOs.
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.
Will include this in a later PR (there is not a simple way to do this currently, and it does not affect the operation of this API outside of testing environment)
this.delegate('restore', [...arguments], false); | ||
} | ||
|
||
// canvas property is readonly. We don't want to implement getters, but this must be here |
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.
We don't want to implement getters
What does this mean?
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.
Nvm, getters are included now.
|
||
export function OffscreenPolyfillCallProcessor(strings: Strings, workerContext: WorkerContext): CommandExecutor { | ||
return { | ||
execute(mutations: Uint16Array, startPosition: number, target: RenderableElement): number { |
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.
Do we have tests that cover these processor functions?
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.
Now we do 👍
return this.implementation; | ||
} | ||
} | ||
(global as any).OffscreenCanvas = OffscreenCanvas; |
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.
Ruh roh. Globals are generally bad, especially this global since it includes the test runner.
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.
Does this new approach seem like a good idea?
constructor() { | ||
// this.x, y | ||
const context = ({} as unknown) as CanvasRenderingContext2D; | ||
OffscreenCanvas.mostRecentInstance = this; |
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.
Does this look weird to you? :)
Also, do we need it? Why can't this be referenced via canvas.getContext('2d').implementation
?
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.
From our discussion yesterday: this is actually needed for calls that happen before the upgraded (auto-synchronized) version of OffscreenCanvas
is fetched from the main-thread.
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.
Make sure that any code review comments are applied across all instances in your PR e.g. using globals. Comments can be addressed after merge.
constructor() { | ||
// this.x, y | ||
const context = ({} as unknown) as CanvasRenderingContext2D; | ||
FakeOffscreenCanvas.mostRecentInstance = this; |
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.
This is still an issue. Remember what we discussed was the possible solution for this?
function createSetterStub(obj: any, property: string, spy: () => {}) { | ||
obj[property] = 'existent'; | ||
sandbox.stub(obj, property).set(spy); | ||
} |
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.
Same here.
import { CanvasRenderingContext2D } from '../../worker-thread/canvas/CanvasTypes'; | ||
import { createTestingDocument } from '../DocumentCreation'; | ||
|
||
const test = anyTest as TestInterface<{ |
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.
This test is 3K lines of code. We should first remove redundant tests (no need to test same upgrade code path more than once) and then split it up if necessary (how should we segment it?).
const { context2d, deferredUpgrade, implementation, sandbox } = t.context; | ||
const instance = new FakeOffscreenCanvas(); | ||
|
||
const instanceStub = (instance.getContext('2d')['clearRect'] = sandbox.stub()); |
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.
I don't understand how this local FakeOffscreenCanvas
instance is at all related to the <canvas>
element created in beforeEach
.
OffscreenCanvas
).CanvasRenderingContext2D
methods will now work on all browsers.@choumx @jridgewell