Guest User

Untitled

a guest
Jun 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. /**
  2.  
  3. The skypager runtime has a feature module registry. It expects to contain
  4. named javascript modules which export a certain interface. The skypager
  5. runtime makes it easy to dynamically load, configure and enable, and then
  6. interact with the feature module as if it was a thing, with identity and state.
  7.  
  8. runtime.features.register('example', () => require('./features/example.js'))
  9.  
  10. runtime.feature('example').enable({ option: 1 })
  11. */
  12.  
  13. // when the feature is enabled, it creates a shortcut property on the runtime
  14. // e.g. runtime.example
  15. export const shortcut = 'example'
  16.  
  17. export const featureMethods = [
  18. 'publicFunctionOne',
  19. 'publicFunctionTwo',
  20. 'getSomeValue',
  21. 'lazySomeValueOnce'
  22. ]
  23.  
  24. // gets attached as runtime.example.publicFunctionOne
  25. export async function publicFunctionOne() {
  26.  
  27. }
  28.  
  29. // gets attached as runtime.example.publicFunctionTwo
  30. export function publicFunctionTwo() {
  31.  
  32. }
  33.  
  34. // gets attached as runtime.example.someValue
  35. // ideal for computed values of some kind
  36. export function getSomeValue() {
  37.  
  38. }
  39.  
  40. // gets attached as runtime.example.someValueOnce
  41. // useful for things which can be expensive and don't need to
  42. // change, or for instantiating objects which you only want to do once
  43. export function lazySomeValueOnce() {
  44.  
  45. }
  46.  
  47. /**
  48. * Feature Module Singleton Behvior
  49.  
  50. If you export isCacheable = false, every call runtime.feature('example')
  51. will produce a new instance of the feature helper.
  52.  
  53. assert(
  54. runtime.feature('example').uuid !== runtime.feature('example').uuid
  55. )
  56.  
  57. If you set this to true, then every unique call will produce the
  58. same instance of the helper.
  59.  
  60. const f1 = runtime.feature('example', { id: 'example-1' })
  61. const f2 = runtime.feature('example', { id: 'example-2' })
  62. const f3 = runtime.feature('example', { id: 'example-2' })
  63.  
  64. assert(f1.uuid !== f2.uuid)
  65. assert(f2.uuid === f3.uuid)
  66. */
  67. export const isCacheable = true
  68.  
  69. /**
  70. * Observable / Statefulness
  71. *
  72. * A feature module which exports these two properties will have
  73. * a state property which is a mobx observable shallow map
  74. * accessible via
  75. *
  76. * runtime.example.state
  77. *
  78. * runtime.example.currentState
  79. */
  80. export const isObservable = true
  81.  
  82. // runtime.example.currentState.someInitialStateValue // true
  83. // runtime.example.state.observe(({ name, oldValue, newValue }) => {})
  84. // runtime.example.state.set('someInitialStateValue', false)
  85. export const initialState = {
  86. someInitialStateValue: true
  87. }
  88.  
  89. /**
  90. * Lifecycle Hooks
  91. */
  92. export function featureWasEnabled(options = {}) {
  93. // do something when the application enables the feature
  94. // via runtime.feature('example').enable({ arbitrary: 'config' })
  95. }
  96.  
  97. // @private
  98. export async function initialize() {
  99. // do something right when the code runs
  100. // runtime.feature('example')
  101. // for the first time
  102. }
Add Comment
Please, Sign In to add comment