Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate DevTools to TS, clean other files #505

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions lib/DevTools.js

This file was deleted.

104 changes: 104 additions & 0 deletions lib/DevTools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
type DevtoolsOptions = {
maxAge?: number;
name?: string;
postTimelineUpdate?: () => void;
preAction?: () => void;
logTrace?: boolean;
remote?: boolean;
};

type DevtoolsSubscriber = (message: {type: string; payload: unknown; state: string}) => void;

type DevtoolsConnection = {
send(data: Record<string, unknown>, state: Record<string, unknown>): void;
init(state: Record<string, unknown>): void;
unsubscribe(): void;
subscribe(cb: DevtoolsSubscriber): () => void;
};

const ERROR_LABEL = 'Onyx DevTools - Error: ';

type ReduxDevtools = {
connect(options?: DevtoolsOptions): DevtoolsConnection;
};

class DevTools {
private remoteDev?: DevtoolsConnection;

private state: Record<string, unknown>;

private defaultState: Record<string, unknown>;

constructor() {
this.remoteDev = this.connectViaExtension();
this.state = {};
this.defaultState = {};
}

connectViaExtension(options?: DevtoolsOptions): DevtoolsConnection | undefined {
try {
// We don't want to augment the window type in a library code, so we use type assertion instead
// eslint-disable-next-line no-underscore-dangle, @typescript-eslint/no-explicit-any
const reduxDevtools: ReduxDevtools = (window as any).__REDUX_DEVTOOLS_EXTENSION__;

if ((options && options.remote) || typeof window === 'undefined' || !reduxDevtools) {
return;
}
// eslint-disable-next-line no-underscore-dangle, @typescript-eslint/no-explicit-any
return reduxDevtools.connect(options);
} catch (e) {
console.error(ERROR_LABEL, e);
}
}

/**
* Registers an action that updated the current state of the storage
*
* @param type - name of the action
* @param payload - data written to the storage
* @param stateChanges - partial state that got updated after the changes
*/
registerAction(type: string, payload: unknown, stateChanges: Record<string, unknown> = {}) {
try {
if (!this.remoteDev) {
return;
}
const newState = {
...this.state,
...stateChanges,
};
this.remoteDev.send({type, payload}, newState);
this.state = newState;
} catch (e) {
console.error(ERROR_LABEL, e);
}
}

initState(initialState: Record<string, unknown> = {}) {
try {
if (!this.remoteDev) {
return;
}
this.remoteDev.init(initialState);
this.state = initialState;
this.defaultState = initialState;
} catch (e) {
console.error(ERROR_LABEL, e);
}
}

/**
* This clears the internal state of the DevTools, preserving the keys included in `keysToPreserve`
*/
public clearState(keysToPreserve: string[] = []): void {
const newState = Object.entries(this.state).reduce((obj: Record<string, unknown>, [key, value]) => {
// eslint-disable-next-line no-param-reassign
obj[key] = keysToPreserve.includes(key) ? value : this.defaultState[key];
return obj;
}, {});

this.registerAction('CLEAR', undefined, newState);
}
}

export default new DevTools();
66 changes: 0 additions & 66 deletions lib/MDTable.js

This file was deleted.

23 changes: 0 additions & 23 deletions lib/index.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions lib/index.js

This file was deleted.

9 changes: 9 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Onyx from './Onyx';
import type {OnyxUpdate, ConnectOptions} from './Onyx';
import type {CustomTypeOptions, OnyxCollection, OnyxEntry, NullishDeep, KeyValueMapping, OnyxKey, Selector, WithOnyxInstanceState} from './types';
import useOnyx from './useOnyx';
import withOnyx from './withOnyx';

export default Onyx;
export {withOnyx, useOnyx};
export type {CustomTypeOptions, OnyxCollection, OnyxEntry, OnyxUpdate, ConnectOptions, NullishDeep, KeyValueMapping, OnyxKey, Selector, WithOnyxInstanceState};
Loading