Skip to content

Commit

Permalink
destinations as object
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Aug 7, 2023
1 parent d1652d8 commit cde0664
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/elblayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('ElbLayer', () => {
allowed: true,
consent: {},
count: expect.any(Number),
destinations: expect.any(Array),
destinations: expect.any(Object),
elbLayer: w.elbLayer,
globals: {},
group: expect.any(String),
Expand Down
12 changes: 7 additions & 5 deletions src/elbwalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ function Elbwalker(
pushToDestination(instance, destination, pushEvent);
});

instance.config.destinations.push(destination);
// @TODO use the destination id as key if given and set it to the config
// @TODO getId could be duplicate, use incremental id instead
instance.config.destinations[getId(6)] = destination;
}

function addHook<Hook extends keyof Hooks.Functions>(
Expand Down Expand Up @@ -194,7 +196,7 @@ function Elbwalker(
// Event counter for each run
count: values.count || current.count || 0,
// Destination list
destinations: values.destinations || current.destinations || [],
destinations: values.destinations || current.destinations || {},
// Async access api in window as array
elbLayer:
values.elbLayer ||
Expand Down Expand Up @@ -384,7 +386,7 @@ function Elbwalker(
// Add event to internal queue
config.queue.push(pushEvent);

config.destinations.forEach((destination) => {
Object.values(config.destinations).forEach((destination) => {
pushToDestination(instance, destination, pushEvent);
});
}
Expand Down Expand Up @@ -477,7 +479,7 @@ function Elbwalker(
instance.config.queue = [];

// Reset all destination queues
instance.config.destinations.forEach((destination) => {
Object.values(instance.config.destinations).forEach((destination) => {
destination.queue = [];
});

Expand Down Expand Up @@ -507,7 +509,7 @@ function Elbwalker(
});

if (runQueue) {
config.destinations.forEach((destination) => {
Object.values(config.destinations).forEach((destination) => {
let queue = destination.queue || [];

// Try to push and remove successful ones from queue
Expand Down
1 change: 1 addition & 0 deletions src/types/destinations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export namespace WebDestination {
interface Config<Custom = unknown, EventCustom = unknown> {
consent?: IElbwalker.Consent; // Required consent states to init and push events
custom?: Custom; // Arbitrary but protected configurations for custom enhancements
id?: string; // A unique key for the destination
init?: boolean; // If the destination has been initialized by calling the init method
loadScript?: boolean; // If an additional script to work should be loaded
mapping?: Mapping<EventCustom>; // A map to handle events individually
Expand Down
6 changes: 5 additions & 1 deletion src/types/elbwalker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export namespace IElbwalker {
allowed: boolean;
consent: Consent;
count: number;
destinations: Array<WebDestination.Function>;
destinations: Destinations;
// @TODO custom state support
elbLayer: ElbLayer;
globals: Walker.Properties;
Expand All @@ -77,6 +77,10 @@ export namespace IElbwalker {
default?: boolean;
}

interface Destinations {
[name: string]: WebDestination.Function;
}

type Events = Array<Event>;
interface Event {
event: string;
Expand Down

0 comments on commit cde0664

Please sign in to comment.