Skip to content

Commit

Permalink
restartWebflow: Preserved ix2 state after re-initting the module.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiglesias93 committed Nov 15, 2021
1 parent b472ac9 commit be02218
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@finsweet/ts-utils",
"version": "0.24.2",
"version": "0.24.3",
"description": "Typescript utils for custom Webflow projects.",
"main": "index.ts",
"module": "index.ts",
Expand Down
49 changes: 41 additions & 8 deletions types/Webflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,54 @@ type Callback = () => unknown;

export type WebflowModule = 'ix2' | 'commerce' | 'lottie' | 'lightbox';

interface WebflowCommerce {
destroy: () => void;
init: (params: { siteId: string; apiUrl: string }) => void;
}

interface WebflowIx2 {
destroy: () => void;
init: () => void;
actions: {
[key: string]: (...params: unknown[]) => unknown;
};
store: {
dispatch: (param: unknown) => void;
getState: () => {
ixData: {
actionLists: unknown;
eventTypeMap: unknown;
events: unknown;
mediaQueries: unknown;
mediaQueryKeys: unknown;
};
ixElements: {
[key: string]: unknown;
};
ixInstances: {
[key: string]: unknown;
};
ixRequest: {
[key: string]: unknown;
};
ixSession: {
eventState: {
[key: string]: unknown;
};
[key: string]: unknown;
};
};
};
}

/**
* Includes methods of the Webflow.js object
*/
export interface Webflow extends Pick<Callback[], 'push'> {
destroy: () => void;
ready: () => void;
env: () => boolean;
require: <Key extends WebflowModule>(
key: Key
) =>
| {
destroy: () => void;
init: Key extends 'commerce' ? (params: { siteId: string; apiUrl: string }) => void : () => void;
}
| undefined;
require: <Key extends WebflowModule>(key: Key) => (Key extends 'commerce' ? WebflowCommerce : WebflowIx2) | undefined;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion webflow/restartWebflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ export const restartWebflow = async (modules?: WebflowModule[]): Promise<unknown
const ix2 = Webflow.require('ix2');

if (ix2) {
ix2.destroy();
const { store, actions } = ix2;
const { eventState } = store.getState().ixSession;

if (!modules) ix2.destroy();

ix2.init();

for (const state of Object.entries(eventState)) store.dispatch(actions.eventStateChanged(...state));
}
}

Expand Down

0 comments on commit be02218

Please sign in to comment.