forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 66
Development
AnhMTV edited this page Jan 18, 2022
·
6 revisions
- API is define in folder
packages/extension-koni-base/src/api
- Add new file depend on types of API
- Simple API can be defined in function, more complicated API should define in Object.
Store is used to persist data into local storage. Stores is defined in folder packages/extension-koni-base/src/store
- Store class should extend class
BaseStore
orSubscribableStore
and overwrite class prefix call via constructor.-
BaseStore
include basic functions persist with chrome local storage -
SubscribableStore
extendBaseStore
, include rxjs subject and can be subscribed with methodgetSubject()
. This subject trigger every time data is set.
export default class PriceStore extends SubscribableStore<PriceJson> { constructor () { super(EXTENSION_PREFIX ? `${EXTENSION_PREFIX}price` : null); } }
-
- Defined in class
KoniState
and call byKoniExtension
,KoniTabs
orKoniCron
.export default class KoniState extends State { private readonly priceStore = new PriceStore(); private priceStoreReady = false; public setPrice (priceData: PriceJson, callback?: (priceData: PriceJson) => void): void { ... } public getPrice (update: (value: PriceJson) => void): void { ... } public subscribePrice () { return this.priceStore.getSubject(); } }
Subwallet extension use message passing concept via browser API to interact between Background - Extensions - Chrome Tabs.
- Extension or Chrome Tabs send a message with id and type to Background
- Background handle message by id and type and response data.
- There are 2 message type:
- One time message: Extension Or Chrome Tabs will send message request and listen response. Listener will be deleted after receive response.
- Subscription message: Same as one time message but listener continue receive data util window close.
- Steps to add new message handle:
- Add request type:
- New request type must define in interface
KoniRequestSignatures
export interface KoniRequestSignatures { 'pri(price.getPrice)': [RequestPrice, PriceJson] // Message type from extension 'pri(price.getSubscription)': [RequestSubscribePrice, boolean, PriceJson] // Message type from extension with subscription 'pub(utils.getRandom)': [RandomTestRequest, number] // Message type from Tabs }
- Every message type must be included:
- Type name like
pri(price.getPrice)
. Message type from extension must start withpri
, message type from Tabs must start withpub
. - Request type like
RequestPrice
- Response type like
PriceJson
- Subscription param type (optional) like
PriceJson
- Type name like
- New request type must define in interface
- Add handler (Background):
- Add new case in function handle of
KoniExtension
orKoniTabs
of packageextension-koni-base
- Add new case in function handle of
- Add caller (Extension, Chrome Tabs):
- Add new function in file
messaging.ts
of packageextension-koni-ui
to send request and handle receive data.
- Add new function in file
- Add request type:
Cronjob is define in folder packages/extension-koni-base/src/cron
.
- Group of cron action should define in separate file in this folder.
- Define new cronjob in method init of class
KoniCron
- Koni Extension UI build with React Native.
- Popup: Main extension page, show when click into extension icon in browser extension list.
- Portfolio (Coming soon): Display more complicated view like dashboard, transaction...
- Another folders:
- assets: images, resources of extensions.
- components: Common components use in extension pages.
- hooks: public hook for global function.
- i18n: Internationalization.
- stores: Redux stores generate with react hook.
- partials: Header components
- util: utilities methods.
- messaging.ts: Send to background and handle return message.
- Subwallet extension use redux-tookit to generate store.
- Define redux store reducers and state into separate file by method
createSlice
of redux toolkit. - Map reducer into root store in file index.ts
Read "Add a message handle"
Extension auto validate code with eslint. Please setup eslint in editor and run yarn lint
before commit code.
Subwallet run test with jest. Create new file with name filename.spec.ts
to write test.
- Please run
yarn lint
andyarn test