Skip to content

Commit

Permalink
Remove private method syntax from Bridge class
Browse files Browse the repository at this point in the history
It's not supported by SkyPack CDN,

skypackjs/skypack-cdn#308
  • Loading branch information
afcapel committed Sep 20, 2023
1 parent df1c2b4 commit b9e7522
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions src/bridge.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
export class Bridge {
#adapter
#lastMessageId
#pendingMessages
#pendingCallbacks

constructor() {
this.#adapter = null
this.#lastMessageId = 0
this.#pendingMessages = []
this.#pendingCallbacks = new Map()
this.adapter = null
this.lastMessageId = 0
this.pendingMessages = []
this.pendingCallbacks = new Map()
}

start() {
Expand All @@ -20,27 +15,27 @@ export class Bridge {
}

supportsComponent(component) {
if (this.#adapter) {
return this.#adapter.supportsComponent(component)
if (this.adapter) {
return this.adapter.supportsComponent(component)
} else {
return false
}
}

send({ component, event, data, callback }) {
if (!this.#adapter) {
this.#savePendingMessage({ component, event, data, callback })
if (!this.adapter) {
this.savePendingMessage({ component, event, data, callback })
return null
}

if (!this.supportsComponent(component)) return null

const id = this.generateMessageId()
const message = { id: id, component: component, event: event, data: data || {} }
this.#adapter.receive(message)
this.adapter.receive(message)

if (callback) {
this.#pendingCallbacks.set(id, callback)
this.pendingCallbacks.set(id, callback)
}

return id
Expand All @@ -51,48 +46,50 @@ export class Bridge {
}

executeCallbackFor(message) {
const callback = this.#pendingCallbacks.get(message.id)
const callback = this.pendingCallbacks.get(message.id)
if (callback) {
callback(message)
}
}

removeCallbackFor(messageId) {
if (this.#pendingCallbacks.has(messageId)) {
this.#pendingCallbacks.delete(messageId)
if (this.pendingCallbacks.has(messageId)) {
this.pendingCallbacks.delete(messageId)
}
}

removePendingMessagesFor(component) {
this.#pendingMessages = this.#pendingMessages.filter(message => message.component != component)
this.pendingMessages = this.pendingMessages.filter(message => message.component != component)
}

generateMessageId() {
const id = ++this.#lastMessageId
const id = ++this.lastMessageId
return id.toString()
}

setAdapter(adapter) {
this.#adapter = adapter
this.adapter = adapter

// Configure <html> attributes
document.documentElement.dataset.bridgePlatform = this.#adapter.platform
document.documentElement.dataset.bridgePlatform = this.adapter.platform
this.adapterDidUpdateSupportedComponents()
this.#sendPendingMessages()
this.sendPendingMessages()
}

adapterDidUpdateSupportedComponents() {
if (this.#adapter) {
document.documentElement.dataset.bridgeComponents = this.#adapter.supportedComponents.join(" ")
if (this.adapter) {
document.documentElement.dataset.bridgeComponents = this.adapter.supportedComponents.join(" ")
}
}

#savePendingMessage(message) {
this.#pendingMessages.push(message)
// Private

savePendingMessage(message) {
this.pendingMessages.push(message)
}

#sendPendingMessages() {
this.#pendingMessages.forEach(message => this.send(message))
this.#pendingMessages = []
sendPendingMessages() {
this.pendingMessages.forEach(message => this.send(message))
this.pendingMessages = []
}
}

0 comments on commit b9e7522

Please sign in to comment.