Skip to content

Commit

Permalink
feat(app-context): addProp
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Jan 4, 2020
1 parent 1aadbb5 commit 6e964fe
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/@nodepack/app-context/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ export async function callHookWithPayload<T> (
await callHook(hookName, ctx, payload, ...args)
return payload
}

export function addProp<TProperty = any> (ctx: any, propertyName: string, init: () => TProperty) {
let isInitialized = false
let prop: TProperty
Object.defineProperty(ctx, propertyName, {
get () {
if (!isInitialized) {
prop = init()
isInitialized = true
}
return prop
},
set () {
return prop
},
enumerable: true,
configurable: false,
})
}

0 comments on commit 6e964fe

Please sign in to comment.