Skip to content

Commit b90ab59

Browse files
committed
Remove comple event loops with Core.js for plan data flows
1 parent 080e361 commit b90ab59

5 files changed

Lines changed: 41 additions & 87 deletions

File tree

src/App.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ui extends Element {
2020
let msg = event.data.pluginMessage
2121
if (msg.type == 'init-hidden' || msg.type == 'init' || msg.type == 'init-direct') {
2222
this.data.preferences = msg.preferences
23+
this.data.license = msg.license
2324
Tracking.setup(WP_AMPLITUDE_KEY, msg.UUID)
2425
Tracking.track('openPlugin', { cmd: msg.cmd })
2526
}
@@ -88,11 +89,15 @@ class ui extends Element {
8889
}
8990
}
9091

92+
escapeAttribute(str) {
93+
return str.replace(/"/g, '"').replace(/'/g, ''')
94+
}
95+
9196
render() {
9297
if (!this.data.preferences) return '';
9398
return`
9499
<c-toolbar></c-toolbar>
95-
<v-form data-view="index" class="view"></v-form>
100+
<v-form data-view="index" class="view" license="${this.escapeAttribute(JSON.stringify(this.data.license || {}))}"></v-form>
96101
<v-preferences class="view" hidden data-view="preferences"
97102
xspacing="${this.data.preferences.spacing.x}"
98103
yspacing="${this.data.preferences.spacing.y}"
@@ -102,7 +107,7 @@ class ui extends Element {
102107
renamestrategy="${this.data.preferences.rename_strategy}"
103108
layoutparadigm="${this.data.preferences.layout_paradigm || 'rows'}"
104109
></v-preferences>
105-
<v-license class="view" hidden data-view="license"></v-license>
110+
<v-license class="view" hidden data-view="license" license="${this.escapeAttribute(JSON.stringify(this.data.license || {}))}"></v-license>
106111
`
107112
}
108113
}

src/Core.js

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function hashLicenseKey(key) {
3838
}
3939

4040
// Helper function to handle gating for direct menu commands
41-
function ensureDirectCommandGate(commandName, executeCommand, preferences, UUID) {
41+
function ensureDirectCommandGate(commandName, executeCommand, preferences, UUID, license) {
4242
if (shouldShowCountdown()) {
4343
// Show UI with countdown for unlicensed users
4444
figma.showUI(__html__, { width: 360, height: UI_HEIGHT })
@@ -50,7 +50,8 @@ function ensureDirectCommandGate(commandName, executeCommand, preferences, UUID)
5050
type: 'init-direct',
5151
UUID: UUID,
5252
cmd: commandName,
53-
preferences: preferences
53+
preferences: preferences,
54+
license: license
5455
})
5556

5657
// Then send countdown start message
@@ -167,7 +168,8 @@ Storage.getMultiple([
167168
type: 'init-hidden',
168169
UUID: UUID,
169170
cmd: cmd,
170-
preferences: preferences
171+
preferences: preferences,
172+
license: license
171173
})
172174

173175
// Command triggered by user
@@ -176,28 +178,28 @@ Storage.getMultiple([
176178
ensureDirectCommandGate('rename', () => {
177179
cmdRename(preferences.rename_strategy, preferences.start_name, preferences.layout_paradigm || 'rows')
178180
figma.notify('Super Tidy: Rename')
179-
}, preferences, UUID)
181+
}, preferences, UUID, license)
180182
} else
181183
if (cmd == 'reorder') {
182184
// RUNS WITH COUNTDOWN GATE
183185
ensureDirectCommandGate('reorder', () => {
184186
cmdReorder(preferences.layout_paradigm || 'rows')
185187
figma.notify('Super Tidy: Reorder')
186-
}, preferences, UUID)
188+
}, preferences, UUID, license)
187189
} else
188190
if (cmd == 'tidy') {
189191
// RUNS WITH COUNTDOWN GATE
190192
ensureDirectCommandGate('tidy', () => {
191193
cmdTidy(preferences.spacing.x, preferences.spacing.y, preferences.wrap_instances, preferences.layout_paradigm || 'rows')
192194
figma.notify('Super Tidy: Tidy')
193-
}, preferences, UUID)
195+
}, preferences, UUID, license)
194196
} else
195197
if (cmd == 'pager') {
196198
// RUNS WITH COUNTDOWN GATE
197199
ensureDirectCommandGate('pager', () => {
198200
cmdPager(preferences.pager_variable, preferences.layout_paradigm || 'rows')
199201
figma.notify('Super Tidy: Pager')
200-
}, preferences, UUID)
202+
}, preferences, UUID, license)
201203
} else
202204
if (cmd == 'all') {
203205
// RUNS WITH COUNTDOWN GATE
@@ -207,7 +209,7 @@ Storage.getMultiple([
207209
cmdRename(preferences.rename_strategy, preferences.start_name, preferences.layout_paradigm || 'rows')
208210
cmdPager(preferences.pager_variable, preferences.layout_paradigm || 'rows')
209211
figma.notify('Super Tidy')
210-
}, preferences, UUID)
212+
}, preferences, UUID, license)
211213
} else
212214
if (cmd == 'options') {
213215
// OPEN UI
@@ -217,6 +219,7 @@ Storage.getMultiple([
217219
UUID: UUID,
218220
cmd: cmd,
219221
preferences: preferences,
222+
license: license,
220223
AD_LAST_SHOWN_DATE: AD_LAST_SHOWN_DATE,
221224
AD_LAST_SHOWN_IMPRESSION: AD_LAST_SHOWN_IMPRESSION
222225
})
@@ -261,26 +264,6 @@ Storage.getMultiple([
261264
if (msg.type === 'resetImpression') {
262265
Storage.set(Storage.getKey('AD_LAST_SHOWN_IMPRESSION'), 0)
263266
} else
264-
if (msg.type === 'get-license') {
265-
// Return stored license data to UI
266-
Storage.get(Storage.getKey('LICENSE_V1'))
267-
.then(license => {
268-
figma.ui.postMessage({
269-
type: 'license-data',
270-
license: license || null
271-
})
272-
})
273-
} else
274-
if (msg.type === 'get-license-for-gate') {
275-
// Return license data specifically for gate cache updates
276-
Storage.get(Storage.getKey('LICENSE_V1'))
277-
.then(license => {
278-
figma.ui.postMessage({
279-
type: 'license-data-for-gate',
280-
license: license || null
281-
})
282-
})
283-
} else
284267
if (msg.type === 'activate-license') {
285268
// Store license data from UI
286269
const licenseData = {

src/payments/license.js

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -140,33 +140,6 @@ export function validateLicenseOnly(licenseKey) {
140140
return validateGumroadLicense(licenseKey, false) // Use shared validation logic without incrementing
141141
}
142142

143-
/**
144-
* Retrieves stored license data from Figma client storage via postMessage
145-
* @returns {Promise<object|null>} License data or null if not found
146-
*/
147-
export function getStoredLicense() {
148-
return new Promise((resolve) => {
149-
parent.postMessage({
150-
pluginMessage: { type: 'get-license' }
151-
}, '*')
152-
153-
// Listen for response
154-
const handler = (e) => {
155-
const msg = e.data.pluginMessage
156-
if (msg.type === 'license-data') {
157-
window.removeEventListener('message', handler)
158-
resolve(msg.license)
159-
}
160-
}
161-
window.addEventListener('message', handler)
162-
163-
// Timeout after 1 second
164-
setTimeout(() => {
165-
window.removeEventListener('message', handler)
166-
resolve(null)
167-
}, 1000)
168-
})
169-
}
170143

171144
/**
172145
* Activates a license by storing it via postMessage to Core.js

src/ui/views/form/FormView.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,23 @@ class FormView extends Element {
1212
this.data.pendingCommand = null
1313
this.data.showingCountdown = false
1414

15-
// Load license status for gate decisions
16-
this.loadLicenseStatus()
15+
// Load license status for gate decisions from props
16+
this.loadLicenseFromProps()
1717
}
1818

19-
loadLicenseStatus() {
20-
// Request license data from Core.js to update gate cache
21-
parent.postMessage({
22-
pluginMessage: { type: 'get-license-for-gate' }
23-
}, '*')
24-
25-
// Listen for license data response (only once)
26-
const handler = (e) => {
27-
const msg = e.data.pluginMessage
28-
if (msg.type === 'license-data-for-gate') {
29-
setCachedLicenseStatus(msg.license)
30-
window.removeEventListener('message', handler) // Clean up listener
19+
loadLicenseFromProps() {
20+
const licenseAttr = this.getAttribute('license')
21+
if (licenseAttr && licenseAttr !== '{}') {
22+
try {
23+
const license = JSON.parse(licenseAttr.replace(/&quot;/g, '"').replace(/&#39;/g, "'"))
24+
setCachedLicenseStatus(license)
25+
} catch (e) {
26+
console.warn('[FormView] Failed to parse license data:', e)
27+
setCachedLicenseStatus(null)
3128
}
29+
} else {
30+
setCachedLicenseStatus(null)
3231
}
33-
window.addEventListener('message', handler)
34-
35-
// Timeout cleanup after 2 seconds
36-
setTimeout(() => {
37-
window.removeEventListener('message', handler)
38-
}, 2000)
3932
}
4033

4134
handleEmptyState(selection) {

src/ui/views/license/LicenseView.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { setCachedLicenseStatus } from 'src/payments/gate'
55
import {
66
verifyGumroadLicense,
77
decrementLicenseUsage,
8-
getStoredLicense,
98
activateLicense,
109
removeLicense,
1110
createLicenseInfo
@@ -19,13 +18,14 @@ class LicenseView extends Element {
1918
this.data.statusType = '' // 'success', 'error', or ''
2019
this.data.isLicensed = false
2120
this.data.licenseInfo = null
22-
this.loadCurrentLicense()
21+
this.loadLicenseFromProps()
2322
}
2423

25-
loadCurrentLicense() {
26-
// Check if user already has a license
27-
getStoredLicense()
28-
.then(license => {
24+
loadLicenseFromProps() {
25+
const licenseAttr = this.getAttribute('license')
26+
if (licenseAttr && licenseAttr !== '{}') {
27+
try {
28+
const license = JSON.parse(licenseAttr.replace(/&quot;/g, '"').replace(/&#39;/g, "'"))
2929
if (license && license.licensed) {
3030
this.data.isLicensed = true
3131
this.data.licenseInfo = license
@@ -35,10 +35,10 @@ class LicenseView extends Element {
3535
}
3636
this.render()
3737
}
38-
})
39-
.catch(e => {
40-
// No existing license found
41-
})
38+
} catch (e) {
39+
console.warn('[LicenseView] Failed to parse license data:', e)
40+
}
41+
}
4242
}
4343

4444
activateLicense() {

0 commit comments

Comments
 (0)