Skip to content

Commit

Permalink
feat: remove custom Thenable polyfill in favor of native promises
Browse files Browse the repository at this point in the history
It should no longer be necessary. Moved it to https://github.com/lojjic/tiny-thenable for posterity.
  • Loading branch information
lojjic committed Mar 12, 2022
1 parent 1fe020b commit 7af402e
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 347 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -24,7 +24,6 @@
"mitt": "^1.2.0",
"node-fetch": "^2.6.0",
"postcss": "^8.2.6",
"promises-aplus-tests": "^2.1.2",
"rollup": "^2.39.0",
"rollup-plugin-buble": "^0.19.8",
"rollup-plugin-commonjs": "^10.1.0",
Expand Down
11 changes: 5 additions & 6 deletions packages/troika-flex-layout/src/FlexLayoutProcessor.js
@@ -1,4 +1,4 @@
import { defineWorkerModule, ThenableWorkerModule } from 'troika-worker-utils'
import { defineWorkerModule } from 'troika-worker-utils'
import { typesetterWorkerModule } from 'troika-three-text'
import yogaFactory from '../libs/yoga.factory.js'

Expand Down Expand Up @@ -285,9 +285,8 @@ export const flexLayoutProcessorWorkerModule = defineWorkerModule({
yogaFactory,
typesetterWorkerModule,
createFlexLayoutProcessor,
ThenableWorkerModule
],
init(yogaFactory, layoutEngine, create, Thenable) {
init(yogaFactory, layoutEngine, create) {
const Yoga = yogaFactory()
function measure(params) {
let result = null
Expand All @@ -296,9 +295,9 @@ export const flexLayoutProcessorWorkerModule = defineWorkerModule({
}
const process = create(Yoga, layoutEngine.loadFont, measure)
return function(styleTree) {
const thenable = new Thenable()
process(styleTree, thenable.resolve)
return thenable
return new Promise(resolve => {
process(styleTree, resolve)
})
}
}
})
Expand Down
26 changes: 13 additions & 13 deletions packages/troika-three-text/src/SDFGenerator.js
@@ -1,4 +1,4 @@
import { defineWorkerModule, terminateWorker, Thenable } from 'troika-worker-utils'
import { defineWorkerModule, terminateWorker } from 'troika-worker-utils'
import createSDFGenerator from 'webgl-sdf-generator'

const now = () => (self.performance || Date).now()
Expand Down Expand Up @@ -48,20 +48,20 @@ const generateSDF_GL = /*#__PURE__*/function() {
timer = queue.length ? setTimeout(nextChunk, 0) : 0
}
return (...args) => {
const thenable = Thenable()
queue.push(() => {
const start = now()
try {
mainThreadGenerator.webgl.generateIntoCanvas(...args)
thenable.resolve({timing: now() - start})
} catch(err) {
thenable.reject(err)
return new Promise((resolve, reject) => {
queue.push(() => {
const start = now()
try {
mainThreadGenerator.webgl.generateIntoCanvas(...args)
resolve({ timing: now() - start })
} catch (err) {
reject(err)
}
})
if (!timer) {
timer = setTimeout(nextChunk, 0)
}
})
if (!timer) {
timer = setTimeout(nextChunk, 0)
}
return thenable
}
}()

Expand Down
17 changes: 8 additions & 9 deletions packages/troika-three-text/src/TextBuilder.js
@@ -1,5 +1,5 @@
import { Color, CanvasTexture, LinearFilter } from 'three'
import { defineWorkerModule, ThenableWorkerModule, Thenable } from 'troika-worker-utils'
import { defineWorkerModule } from 'troika-worker-utils'
import { createTypesetter } from './Typesetter.js'
import { generateSDF, warmUpSDFCanvas, resizeWebGLCanvasWithoutClearing } from './SDFGenerator.js'
import bidiFactory from 'bidi-js'
Expand Down Expand Up @@ -240,7 +240,7 @@ function getTextRenderInfo(args, callback) {
sdfTexture.dispose()
}

Thenable.all(neededSDFs.map(glyphInfo =>
Promise.all(neededSDFs.map(glyphInfo =>
generateGlyphSDF(glyphInfo, atlas, args.gpuAccelerateSDF).then(({timing}) => {
timings.sdf[glyphInfo.atlasIndex] = timing
})
Expand Down Expand Up @@ -287,7 +287,7 @@ function getTextRenderInfo(args, callback) {
// While the typesetting request is being handled, go ahead and make sure the atlas canvas context is
// "warmed up"; the first request will be the longest due to shader program compilation so this gets
// a head start on that process before SDFs actually start getting processed.
Thenable.all([]).then(() => {
Promise.resolve().then(() => {
if (!atlas.contextLost) {
warmUpSDFCanvas(sdfTexture.image)
}
Expand Down Expand Up @@ -350,7 +350,7 @@ function initContextLossHandling(atlas) {
promises.push(generateGlyphSDF(glyph, atlas, true))
})
})
Thenable.all(promises).then(() => {
Promise.all(promises).then(() => {
atlas.sdfTexture.needsUpdate = true
})
})
Expand Down Expand Up @@ -417,13 +417,12 @@ const typesetInWorker = /*#__PURE__*/defineWorkerModule({
name: 'Typesetter',
dependencies: [
typesetterWorkerModule,
ThenableWorkerModule
],
init(typesetter, Thenable) {
init(typesetter) {
return function(args) {
const thenable = new Thenable()
typesetter.typeset(args, thenable.resolve)
return thenable
return new Promise(resolve => {
typesetter.typeset(args, resolve)
})
}
},
getTransferables(result) {
Expand Down
29 changes: 0 additions & 29 deletions packages/troika-worker-utils/__tests__/Thenable.test.js

This file was deleted.

This file was deleted.

0 comments on commit 7af402e

Please sign in to comment.