You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One of dj's most powerful methods is dj.hook(). Its mission is military-grade extensibility. It is designed to be:
safe - meaning that its design makes it hard for one bad line to break a whole app. It provides a safe access point for otherwise decoupled functionality.
adaptable - meaning that it can be applied to many situations, and that its capabilities are reusable. It can store functions that hook into points of other functions and/or can be used to store/access entire modules.
logical - meaning that it is systematic. It follows simple rules based on typeof checks. Thus it aims to be very reliable and learnable.
Over the last few months I've been developing the syntax/features and I'd like to get opinions on them now so that we can make any needed changes sooner rather than later. dj.hook() stores hooks in a private hash object. Its syntax is loosely analogous to jQuery's data methods.
Keys (identifiers) must be typeof "string" or "number". Each key represents a single value (not a stack). When you set a value it overrides the previous value. Each hook's first value is retained privately such that it is possible to restore default hooks.
Values (hooks) are restricted to being either typeof "function" or "object" and not null. Objects can be upgraded to functions, but not vice versa. This makes it so that if a hook is initiated as a function, then it will remain so. For safety, values cannot easily be deleted.
// basic usagedj.hook(key)// get a hook valuedj.hook(key,value)// set a hook value (function or object)// heavy liftingdj.hook()// get all (get a clone of the hash at its current state)dj.hook(undefined)// same as dj.hook()dj.hook(object)// set multiple hooks via an object's key/value pairsdj.hook(object,prefix)// like above + prefix is added to each keydj.hook(callback)// same as dj.hook( callback.call(this, dj.hook()) )dj.hook.remix()// return a new hook method tied to a fresh hash// opsdj.hook(key,false)// lock a hook at its current statedj.hook(false)// lock all hooks at their current statedj.hook(key,true)// restore a hook's default valuedj.hook(true)// restore all default hooks// detectsdj.hook(key,null)// get status (true => writable, false => locked)dj.hook(null)// get status (true => all writable, false => all locked)
It might be slightly more expressive to have separate submethods for the "ops" or "detects", but it seems safer and much terser to have them all wrapped in one function. Having them all within ensures that the operations are done on the intended hash. Typechecking is necessary regardless so it is rather efficient under the hood. (See source.)
The purpose of this thread is to help make sure that the syntax is intuitive/expressive enough and that the right features are there. Opinions?
The text was updated successfully, but these errors were encountered:
I'm planning on adding the ability to call a callback each time a hook value is updated, such that you could listen to a hook that is uninitiated and then do something if/when it is. This would add asynchronous possibilities and could also be used to trigger custom events. I'd like to implement this with a syntax analogous to jQuery's on/off methods:
// planned future featuresdj.hook.on(callback)// call callback each time the hash is changeddj.hook.on(key,callback)// call callback each time key's hook is changeddj.hook.off(callback)// remove callback previously added via dj.hook.ondj.hook.off(key,callback)// remove callback previously added via dj.hook.on
Feel free to ignore this comment, but taking a only quick look at dj and dj.hook, and reading this thread, I’m not sure of the intended use cases of dj.hook.
One of dj's most powerful methods is
dj.hook()
. Its mission is military-grade extensibility. It is designed to be:typeof
checks. Thus it aims to be very reliable and learnable.Over the last few months I've been developing the syntax/features and I'd like to get opinions on them now so that we can make any needed changes sooner rather than later.
dj.hook()
stores hooks in a private hash object. Its syntax is loosely analogous to jQuery's data methods.Keys (identifiers) must be typeof
"string"
or"number"
. Each key represents a single value (not a stack). When you set a value it overrides the previous value. Each hook's first value is retained privately such that it is possible to restore default hooks.Values (hooks) are restricted to being either typeof
"function"
or"object"
and notnull
. Objects can be upgraded to functions, but not vice versa. This makes it so that if a hook is initiated as a function, then it will remain so. For safety, values cannot easily be deleted.Version 0.7.0 implementation:
It might be slightly more expressive to have separate submethods for the "ops" or "detects", but it seems safer and much terser to have them all wrapped in one function. Having them all within ensures that the operations are done on the intended hash. Typechecking is necessary regardless so it is rather efficient under the hood. (See source.)
The purpose of this thread is to help make sure that the syntax is intuitive/expressive enough and that the right features are there. Opinions?
The text was updated successfully, but these errors were encountered: