-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
389a0b2
commit 59d1104
Showing
15 changed files
with
404 additions
and
34 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { CustomElementData, PartytownWebWorker, WinId, WorkerMessageType } from '../types'; | ||
import { defineConstructorName } from '../utils'; | ||
import { getAndSetInstanceId } from './main-instances'; | ||
import { winCtxs } from './main-constants'; | ||
|
||
export const defineCustomElement = ( | ||
winId: WinId, | ||
worker: PartytownWebWorker, | ||
ceData: CustomElementData | ||
) => { | ||
const Cstr = defineConstructorName( | ||
class extends (winCtxs[winId]!.$window$ as any).HTMLElement {}, | ||
ceData[0] | ||
); | ||
|
||
const ceCallbackMethods = | ||
'connectedCallback,disconnectedCallback,attributeChangedCallback,adoptedCallback'.split(','); | ||
|
||
ceCallbackMethods.map( | ||
(callbackMethodName) => | ||
(Cstr.prototype[callbackMethodName] = function (...args: any) { | ||
worker.postMessage([ | ||
WorkerMessageType.CustomElementCallback, | ||
winId, | ||
getAndSetInstanceId(this)!, | ||
callbackMethodName, | ||
args, | ||
]); | ||
}) | ||
); | ||
|
||
Cstr.observedAttributes = ceData[1]; | ||
|
||
return Cstr; | ||
}; |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { callMethod } from './worker-proxy'; | ||
import type { | ||
CustomElementData, | ||
InstanceId, | ||
WinId, | ||
WorkerMessageType, | ||
WorkerNodeConstructors, | ||
} from '../types'; | ||
import { getOrCreateNodeInstance } from './worker-constructors'; | ||
|
||
export const createCustomElementRegistry = (win: any, nodeCstrs: WorkerNodeConstructors) => { | ||
const customElements = 'customElements'; | ||
const registry = new Map<string, any>(); | ||
|
||
win[customElements] = { | ||
define(tagName: string, Cstr: any, opts: any) { | ||
registry.set(tagName, Cstr); | ||
nodeCstrs[tagName.toUpperCase()] = Cstr; | ||
const ceData: CustomElementData = [Cstr.name, Cstr.observedAttributes]; | ||
callMethod(win, [customElements, 'define'], [tagName, ceData, opts]); | ||
}, | ||
|
||
get: (tagName: string) => registry.get(tagName), | ||
|
||
whenDefined: (tagName: string) => | ||
registry.has(tagName) | ||
? Promise.resolve() | ||
: callMethod(win, [customElements, 'whenDefined'], [tagName]), | ||
|
||
upgrade: (elm: any) => callMethod(win, [customElements, 'upgrade'], [elm]), | ||
}; | ||
}; | ||
|
||
export const callCustomElementCallback = ( | ||
_type: WorkerMessageType.CustomElementCallback, | ||
winId: WinId, | ||
instanceId: InstanceId, | ||
callbackName: string, | ||
args: any[] | ||
) => { | ||
const elm = getOrCreateNodeInstance(winId, instanceId) as any; | ||
if (elm && typeof elm[callbackName] === 'function') { | ||
elm[callbackName].apply(elm, args); | ||
} | ||
}; |
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
Oops, something went wrong.
59d1104
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.
Successfully deployed to the following URLs: