-
Notifications
You must be signed in to change notification settings - Fork 70
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
28c6d5c
commit 230ef57
Showing
63 changed files
with
5,327 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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; | ||
}; | ||
declare class DevTools { | ||
private remoteDev?; | ||
private state; | ||
private defaultState; | ||
constructor(); | ||
connectViaExtension(options?: DevtoolsOptions): DevtoolsConnection | undefined; | ||
/** | ||
* 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> | null): void; | ||
initState(initialState?: Record<string, unknown>): void; | ||
/** | ||
* This clears the internal state of the DevTools, preserving the keys included in `keysToPreserve` | ||
*/ | ||
clearState(keysToPreserve?: string[]): void; | ||
} | ||
declare const _default: DevTools; | ||
export default _default; | ||
export type { DevtoolsConnection }; |
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,69 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ERROR_LABEL = 'Onyx DevTools - Error: '; | ||
class DevTools { | ||
constructor() { | ||
this.remoteDev = this.connectViaExtension(); | ||
this.state = {}; | ||
this.defaultState = {}; | ||
} | ||
connectViaExtension(options) { | ||
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 = typeof window === 'undefined' ? undefined : window.__REDUX_DEVTOOLS_EXTENSION__; | ||
if ((options === null || options === void 0 ? void 0 : options.remote) || !reduxDevtools) { | ||
return; | ||
} | ||
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, payload, stateChanges = {}) { | ||
try { | ||
if (!this.remoteDev) { | ||
return; | ||
} | ||
const newState = Object.assign(Object.assign({}, this.state), stateChanges); | ||
this.remoteDev.send({ type, payload }, newState); | ||
this.state = newState; | ||
} | ||
catch (e) { | ||
console.error(ERROR_LABEL, e); | ||
} | ||
} | ||
initState(initialState = {}) { | ||
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` | ||
*/ | ||
clearState(keysToPreserve = []) { | ||
const newState = Object.entries(this.state).reduce((obj, [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); | ||
} | ||
} | ||
exports.default = new DevTools(); |
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,22 @@ | ||
type LogData = { | ||
message: string; | ||
level: 'alert' | 'info' | 'hmmm'; | ||
}; | ||
type LoggerCallback = (data: LogData) => void; | ||
/** | ||
* Register the logging callback | ||
*/ | ||
declare function registerLogger(callback: LoggerCallback): void; | ||
/** | ||
* Send an alert message to the logger | ||
*/ | ||
declare function logAlert(message: string): void; | ||
/** | ||
* Send an info message to the logger | ||
*/ | ||
declare function logInfo(message: string): void; | ||
/** | ||
* Send an hmmm message to the logger | ||
*/ | ||
declare function logHmmm(message: string): void; | ||
export { registerLogger, logInfo, logAlert, logHmmm }; |
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,33 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.logHmmm = exports.logAlert = exports.logInfo = exports.registerLogger = void 0; | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
let logger = () => { }; | ||
/** | ||
* Register the logging callback | ||
*/ | ||
function registerLogger(callback) { | ||
logger = callback; | ||
} | ||
exports.registerLogger = registerLogger; | ||
/** | ||
* Send an alert message to the logger | ||
*/ | ||
function logAlert(message) { | ||
logger({ message: `[Onyx] ${message}`, level: 'alert' }); | ||
} | ||
exports.logAlert = logAlert; | ||
/** | ||
* Send an info message to the logger | ||
*/ | ||
function logInfo(message) { | ||
logger({ message: `[Onyx] ${message}`, level: 'info' }); | ||
} | ||
exports.logInfo = logInfo; | ||
/** | ||
* Send an hmmm message to the logger | ||
*/ | ||
function logHmmm(message) { | ||
logger({ message: `[Onyx] ${message}`, level: 'hmmm' }); | ||
} | ||
exports.logHmmm = logHmmm; |
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,137 @@ | ||
import * as Logger from './Logger'; | ||
import type { CollectionKeyBase, ConnectOptions, InitOptions, Mapping, OnyxKey, OnyxMergeCollectionInput, OnyxMergeInput, OnyxMultiSetInput, OnyxSetInput, OnyxUpdate } from './types'; | ||
/** Initialize the store with actions and listening for storage events */ | ||
declare function init({ keys, initialKeyStates, safeEvictionKeys, maxCachedKeysCount, shouldSyncMultipleInstances, debugSetState, }: InitOptions): void; | ||
/** | ||
* Subscribes a react component's state directly to a store key | ||
* | ||
* @example | ||
* const connectionID = Onyx.connect({ | ||
* key: ONYXKEYS.SESSION, | ||
* callback: onSessionChange, | ||
* }); | ||
* | ||
* @param mapping the mapping information to connect Onyx to the components state | ||
* @param mapping.key ONYXKEY to subscribe to | ||
* @param [mapping.statePropertyName] the name of the property in the state to connect the data to | ||
* @param [mapping.withOnyxInstance] whose setState() method will be called with any changed data | ||
* This is used by React components to connect to Onyx | ||
* @param [mapping.callback] a method that will be called with changed data | ||
* This is used by any non-React code to connect to Onyx | ||
* @param [mapping.initWithStoredValues] If set to false, then no data will be prefilled into the | ||
* component | ||
* @param [mapping.waitForCollectionCallback] If set to true, it will return the entire collection to the callback as a single object | ||
* @param [mapping.selector] THIS PARAM IS ONLY USED WITH withOnyx(). If included, this will be used to subscribe to a subset of an Onyx key's data. | ||
* The sourceData and withOnyx state are passed to the selector and should return the simplified data. Using this setting on `withOnyx` can have very positive | ||
* performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally | ||
* cause the component to re-render (and that can be expensive from a performance standpoint). | ||
* @param [mapping.initialValue] THIS PARAM IS ONLY USED WITH withOnyx(). | ||
* If included, this will be passed to the component so that something can be rendered while data is being fetched from the DB. | ||
* Note that it will not cause the component to have the loading prop set to true. | ||
* @returns an ID to use when calling disconnect | ||
*/ | ||
declare function connect<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): number; | ||
/** | ||
* Remove the listener for a react component | ||
* @example | ||
* Onyx.disconnect(connectionID); | ||
* | ||
* @param connectionID unique id returned by call to Onyx.connect() | ||
*/ | ||
declare function disconnect(connectionID: number, keyToRemoveFromEvictionBlocklist?: OnyxKey): void; | ||
/** | ||
* Write a value to our store with the given key | ||
* | ||
* @param key ONYXKEY to set | ||
* @param value value to store | ||
*/ | ||
declare function set<TKey extends OnyxKey>(key: TKey, value: OnyxSetInput<TKey>): Promise<void>; | ||
/** | ||
* Sets multiple keys and values | ||
* | ||
* @example Onyx.multiSet({'key1': 'a', 'key2': 'b'}); | ||
* | ||
* @param data object keyed by ONYXKEYS and the values to set | ||
*/ | ||
declare function multiSet(data: OnyxMultiSetInput): Promise<void>; | ||
/** | ||
* Merge a new value into an existing value at a key. | ||
* | ||
* The types of values that can be merged are `Object` and `Array`. To set another type of value use `Onyx.set()`. | ||
* Values of type `Object` get merged with the old value, whilst for `Array`'s we simply replace the current value with the new one. | ||
* | ||
* Calls to `Onyx.merge()` are batched so that any calls performed in a single tick will stack in a queue and get | ||
* applied in the order they were called. Note: `Onyx.set()` calls do not work this way so use caution when mixing | ||
* `Onyx.merge()` and `Onyx.set()`. | ||
* | ||
* @example | ||
* Onyx.merge(ONYXKEYS.EMPLOYEE_LIST, ['Joe']); // -> ['Joe'] | ||
* Onyx.merge(ONYXKEYS.EMPLOYEE_LIST, ['Jack']); // -> ['Joe', 'Jack'] | ||
* Onyx.merge(ONYXKEYS.POLICY, {id: 1}); // -> {id: 1} | ||
* Onyx.merge(ONYXKEYS.POLICY, {name: 'My Workspace'}); // -> {id: 1, name: 'My Workspace'} | ||
*/ | ||
declare function merge<TKey extends OnyxKey>(key: TKey, changes: OnyxMergeInput<TKey>): Promise<void>; | ||
/** | ||
* Merges a collection based on their keys | ||
* | ||
* @example | ||
* | ||
* Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, { | ||
* [`${ONYXKEYS.COLLECTION.REPORT}1`]: report1, | ||
* [`${ONYXKEYS.COLLECTION.REPORT}2`]: report2, | ||
* }); | ||
* | ||
* @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT` | ||
* @param collection Object collection keyed by individual collection member keys and values | ||
*/ | ||
declare function mergeCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TKey, collection: OnyxMergeCollectionInput<TKey, TMap>): Promise<void>; | ||
/** | ||
* Clear out all the data in the store | ||
* | ||
* Note that calling Onyx.clear() and then Onyx.set() on a key with a default | ||
* key state may store an unexpected value in Storage. | ||
* | ||
* E.g. | ||
* Onyx.clear(); | ||
* Onyx.set(ONYXKEYS.DEFAULT_KEY, 'default'); | ||
* Storage.getItem(ONYXKEYS.DEFAULT_KEY) | ||
* .then((storedValue) => console.log(storedValue)); | ||
* null is logged instead of the expected 'default' | ||
* | ||
* Onyx.set() might call Storage.setItem() before Onyx.clear() calls | ||
* Storage.setItem(). Use Onyx.merge() instead if possible. Onyx.merge() calls | ||
* Onyx.get(key) before calling Storage.setItem() via Onyx.set(). | ||
* Storage.setItem() from Onyx.clear() will have already finished and the merged | ||
* value will be saved to storage after the default value. | ||
* | ||
* @param keysToPreserve is a list of ONYXKEYS that should not be cleared with the rest of the data | ||
*/ | ||
declare function clear(keysToPreserve?: OnyxKey[]): Promise<void>; | ||
/** | ||
* Insert API responses and lifecycle data into Onyx | ||
* | ||
* @param data An array of objects with update expressions | ||
* @returns resolves when all operations are complete | ||
*/ | ||
declare function update(data: OnyxUpdate[]): Promise<void>; | ||
declare const Onyx: { | ||
readonly METHOD: { | ||
readonly SET: "set"; | ||
readonly MERGE: "merge"; | ||
readonly MERGE_COLLECTION: "mergecollection"; | ||
readonly MULTI_SET: "multiset"; | ||
readonly CLEAR: "clear"; | ||
}; | ||
readonly connect: typeof connect; | ||
readonly disconnect: typeof disconnect; | ||
readonly set: typeof set; | ||
readonly multiSet: typeof multiSet; | ||
readonly merge: typeof merge; | ||
readonly mergeCollection: typeof mergeCollection; | ||
readonly update: typeof update; | ||
readonly clear: typeof clear; | ||
readonly init: typeof init; | ||
readonly registerLogger: typeof Logger.registerLogger; | ||
}; | ||
export default Onyx; | ||
export type { OnyxUpdate, Mapping, ConnectOptions }; |
Oops, something went wrong.