-
Notifications
You must be signed in to change notification settings - Fork 128
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
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
packages/signals/browser-signals/src/core/processor/processor.ts
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,44 @@ | ||
// class SignalsEventProcessor { | ||
// private sandbox: Sandbox | ||
// private signalsRuntime: SignalsRuntime | ||
// constructor(public analyticsInstance: Analytics) { | ||
// this.sandbox = new Sandbox(analyticsInstance.cdnSettings.edgeFn) | ||
// this.signalsRuntime = new SignalsRuntime(new SignalBuffer()) | ||
// } | ||
|
||
// process(signal: Signal) { | ||
// // todo: think about loop protection | ||
// const events = await this.sandbox.process(signal) | ||
// // proxy arguments to real analytics instance | ||
// Object.keys(events).forEach((eventName) => this.analyticsInstance[eventName](events[eventName])) | ||
// } | ||
|
||
// cleanup() { | ||
// this.sandbox.destroy() | ||
// } | ||
// } | ||
|
||
// class Sandbox { | ||
// edgeFn: Promise<string> | ||
// jsSandbox = createWorkerBox() | ||
// signalsRuntime: Signals | ||
|
||
// constructor(edgeFnDownloadURL: URL, signals: Signals) { | ||
// this.edgeFn = fetch(edgeFnDownloadURL).then(res => res.text()) | ||
// } | ||
|
||
// async process(signal: Signal): SegmentEvents => { | ||
// const scope = { | ||
// Signals: this.signalsRuntime, | ||
// analytics: new AnalyticsStub(), | ||
// processSignal: await edgeFn, | ||
// } | ||
|
||
// await this.sandbox.run("processSignal(" + JSON.stringify(signal) + ");", scope) | ||
// return analyticsStub.events | ||
// } | ||
|
||
// cleanup() { | ||
// return this.jsSandbox.destroy() | ||
// } | ||
// } |
80 changes: 80 additions & 0 deletions
80
packages/signals/browser-signals/src/core/processor/sandbox.ts
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,80 @@ | ||
import { Signal } from '../../types' | ||
|
||
// could be the buffered signals object? | ||
class SignalsRuntime { | ||
find(...args: any[]) { | ||
throw new Error('nope') | ||
} | ||
// getNextIndex(...) {} | ||
} | ||
interface BufferedSegmentEvents { | ||
track: any[] | ||
identify: any[] | ||
} | ||
|
||
class AnalyticsRuntime { | ||
events: BufferedSegmentEvents = { track: [], identify: [] } | ||
track(...args: any[]) { | ||
this.events.track.push(args) | ||
} | ||
} | ||
|
||
interface SandboxSettings { | ||
edgeFnDownloadUrl: string | URL | ||
signalsRuntime: SignalsRuntime | ||
} | ||
interface JavascriptSandbox { | ||
run: (fn: string, scope: Record<string, any>) => Promise<any> | ||
} | ||
|
||
export class Sandbox { | ||
edgeFn: Promise<string> | ||
signalsRuntime: SignalsRuntime | ||
jsSandbox: JavascriptSandbox | ||
|
||
constructor(settings: SandboxSettings) { | ||
this.edgeFn = fetch(settings.edgeFnDownloadUrl).then((res) => res.text()) | ||
this.jsSandbox = {} as any // TODO | ||
this.signalsRuntime = settings.signals | ||
} | ||
|
||
async process(signal: Signal): Promise<BufferedSegmentEvents> { | ||
const analytics = new AnalyticsRuntime() | ||
const scope = { | ||
Signals: this.signalsRuntime, | ||
analytics, | ||
processSignal: await this.edgeFn, | ||
} | ||
|
||
await this.jsSandbox.run( | ||
'processSignal(' + JSON.stringify(signal) + ');', | ||
scope | ||
) | ||
return analytics.events | ||
} | ||
} | ||
|
||
// class Sandbox { | ||
// edgeFn: Promise<string> | ||
// jsSandbox = createWorkerBox() | ||
// signalsRuntime: Signals | ||
|
||
// constructor(edgeFnDownloadURL: URL, signals: Signals) { | ||
// this.edgeFn = fetch(edgeFnDownloadURL).then(res => res.text()) | ||
// } | ||
|
||
// async process(signal: Signal): SegmentEvents => { | ||
// const scope = { | ||
// Signals: this.signalsRuntime, | ||
// analytics: new AnalyticsStub(), | ||
// processSignal: await edgeFn, | ||
// } | ||
|
||
// await this.sandbox.run("processSignal(" + JSON.stringify(signal) + ");", scope) | ||
// return analyticsStub.events | ||
// } | ||
|
||
// cleanup() { | ||
// return this.jsSandbox.destroy() | ||
// } | ||
// } |
Empty file.
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