Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- The skypager runtime has a feature module registry. It expects to contain
- named javascript modules which export a certain interface. The skypager
- runtime makes it easy to dynamically load, configure and enable, and then
- interact with the feature module as if it was a thing, with identity and state.
- runtime.features.register('example', () => require('./features/example.js'))
- runtime.feature('example').enable({ option: 1 })
- */
- // when the feature is enabled, it creates a shortcut property on the runtime
- // e.g. runtime.example
- export const shortcut = 'example'
- export const featureMethods = [
- 'publicFunctionOne',
- 'publicFunctionTwo',
- 'getSomeValue',
- 'lazySomeValueOnce'
- ]
- // gets attached as runtime.example.publicFunctionOne
- export async function publicFunctionOne() {
- }
- // gets attached as runtime.example.publicFunctionTwo
- export function publicFunctionTwo() {
- }
- // gets attached as runtime.example.someValue
- // ideal for computed values of some kind
- export function getSomeValue() {
- }
- // gets attached as runtime.example.someValueOnce
- // useful for things which can be expensive and don't need to
- // change, or for instantiating objects which you only want to do once
- export function lazySomeValueOnce() {
- }
- /**
- * Feature Module Singleton Behvior
- If you export isCacheable = false, every call runtime.feature('example')
- will produce a new instance of the feature helper.
- assert(
- runtime.feature('example').uuid !== runtime.feature('example').uuid
- )
- If you set this to true, then every unique call will produce the
- same instance of the helper.
- const f1 = runtime.feature('example', { id: 'example-1' })
- const f2 = runtime.feature('example', { id: 'example-2' })
- const f3 = runtime.feature('example', { id: 'example-2' })
- assert(f1.uuid !== f2.uuid)
- assert(f2.uuid === f3.uuid)
- */
- export const isCacheable = true
- /**
- * Observable / Statefulness
- *
- * A feature module which exports these two properties will have
- * a state property which is a mobx observable shallow map
- * accessible via
- *
- * runtime.example.state
- *
- * runtime.example.currentState
- */
- export const isObservable = true
- // runtime.example.currentState.someInitialStateValue // true
- // runtime.example.state.observe(({ name, oldValue, newValue }) => {})
- // runtime.example.state.set('someInitialStateValue', false)
- export const initialState = {
- someInitialStateValue: true
- }
- /**
- * Lifecycle Hooks
- */
- export function featureWasEnabled(options = {}) {
- // do something when the application enables the feature
- // via runtime.feature('example').enable({ arbitrary: 'config' })
- }
- // @private
- export async function initialize() {
- // do something right when the code runs
- // runtime.feature('example')
- // for the first time
- }
Add Comment
Please, Sign In to add comment