Implementing Methods
Keep LearnCard Weird!
Last updated
Was this helpful?
Keep LearnCard Weird!
Last updated
Was this helpful?
Sometimes plugins need to expose some bespoke logic that doesn't fit neatly into one of the . Plugin methods allow plugins to expose this logic directly on the resulting LearnCard object.
We have already seen this in action in , but let's go into a bit more depth about what's happening here by making a quick plugin that implements a basic counter.
Before implementing methods on a Plugin object, it's best to get the types in order. In general, starting with the types can be easier to think through, and once they're in place, they can guide the implementation. To add types for methods, we use the third generic argument of .
The types above have defined a Plugin with three methods: get
, increment
, and reset
, which will provide basic counter controls.
With the above types in place, we can build out a skeleton plugin before actually implementing anything:
With our boilerplate out of the way, implementing the Counter plugin will be a cakewalk! 🍰
We will use the lexical scope of the getCounterPlugin
function to store out counter state, and manipulate it via the exposed methods.
Our plugin is now complete, and we have successfully implemented bespoke method. Easy as 🍰.
Our plugin can be added to and used in a LearnCard like so: