-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathsubscription.lua
More file actions
29 lines (24 loc) · 900 Bytes
/
subscription.lua
File metadata and controls
29 lines (24 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
local util = require 'util'
--- @class Subscription
-- @description A handle representing the link between an Observer and an Observable, as well as any
-- work required to clean up after the Observable completes or the Observer unsubscribes.
local Subscription = {}
Subscription.__index = Subscription
Subscription.__tostring = util.constant('Subscription')
--- Creates a new Subscription.
-- @arg {function=} action - The action to run when the subscription is unsubscribed. It will only
-- be run once.
-- @returns {Subscription}
function Subscription.create(action)
local self = {
action = action or util.noop,
}
return setmetatable(self, Subscription)
end
--- Unsubscribes the subscription, performing any necessary cleanup work.
function Subscription:unsubscribe()
action = self.action
self.action = util.noop
action(self)
end
return Subscription